The Socketserver module is used to realize network client-server concurrent connection non-blocking communication.
First, let's look at the classes available in the next Socketserver module:
Baseserver: The core functionality of the containing server is linked to the hybrid (Mix-in) class, and this class is used only for derivation, so instances of this class are not generated; Consider using TCPServer and udpserver.
Tcpserver/udpserver: Basic network Synchronization TCP/UDP server.
Unixstreamserver/unixdatagramserver: Basic file-based synchronization tcp/udp server.
Forkingmixin/threadingmixin: The core process or threading function is implemented, and as a hybrid class, it is used with the server class to provide some asynchronous features; This class is not instantiated directly.
Combination of Forkingtcpserver/forkingudpserver:forkingmixin and tcpserver/udpserver.
Baserequesthandler: Contains core functionality for processing service requests. This class is used only for derivation, so instances of this class are not generated to consider using Streamrequesthandler or Datagramrequesthandler.
Streamrequesthandler/datagramrequesthandler: Service Processing tool for TCP/UDP server.
Below we formally enter the topic, here we use Streamrequesthandler and Threadingtcpserver to implement the client and server concurrent connection non-blocking socket.
Threadingtcpserver derives from ThreadingMixIn, which mainly implements the core process of combining threading functionality.
Streamrequesthandler is primarily used for service processing tools for TCP/UDP servers.
First, create SOCKETSERVERTCP server
#创建SocketServerTCP服务器: Import socketserverfrom socketserver import Streamrequesthandler as Srhfrom time import ctimehost = ' xxx.xxx.xxx.xxx ' port = 9999addr = (Host,port) class Servers (SRH): def handle (self): print ' Got connection from ', self.client_address self.wfile.write (' Connection%s:%s at%s succeed! '% (Host,port,ctime ())) while True: data = SELF.REQUEST.RECV (1024x768) if not data: break print data print ' recv from ', Self.client_ Address[0] self.request.send (data) print ' server is running .... ' Server = socketserver.threadingtcpserver (addr, Servers) Server.serve_forever ()
Second, create SOCKETSERVERTCP client
From socket import *host = ' xxx.xxx.xxx.xxx ' port = 9999bufsize = 1024addr = (host,port) client = socket (Af_inet,sock_stream ) Client.connect (addr) while True: data = raw_input () if not data or data== ' exit ': break client.send ('% s\r\n '% data ' data = CLIENT.RECV (bufsize) if not data: break print Data.strip () client.close ()
"Python" network programming-socketserver for non-blocking communication between client and server