-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebServer.py
32 lines (29 loc) · 1.13 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
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverSocket.bind(('', 6789)) # 将TCP欢迎套接字绑定到指定端口
serverSocket.listen(1) # 最大连接数为1
while True:
#Establish the connection
print('Ready to serve...')
connectionSocket, addr = serverSocket.accept() # 接收到客户连接请求后,建立新的TCP连接套接字
try:
message = connectionSocket.recv(1024) # 获取客户发送的报文
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.read();
#Send one HTTP header line into socket
header = ' HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html\nContent-Length: %d\n\n' % (len(outputdata))
connectionSocket.send(header.encode())
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.close()
except IOError:
#Send response message for file not found
header = ' HTTP/1.1 404 Not Found'
connectionSocket.send(header.encode())
#Close client socket
connectionSocket.close()
serverSocket.close()