Code:
1 #socket test with Forkingmixin class2 3 ImportOS4 ImportSocket5 ImportSocketserver6 ImportThreading7 #From multiprocessing import Process8 9Server_host ="localhost"TenServer_port = 0#Indicat to use Dyminic port OneBuf_size = 1024 AEcho_msg ="Hello Echo Server" - - the classforkingclient (): - """a client to use forking server""" - def __init__(self, IP, port): -Self.sock =Socket.socket (socket.af_inet, socket. SOCK_STREAM) + self.sock.connect ((IP, port)) - + defRun (self): A """client send infor to server""" at #get current client ' s IP -current_process_id =os.getpid () - Print "PID%s sending echo messages to server:%s"% - (current_process_id, echo_msg) - #Use sock.send () to get length -Sent_data_length =self.sock.send (echo_msg) in Print "%s characters sent so far ..."%Sent_data_length - to #get the server response +S_response =self.sock.recv (buf_size) - #DISP The server response, split S_response the Print "PID%s Received:%s"% (current_process_id, s_response[5:] ) * $ defClientshutdown (self):Panax Notoginseng self.sock.close () - the #def server ' s handle MTHD + classForkingserverrequesthandler (socketserver.baserequesthandler): A defhandle (self): the #get echo from client, notice "REQUEST.RECV ()" +data =self.request.recv (buf_size) -current_process_id =os.getpid () $S_response ='%s:%s'%(current_process_id, data) $ #send echo back to Client - Print "Server sending back [Current_process_id:data]%s"% - S_response the #Use . Request.send () Mthd - self.request.send (s_response)Wuyi #Use return the return - Wu classforkingserver (Socketserver.forkingmixin, socketserver.tcpserver): - """just inherit everything from parents""" About Pass $ - defMain (): - #build the server like TCPServer initiate -Server =forkingserver (Server_host, Server_port), Forkingserverrequesthandler) A +IP, port = server.server_address#Get server ' s IP and Port the #Serve_forever Handle requests until an explicit shutdown () request. -Server_thread = Threading. Thread (target=server.serve_forever) $ Server_thread.setdaemon (True) the the #Server Start the Server_thread.start () the #Server_thread.join () - Print "Server Loop runing PID:%s"%os.getpid () in the #build client and start the AboutCLIENT1 = forkingclient (IP, port)#Use server ' s IP and Port the Client1.run () the theClient2 =forkingclient (IP, port) + Client2.run () - the # CleanBayi Server.shutdown () the Client1.clientshutdown () the Client2.clientshutdown () - server.socket.close () - the if __name__=='__main__': theMain ()
The program notes are as follows:
1. First run the Times on Windows error as follows, in the Help document query to Os.fork can only run on UNIX platforms:
Error:
File "C:\Python27\lib\SocketServer.py", line 556, in Process_request
PID = Os.fork ()attributeerror: ' Module ' object has no attribute ' fork '
os.fork () description:
-
-
Fork a child process. Return 0 in the child and the child's process ID in the parent. If An error occurs oserror is raised.
Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX has known issues when using fork () from a th Read.
availability:unix.
2. After the main block finishes sending and receiving server and client, the server executes shutdown () then execute socket.close (), why do you do this?
shutdown () closes the read-write connection and sends a fin/eof signal to all nodes, Regardless of how many processes are connected to the server, but it does not release the connection, call Close () again to release the
Python network programming (i)