In order to determine the occupation of the Socket and Communication Socket to the port, write the following code for testing: server.py function After starting the Socket, the Accept connection, turn off the listening socket, communicate with the port received by Accept. Client.py function To connect Server, send reception data, whether the test connection is normal. The test results found that in different operating system operations Different: Under Windows 2003: After the client connects to Server, Server closes the listener port, then start a server process again, It can be started successfully. Under Redhat Linux9: When the client connects to Server, Server closes the listener port, start a server process again, prompt failure, port is occupied. Use netstat -an query, port actually does not in the Listen state The test code is as follows, Server.py: import.py: IMPORT SOCKET Import Time Sock = Socket.socket (socket.af_inet, socket.sock_stream) Sock.bind (("127.0.0.1", 7920) Sock.Listen (1) Print 'Server [% d] listen ... '% sock.fileno () conn, rhost = sock.accept () rhost = rhost [0] if rhost: print' got connection [% D] from:% s'% (conn. Fileno (), RHOST SOCK.CLOSE () # After successfully receiving the client connection, close the listener Socket Print "Server Socket Close" for I in Range (1000): # send a request to the host recvdata = conn.recv ( 100) Print recvdata if recvdata == "end!": Print "Recv end command socket close!" Break; conn.send ("% 04drecved"% i) Time.sleep (2) # get the host's response, no more Than , SAY, 1, 024 BYTES Conn.close () Client code is as follows, client.py: import socket import time sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) remote_host = "127.0.0.1" remote_port = 7920 # Connect to the remote host and port sock.connect ((remote_host, remote_port)) for i in range (100): # send a request to the host sock.send ("Client send:% 04d / r / n"% i) # Get the Host's response, no more Than, Say, 1,024 BYTES response_data = Sock.Recv (1024 PRINT RESPONSE_DATA TIME.SLEP (2) Else: Print "End No Error!" sock.send ("end!") # Terminate sock.close ()