-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
38 lines (36 loc) · 1.08 KB
/
server.py
File metadata and controls
38 lines (36 loc) · 1.08 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
#import socket module
from socket import *
serverPort=80
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a server socket
#Fill in start
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print 'the web server is up on port:',serverPort
#Fill in end
while True:
#Establish the connection
print 'Ready to serve...'
connectionSocket, addr = serverSocket.accept()
try:
message = connectionSocket.recv(1024)
print message,'::',message.split()[0],':',message.split()[1]
filename = message.split()[1]
print filename,'||',filename[1:]
f = open(filename[1:])
outputdata = f.read()
print outputdata
#Send one HTTP header line into socket
#Fill in start
connectionSocket.send('\nHTTP/1.1 200 OK\n\n')
connectionSocket.send(outputdata)
#Fill in end
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i])
connectionSocket.close()
except IOError:
#Send response message for file not found
#Fill in start
connectionSocket.send('\nHTTP/1.1 404 Not Found\n\n')
connectionSocket.send('\nHTTP/1.1 404 Not Found\n\n')