Python multi-thread socket programming many client access, pythonsocket
In Python, the server that implements socket communication is complicated, and the client is very simple. Therefore, the client is basically implemented using the sockct module, and the service
There are many modules available at the end, as shown below:
1. Client
#! /Usr/bin/env python # coding: UTF-8 ''' file: client. pydate: 9/9/17 PMauthor: lockeyemail: lockey@123.comdesc: socket programming client, python3.6.2 ''' import socket, sysHOST = '192. 168.1.6 'port = 8998 ADDR = (HOST, PORT) BUFSIZE = 1024 sock = socket. socket () try: sock. connect (ADDR) print ('have connected with Server') while True: data = input ('lockey # ') if len (data)> 0: print ('send :', data) sock. sendall (data. encode ('utf-8') # Do not use send () recv_data = sock. recv (BUFSIZE) print ('receive: ', recv_data.decode ('utf-8') else: sock. close () breakfailed t Exception: print ('error') sock. close () sys. exit ()
2. SocketServer Module
To allow multiple clients to access services and communicate with each other at the same time, the server uses the SocketServer module. In this way, you do not need to use the parent process to process the sub-thread after it ends, you don't need to worry about socket shutdown. Everything is done by SocketServer.
#! /Usr/bin/env python # coding: UTF-8 ''' file: client. pydate: 9/9/17 PMauthor: lockeyemail: lockey@123.comdesc: socket programming server side, comment ''' from socketserver import BaseRequestHandler, ThreadingTCPServerimport threadingBUF_SIZE = 1024 class Handler (BaseRequestHandler ): def handle (self): address, pid = self. client_address print ('% s connected! '% Address) while True: data = self. request. recv (BUF_SIZE) if len (data)> 0: print ('receive = ', data. decode ('utf-8') cur_thread = threading. current_thread () # response = '{}:{}'. format (cur_thread.ident, data) self. request. sendall ('response '. encode ('utf-8') print ('send: ', 'response') else: print ('close ') breakif _ name _ = '_ main _': HOST = '2017. 168.1.6 'port = 8998 ADDR = (HOST, PORT) server = ThreadingTCPServer (ADDR, Handler) # The parameter is the print ('listening') server of the listening address and the processing class that has established a connection. serve_forever () # listener. After a TCP connection is established, a new socket and thread are created for the connection, and print (server) is processed by the handle method in the processing class)
In the following test, I used two interruptions on the other host and the local machine to connect to the server. Both of them can be normally connected and can perform data response interaction.
Note: The running platform of this program is rhel7.2.x86 _ 64, and the python version is 3.6.2, which may not be compatible with users of windows and non-same python versions, if you are a user of another platform, please correct it yourself !!
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.