扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
师父布置的任务,让我写一个服务练练手,搞清楚socket的原理和过程后跑了一个小demo,很有成就感,代码内容也比较清晰易懂,很有教育启发意义。
代码
# coding:utf-8 import socket from multiprocessing import Process HTML_ROOT_DIR = "" def handle_client(client_socket): """处理客户端请求""" # 获取客户端请求数据 request_data = client_socket.recv(1024) print("request data:", request_data) # 构造响应数据 response_start_line = "HTTP/1.1 200 OK\r\n" response_headers = "Server: My server\r\n" response_body = "hello itcast" response = response_start_line + response_headers + "\r\n" + response_body print("response data:", response) # 向客户端返回响应数据 client_socket.send(bytes(response, "utf-8")) # 关闭客户端连接 client_socket.close() if __name__=="__main__": server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(("", 8000)) server_socket.listen(120) while True: client_socket, client_address = server_socket.accept() # print("[%s, %s]用户连接上了"%client_addrest[0],client_address[1]) print("[%s, %s]用户连接上了" % client_address) handle_client_process = Process(target=handle_client, args=(client_socket,)) handle_client_process.start() client_socket.close()
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流