-
Notifications
You must be signed in to change notification settings - Fork 4
/
#Webserver.py
83 lines (59 loc) · 2.06 KB
/
#Webserver.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from socket import *
#Start test()
def test():
#Specify the port
serverPort = 80
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',serverPort))
#Listen for the 1 connection
serverSocket.listen(1)
#Print the port address
print("web server on port",serverPort)
#Start thw while loop.
while True:
#Establish the connection.
print("ready to serve")
#Create connection socket for accepted client.
connectionSocket,addr = serverSocket.accept()
#Start the try block.
try:
#Recieve message.
message = connectionSocket.recv(1024)
#Print the connection message
print(message)
#Determine the filename
filename = message.split()[1]
#Print the file name
print(filename[1])
print(filename,'||',filename[1])
#Open the file
f = open(filename[1:])
outputdata = f.read()
#DEBUG to check output data
print(outputdata)
#Send one HTTP header line into socket
connectionSocket.send("""HTTP/1.0 200 OK
Content-Type: text/html
<html>
<head>
<title>Success</title>
</head>
<body>
Your file Exist!
</body>
</html>
""".encode());
#connectionSocket.send(outputdata)
#connectionSocket.send(message)
connectionSocket.close()
#If IOError
except IOError:
#Send response message for the file not found.
print ("404 Not Found")
connectionSocket.send("""HTTP/1.0 404 Not Found\r\n""".encode());
pass
#Temp break
break
pass
if __name__ =="__main__":
test()