Python socket (multi-threaded)

Source: Internet
Author: User

1 server-side code2 3 ImportSocketserver4 classMytcphandler (socketserver.baserequesthandler):5     """6 The RequestHandler class for socket server.7 It is instantiated once per connection being established (request from client) to the Socker server.8 And must is override the handler () method to implement communication to the client.9     """TenConnection_account =0 One     defHandle (self):#Redefine method ' Handl () ' in parent class AMytcphandler.connection_account + = 1 -         Print("The %d client connected!"% Mytcphandler.connection_account)#How to statistic Total No. -         #Self.request is the TCP socket of the connected to the client the          while1: -Self.data = SELF.REQUEST.RECV (1024). Strip () -             #If connected client dead, exit loop -             if  notSelf.data: +                  Break -             Print("Client:%s \ n" +                    "Send:%s") %(Self.client_address[0],self.data) ASelf.request.sendall ("GOT IT!") at  - if __name__=="__main__": -Host,port ="localhost", 8888 -     #creating the socket server and binding to HOST::P ort = = "localhost":: 8888 -Server =Socketserver.threadingtcpserver ((host,port), Mytcphandler) -     #keeping server running until interrupt (Ctrl + C) inServer.serve_forever (poll_interval=0.5)

client-side code with single thread

Reference Blog, http://www.cnblogs.com/zzyzz/p/5581503.html

Python socket (multi-threaded)

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.