-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver.py
44 lines (39 loc) · 1.06 KB
/
httpserver.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
from socket import *
#接收,查看,返回客户端请求内容
def handleClient(connfd):
request = connfd.recv(1024)
# print('******')
# print(request)
# print('******')
request_lines = request.splitlines()
for line in request_lines:
print(line.decode())
try:
f = open('index.html')
except IOError:
response = "HTTP/1.1 404 NOT FOUND\r\n"
response += "\r\n" #空行
response += '''
sorry
'''
else:
response = "HTTP/1.1 200 OK \r\n"
response += '\r\n'
response += f.read()
finally:
connfd.send(response.encode())
#创建套接字,调用handleclient完成功能
def main():
#创建TCP套接字
sockfd = socket()
sockfd.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
sockfd.bind(('0.0.0.0' ,8000))
sockfd.listen()
while True:
print("Listen the port 8000...")
connfd,addr = sockfd.accept()
#处理浏览器发来的请求
handleClient(connfd)
connfd.close()
if __name__ =="__main__":
main()