Python simple multi-threaded Chat

Source: Internet
Author: User

  # Client-side Import Socketip_port = (' 127.0.0.1 ', 8091) SK = Socket.socket () sk.connect (ip_port) print ("Clients start:") While TRUE:INP = input (">>>") Sk.sendall (bytes (INP, "UTF8")) if InP = = ' exit ': Break Server_r Esponse = SK.RECV (1024x768) print (str (server_response, "UTF8")) Sk.close () # Server-side import Socketserverclass MyServer (Socke Tserver.            Baserequesthandler): def handle (self): print ("Server startup ...") while true:conn = Self.request Print (self.client_address) while true:client_data = Conn.recv (1024x768) print (                STR (client_data, "UTF8")) print ("Waiting ...") Server_response = input (">>>") Conn.sendall (Bytes (server_response, "UTF8")) conn.close () if __name__ = = ' __main__ ': Server = socket Server. Threadingtcpserver ((' 127.0.0.1 ', 8091), MyServer) Server.serve_forever () # This will execute the handle method, so the handle method is to write the program logic. 
Create a socketserver at least one of the following steps

First, you must create a request handler class by subclassing the Baserequesthandlerclass and overriding its handle () meth Od This method would process incoming requests.
  
Second, you must instantiate one of the server classes, passing it the server ' s address and the request handler class.

Then call the Handle_request () or Serve_forever () method of the server object to process one or many requests.

Finally, call Server_close () to close the socket.

Python simple multi-threaded Chat

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.