1 #!/usr/bin/env python 2 import socket 3 def handle_request(client): 4 buf = client.recv(1024) 5 client.sendall(bytes("HTTP/1.1 201 OK ","utf8")) 6 with open("a.html") as f: 7 data = f.read() 8 client.sendall(bytes(data,"utf8")) 9 10 def main(): 11 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 12 sock.bind(('localhost',8082)) 13 sock.listen(5) 14 15 while True: 16 connection, address = sock.accept() 17 handle_request(connection) 18 connection.close() 19 20 if __name__ == '__main__': 21 22 main()
a.html文件内容:
<h1 style='color:green'>Hello, World</h1>