import socket def main(): """简单web服务器""" # 1、创建套间字 web_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) web_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 如果强制服务端先调用close,则再次链接需要转换端口,不然端口会被占用等待客户端的反馈 web_socket.bind((‘192.168.0.106‘,8080)) web_socket.listen(128) # 128是指最大运行客户端的链接数 while True: resp_socket, resp_addr = web_socket.accept() recv_data = resp_socket.recv(1024) print(recv_data) resp_data = ‘HTTP/1.1 200 OK\r\n‘ + "\r\n" +‘hahaha‘ # 浏览器识别的换行为\r\n resp_socket.send(resp_data.encode(‘utf-8‘)) resp_socket.close web_socket.close() if __name__ == ‘__main__‘: main()
原文地址:https://www.cnblogs.com/kogmaw/p/12602492.html
时间: 2024-10-03 07:58:45