-
Notifications
You must be signed in to change notification settings - Fork 221
/
Server.py
33 lines (33 loc) · 1.11 KB
/
Server.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
#改为Python3格式
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverPort = 80
serverSocket.bind(("", serverPort))
serverSocket.listen(1)
while True:
#Establish the connection
print('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
try:
message = connectionSocket.recv(1024)
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.read()
f.close()
#Send one HTTP header line into socket
outputdata = 'HTTP/1.1 200 OK\r\n\r\n' + outputdata
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.close()
print("OK!")
except IOError:
#Send response message for file not found
outputdata = 'HTTP/1.1 404 Not Found\r\n\r\n'
#Close client socket
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.close()
serverSocket.close()