This repository was archived by the owner on Feb 3, 2022. It is now read-only.
forked from asimshrestha2/socket-python-OS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.py
More file actions
68 lines (55 loc) · 2.11 KB
/
webserver.py
File metadata and controls
68 lines (55 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import socket
import os
import io
from requestapp import Requestapp
HOST, PORT = '', 8888
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(4)
print 'Serving HTTP on port %s ...' % PORT
while True:
client_connection, client_address = listen_socket.accept()
request = client_connection.recv(1024)
print request
req = request.split("\r\n")
protocal = req[0].split(" ")
try:
filename = protocal[1]
print filename
if (protocal[0] == "POST"):
dataArray = req[len(req)-1].split("&")
#print dataArray
data = {}
for dataEntry in dataArray:
#print dataEntry
naval = dataEntry.split('=')
data[naval[0]] = naval[1]
print data
if (filename == "/update"):
newfile = False
if not os.path.exists('./data/'+data['room']):
newfile = True
os.makedirs('./data/'+data['room'])
roomdata = io.open('./data/'+data['room']+'/1', 'w+', encoding='utf8')
if newfile or roomdata.read() == "":
roomdata.write(u'0,0\n')
roomdata.close();
filecontent = ""
with open('./data/'+data['room']+'/1', 'r') as f:
filecontent = f.read()
print filecontent
http_response = Requestapp(filename, filecontent).getResponse()
elif (protocal[0] == "GET"):
if(filename == "/"):
rf = open('./serverfiles/index.html', 'r')
filename = "/index.html"
else:
rf = open('./serverfiles' + filename, 'r')
filecontent = rf.read()
http_response = Requestapp(filename, filecontent).getResponse()
except Exception as e:
print e
http_response = Requestapp("", "", 404).getResponse()
client_connection.sendall(http_response)
client_connection.close()