Python Growth Notes-Basics (ix)

Source: Internet
Author: User

Create a socketserver at least in the following steps:
    1. First, you must create a request handler class by subclassing the Baserequesthandlerclass and overriding its handle () meth Od This method would process incoming requests.
    2. Second, you must instantiate one of the server classes, passing it the server ' s address and the request handler class.
    3. Then call the Handle_request () Orserve_forever () method of the server object to process one or many requests.
    4. Finally, call Server_close () to close the socket.
 1, create a request processing class yourself, 2, instantiate a TCP server yourself, and pass the server IP and the request processing class you created above 3,    import socketserver  Class Mytcphandler (Socketserver. Baserequesthandler): "" "The request handler class for we server. it is instantiated once per connection to the server , and MustOverride the handle () method to implement communication to theclient. "" "  def handle (self): # Self.request was the TCP socket connected to the clientwhile True:try:self.data = Self.request.rec V (1024x768). Strip () print ("{} wrote:". Format (Self.client_address[0])) print (Self.data) self.request.send ( Self.data.upper ()) except Connectionabortederror as E:print ("Err", e) break if __name__ = = "__main__": HOST, PORT = " LocalHost ", 1111 # Create the server, binding to localhost on port 9999server = Socketserver. Threadingtcpserver (HOST, PORT), Mytcphandler) Server.serve_forever ()      Threadingtcpserver supports multithreaded      4, individual dictionaries per user 5, running  7 on Linux, MOD5 authentication 8, percent 9, Breakpoint Continuation: The file size (bytes) in the temporary file is paused, continuedWhen reading temporary files  

Python Growth Notes-Basics (ix)

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.