Python path-socketserver for multiple concurrency

Source: Internet
Author: User

    • Python path-socketserver for multiple concurrency
      • Read the guide??
      • Socketserver??
      • Achieve multiple concurrency??
Read the guide??

In the above finishing chapter, the Simple network programming basically already, a TCP, a UDP, then is the sticky packet problem

But there is a problem in the above, in the real life, a server must often serve several clients at the same time, and the above chapters do not achieve a one-to-many simultaneous situation, TCP can only wait for the previous link to disconnect the subsequent to connect, not even on the have been; UDP is then sent once, and can not be simultaneous two times. In order to deal with this problem, which is implementation concurrency (follow-up article in detail), Python has a socketserver module to meet our requirements

Socketserver??

Python provides two levels of access to network services:

    1. The low-level network service supports the basic socket, which provides the standard BSD socket API to access all the methods of the underlying operating system socket interface
    2. High-level Network Service Module Socketserver, which provides a server-centric class that simplifies the development of Web servers

The socket is needless to say, now Socketserver

We know that TCP-based sockets, the key is two loops, a link loop (multiplayer), a communication loop (multi-message)

There are two main classes in the Socketserver module: the server class (to solve the link problem) and the request class (to solve the communication problem)

For further information, check out the official documentation < Socketserver official documentation >

Achieve multiple concurrency??

multi_socketserver_server.py

Import SocketserverClassMyServer(Socketserver. Baserequesthandler):DefHandle(self):# Create a link that inherits from the Baserequesthandler class in Socketserver conn = Self.request# Send login Prompt Conn.sendall (B "Welcome to login ...") Print ("Client Connect ...")WhileTrue:print ("Waitting for recving message ...")# Receive message = CONN.RECV (1024x768) Print (Message.decode (' Utf-8 '))# exit when you receive exitif message = =  "exit": break # reply message data = input ( "Reply message:") # Send Message Conn.sendall (Data.encode ( ' utf-8 ')) if __name__ =  "__main__": # instantiation of server = Socketserver. Threadingtcpserver (( ' 127.0.0.1 ', 999,), MyServer) # call Serve_forever Method Server.serve_forever ()              

multi_socketserver_client.py

# is a simple TCP clientimport Socketsock = Socket.socket () # Connection server Sock.connect (( Span class= "hljs-string" > ' 127.0.0.1 ', 999,)) login = Sock.recv ( 1024x768) Print (Login.decode ( ' utf-8 ')) while true:message = input ( "Please input the message:"). Strip () if message = =  "exit": Sock.sendall (b ' exit ') break else:sock.sendall (Message.encode ( Utf-8 ')) print (" waitting for recving message ... ") data = SOCK.RECV (1024) print (Data.decode ( ' utf-8 ')) sock.close ()    

Here, we have succeeded in achieving multiple concurrency, what is multiple concurrency? This is related to the process and threads in the operating system, since the network programming is to achieve the communication between the two processes, then can not escape the process, the thread and so on

Python path-socketserver for multiple concurrency

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.