From socket import * from time import ctimeimport OS, sys # sys defines the process exit method HOST = 'localhost' # defines the host port = 21567 # defines the PORT number BUFSIZ = 1024 # defines the buffer ADDR = (HOST, PORT) # define tcpSerSock = socket (AF_INET, SOCK_STREAM) # generate TCP SockettcpSerSock. bind (ADDR) # bind the Socket to the tcpSerSock. listen (5) # listen to the request while True: # define an infinite loop (more and more like shell) print "waiting .... "try: # The tcpSerSock statement is destroyed in the generation process. If this exception is not caught, a program error will occur! TcpCliSock, addr = tcpSerSock. accept () failed T: # capture all exceptions sys. exit (0) # exit the current process as long as the blocking failure is captured. (Only the generated process can cause blocking failure. Only the words tcpSerSock are destroyed in the generated process.) if OS. fork (): # If the operation in the main process passes else: # If the operation in the derived process prints 'Con from: ', addr while True: data = tcpCliSock. recv (BUFSIZ) if not data: tcpSerSock. close () # destroy tcpSerSock break # Jump out of the loop! TcpCliSock. send ('[% s] % s' % (ctime (), data) tcpSerSock. close () # This sentence will never be executed!