"Python" network programming-socketserver for non-blocking communication between client and server

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.