The socketserver of Python

Source: Internet
Author: User

ImportThreading fromSocketserverImportThreadingtcpserver,baserequesthandlerImportSYSImportLoggingformat='% (asctime) s% (threadname) d% (thread) d% (message) s'Logging.basicconfig ( level=logging.info,format=FORMAT)classChathandler (Baserequesthandler):#One to one, equivalent to the receive functionClients = {}    defSetup (self): self.event=Threading. Event () self.clients[self.client_address]=self.requestdefFinish (self): Self.clients.pop (self.client_address) self.event.set ()defhandle (self):Print(self.request)#New_socket used to recv         while  notSelf.event.is_set (): Data= SELF.REQUEST.RECV (1024)            Print(data,self.client_address) msg='{}.ack'. Format (data). Encode ()#How to implement a one-to-many, more where, how to get             forCinchself.clients.values (): C.send (msg) Addr= ('0.0.0.0', 9999) Server= Threadingtcpserver (Addr,chathandler)#equivalent to TCP total socketPrint(server) T= Threading. Thread (target=server.serve_forever,name='Chatserver') T.start ()Try:     whileTrue:cmd= Input (">>>")        ifCmd.strip () = ='quit':             BreakexceptException as E:Print(e)exceptKeyboardinterrupt:Passfinally:    Print('Exit') sys.exit (0)

The socketserver of Python

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.