a issue about python socket -
I have terminated a Python socket server script. I and I want to connect it to the browser, and then the socket server is something The data will be sent to the browser which will display the data message. The server can accept the request from the socket browser, but the server can not send data messages in the browser, the browser is in a blocked position for a long time. Why? Server socket script is as follows, please help me review this code, thank you! Import socket HOST = '127.0.0.1' ports = 9003 buffer = 4096 sock = socket.socket (socket (FINNET, socket.SOCECRRM) sock.bind (HOST, PORT) sock .listen (0) Print ('Listen to TCP server:% s:% s \ n \ r'% (HOST, PORT)) True: Client_sock, client_addr = sock.accept () Print ('% s:% s Connect 'Client_addr' is correct: recv = client_sock.recv (buffer) if not recv: client_sock.close () break print ('[client% s Client_addr [0], client_addr [1], recv)) client_sock.send (' HTTP / 1.1 200 is OK \ n Content Type: Text / html \ r \ n \ r \ N tcpServer has received your message ') sock.close ()
You just close Note that this also means that you have to wait for any other connection to go directly, so I have removed the inner loop from your code. client_sock after sending the response
import socket HOST = '127.0. 0.1 'port = 9003 buffer = 4096 sock.socket (socket.AF_INET, Socket.SOCK_STREAM) sock.bind (HOST, PORT) sock.listen (0) print (' tcpServer Listen:% s:% S \ N \ r '% (HOST, PORT) is correct: client_sock, client_addr = sock.accept () print ('% s:% s connect '% client_addr) recv = client_sock.recv (buffer) if not recv: client_sock. Close () Brake print ('[client% s:% s said]:% s'% (client_addr [0], client_addr [1], recv)) client_sock.send (' http / 1.1 200 OK \ n content-type : Text / html \ r \ n \ r \ nTCP server is yours Message received ') client_sock.close () sock.close ()
Comments
Post a Comment