Python socket multi-thread communication instance analysis (chat room), pythonsocket

Source: Internet
Author: User

Python socket multi-thread communication instance analysis (chat room), pythonsocket

This article describes the multi-thread communication method of python socket. We will share this with you for your reference. The details are as follows:

#! /Usr/bin/evn python "This is an example of Socket + multi-process (Chat Server) "import socketimport threading # encoding =" GBK "def HKServer (client, addr) for processing Chinese data ): "the real-time communication function is added to encoding to process the input of Chinese data. client customer socket addr customer address" "# notify existing customers, A new member is added to for c in clients: c. send (bytes ("[% s] add \ r \ n" % addr [1], encoding) # accept client data say = B "" while True: data = client. recv (1024) if not data: break # if it is not the Enter key, if data! = B '\ r \ N': say + = data #. encode (encoding) continue # Send the content sent from the client to all clients for c in clients: c. send (bytes ("[% s]: % s \ r \ n" % (addr [1], say. decode (encoding), encoding) # content is \ x0 say = B "# After the customer leaves, move the current customer from the customer list and close the socket connection to clients. remove (client) client. close () # notify each existing customer that a member has left for c in clients: c. send (bytes ("[% s] Quit \ r \ n" % addr [1], encoding )) # client list clients = [] # set IP address and port host = ''PORT = 9999 # initialize sockets = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # bind the IP address and port s. bind (HOST, PORT) # Start listening to s. listen (1) # loop wait while True: # accept client, addr = s. accept () # Start a new process to communicate with the customer thread = threading. thread (target = HKServer, args = (client, addr) thread. start () # record new customer clients. append (client)

I hope this article will help you with Python programming.

Articles you may be interested in:
  • Simple Example of Python multi-thread Crawler
  • Python implements simple multi-thread task queue
  • Comparison of Python multi-thread image capturing efficiency
  • Python multi-thread, asynchronous, and multi-process crawler implementation code
  • Example of trying to capture the IP address of the proxy server using Python multi-thread
  • Multi-thread web crawler using python
  • Using Python to capture pictures
  • How to download Baidu music using Python multi-thread and queue
  • Fast multi-thread ping using Python
  • Python multi-thread File Download Method
  • A simple multi-thread TCP server tutorial using Python
  • Analysis of variable problems in multiple threads of Python
  • Example of queue operation for multi-thread programming in Python3
  • Python Socket network programming
  • How does python query whois through socket?
  • How to handle socket remote connection errors in python

Related Article

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.