标签:app pre pass stream list() try rem 四次挥手 http
import socket """ 短链接:传输三个数据,传一个进行一次三次握手四次挥手,再传输一个,再建立一个三次握手四次挥手...... 长链接:只建立一次链接,传三个数据 """ def tcp_serve(resp_socket): resp_body = ‘hahaha‘ resp_header = ‘HTTP/1.1 200 OK\r\n‘ + ‘Content-Length:%d\r\n‘ % len(resp_body) + "\r\n" + "\r\n" # ‘Content-Length,这个参数可以告诉浏览器此次传输的数据有多长,以便浏览器判断此次数据是否接受完成 resp_data = resp_header + resp_body resp_socket.send(resp_data.encode(‘utf-8‘)) def main(): web_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) web_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) web_socket.bind((‘192.168.0.106‘,8080)) web_socket.listen(128) web_socket.setblocking(False) client_socket_list = list() while True: try: resp_socket, resp_addr = web_socket.accept() except Exception as e: pass else: resp_socket.setblocking(False) client_socket_list.append(resp_socket) for client_socket in client_socket_list: try: recv_data = client_socket.recv(1024) except Exception as e: pass else: if recv_data: tcp_serve(client_socket) print(recv_data) else: client_socket.close() client_socket_list.remove(client_socket) web_socket.close() if __name__ == ‘__main__‘: main()
标签:app pre pass stream list() try rem 四次挥手 http
原文地址:https://www.cnblogs.com/kogmaw/p/12602513.html