Python32 Socket Server

Source: Internet
Author: User

Type
    • There are several types of socketserver:

      Class Socketserver. TCPServer: For TCP
      Class Socketserver. Udpserver: For UDP
      Class Socketserver. Unixstreamserver: TCP for UNIX
      Class Socketserver. Unixdatagramserver: UDP for UNIX


TCPServer inherited Baseserver.
Unixstreamserver inherited TCPServer.

    • Creating a socket server is at least the following steps:

      1, must create a request processing class, and this class to inherit Baserequesthandler, but also to rewrite the parent class handle method (all interaction with the client is written in handle).
      2. You must instantiate a protocol server (such as TCPServer) and pass the server IP and the request processing class you created above to this tcpserver (the IP and request processing classes are passed to TCPServer when instantiating).
      3. The request can then be processed according to the above instance:
      Server.handle_request () #只处理一个请求
      Server.handle_forever () #处理多个请求, always executed (usually with this)
      4. Call Server_close () to close

Server side: Import Socketserverclass Mytcphandler (socketserver. Baserequesthandler): ' 1th step: Create a processing class, inherit base. Each request from the client instantiates the class ' Def handle (self): ' 1th step: Override the handle method, handle the default existence of the code in the parent class is empty.            All client interactions are completed in handle ' while True: #使其可以循环发送数据 self.data = Self.request.recv (1024x768). Decode (). Strip () #这里的self. REQUEST.RECV corresponds to the previously used CONN.RECV print ("{} wrote:". Format (self.client_address[0)) #打印客户            End IP Address information print (self.data) #打印数据信息 self.request.send (Self.data.upper (). Encode (' Utf-8 '))    #传回数据给客户端, just upper a bit #sendall就是重复调用sendif __name__ = = "__main__": Host,port = "localhost", 9999 Server = Socketserver. TCPServer ((host,port), Mytcphandler) #第2步: Instantiate the TCPServer and pass the IP and Mytcphandler as parameters to the request processing class #监听客户端的每一个请求,    The Mytcphandler class is instantiated, with Mytcphandler handle interacting with the client. Server.serve_forever () #第3步: Allows multiple requests to be processed forever Client side: Import socketclient = Socket.socket () client.connect (' localhost ', 9999)) While true:msg = InPUT (">>:"). Strip () If Len (msg) = = 0:continue Client.send (msg.encode (' Utf-8 ')) data = CLIENT.RECV (1024x768) PR Int ("recv:", Data.decode ()) Client.close () client execution result: >>:ABCRECV:ABC>>:EFGRECV:EFG>>:HHHRECV: Hhh>>:server execution Result: 127.0.0.1 wrote:abc127.0.0.1 wrote:efg127.0.0.1 wrote: Server error after Hhhclient disconnect:----------------------------------------Exception happened during processing of request from (' 127.0.0.1 ', 53933) Traceback (most recent): File "D:\python37\lib\socketserver.py", line 313, in _handle_request _noblock self.process_request (Request, client_address) File "D:\python37\lib\socketserver.py", line 344, in Process_re     Quest Self.finish_request (Request, client_address) File "D:\python37\lib\socketserver.py", line 357, in Finish_request Self. Requesthandlerclass (Request, client_address, self) The File "D:\python37\lib\socketserver.py", line 712, and __init__ self. Handle () File "e:/python/code exercise/a2.py", line one, in handle self.data = SelF.REQUEST.RECV (1024x768). Decode (). Strip () Connectionreseterror: [Winerror 10054] The remote host forced the shutdown of an existing connection. ----------------------------------------#可以看到这里报错ConnectionResetError: [Winerror 10054]
修改server端:import socketserverclass MyTCPHandler(socketserver.BaseRequestHandler):    def handle(self):        while True: #使其可以循环发送数据           try: #使用try的话就不需要使用 if not data的方式来判断客户端断开时,无数据的情况了                self.data = self.request.recv(1024).decode().strip()                #这里的self.request.recv相当于之前用的conn.recv                print ("{} wrote:".format(self.client_address[0]))                print (self.data)                self.request.send(self.data.upper().encode(‘utf-8‘))           except ConnectionResetError as e:               print ("err:",e)               break    #这里一定要break,不然就会一直死循环if __name__ == "__main__":    HOST,PORT = "localhost",9999    server = socketserver.TCPServer((HOST,PORT),MyTCPHandler)    server.serve_forever()server执行结果:127.0.0.1 wrote:abc127.0.0.1 wrote:123err: [WinError 10054] 远程主机强迫关闭了一个现有的连接。#客户端断开连接后,就通过断言的方式抓到错误了。

The above code currently does not support multiple concurrency, if there are multiple concurrency, the subsequent concurrency will be suspended, if you want to concurrency, you need to modify the code.


You can see that TCPServer is inherited baseserver

by using the Ctrl dot TCPServer

  Modify the server side: Import Socketserverclass Mytcphandler (socketserver. Baserequesthandler): def handle (self): when True: #使其可以循环发送数据 try: #使用try的话就不需要使用 if not data to determine the client When the end is disconnected, no data is Self.data = SELF.REQUEST.RECV (1024x768). Decode (). Strip () print ("{} wrote:". Format ( Self.client_address[0]) print (self.data) self.request.send (Self.data.upper (). Encode (' Utf-8 ') except Connectionreseterror as E:print ("ERR:", e) break if __name__ = = "__ma in__ ": Host,port =" localhost ", 9999 server = Socketserver. Threadingtcpserver ((host,port), Mytcphandler) # The above code needs to use Threadingtcpserver Server.serve_forever () Client 1 execution result:> >:test 1recv:test 1>>:client 2 execution Result: >>:test 2recv:test 2>>:client 3 execution Result: >>:test 3recv:test 3 >>:server execution Result: 127.0.0.1 wrote:test 1127.0.0.1 wrote:test 2127.0.0.1 wrote:test 3  


You can see that the server side now supports multiple concurrency and is not suspended, and each request opens a new thread that interacts with the server, and each thread is independent, with 10 threads, and can do 10 things.

Threadingtcpserver by Ctrl Point

You can see that the TCPServer class is passed in.
At the same time, Threadingmixin;tcpserver is responsible for interacting with clients, and multithreading is implemented by ThreadingMixIn.

Ctrl DOT ThreadingMixIn

This part is the main code of multithreading

Python32 Socket Server

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.