Python Network Programming - TCP Server Multithreading

Overview


The TCP server establishes sockets with multiple clients at the same time, requiring one thread to maintain one client.


Implementation steps



1. Import the socket package
import socket

2. Create a server socket
socket.socket(AddressFamily, Type)
socket.AF_INET represents IPv4 type
SOCK_STREAM means tcp
Need to set port multiplexing to serve multiple clients
tcp_server_socket = socket.socket ( socket.AF_INET , socket.SOCK_STREAM ) _ _ _ _ _ _

#Port multiplexing server exit port immediately release
tcp_server_socket.setsockopt ( socket.SOL_SOCKET , socket.SO_REUSEADDR , True ) _ _ _ _ _ _

3. Bind the port number
tcp_server_socket.bind
The first parameter represents the ip address, generally you do not need to specify any ip that represents the machine
The second parameter is the port number
tcp_server_socket.bind (( ' ' , 9090 ) )

4. Set up monitoring
Indicates the maximum number of connections waiting to be established 128
tcp_server_socket.listen ( 128 ) _ _

5. Wait for the client's connection request
A new socket is returned every time the client and server establish a successful connection
while True :
new_client , ip_port = tcp_server_socket.accept ( ) _
sub_thresd = threading . Thread ( target = handle , args = ( ip_port , new_client ))
sub_thresd.start ( ) _

6. Receive data
Send and receive messages using the newly returned socket
rece_data = new_client.recv ( 1024 ) _ _
if rece_data :
rece_data = rece_data.decode ( ' utf -8' )
print ( "The received data is" , rece_data )

7. Send data
send_content = "Problem in progress"
send_data = send_content.encode ( ' utf -8' )
new_client.send ( send_data ) _ _
new_client.close ( ) _

8. Close the socket
tcp_server_socket.close ( ) _


Code:
import socket
import threading


def handle ( ip_port , new_client ):
print ( "The client's ip and port number is: " , ip_port )
while True :
# 5. Receive data
# Send and receive messages using the newly returned socket
rece_data = new_client.recv ( 1024 ) _ _
if rece_data :
rece_data = rece_data.decode ( ' utf -8' )
print ( "The received data is" , rece_data )
# 6. Send data
send_content = "Problem in progress"
send_data = send_content.encode ( ' utf -8' )
new_client.send ( send_data ) _ _
new_client.close ( ) _
else :
print ( "Client is offline" , ip_port )
break


if __name__ == '__main__' :
# 1. Create a server socket
# socket.AF_INET represents the IPv4 type
# SOCK_STREAM means tcp
tcp_server_socket = socket.socket ( socket.AF_INET , socket.SOCK_STREAM ) _ _ _ _ _ _
tcp_server_socket.setsockopt ( socket.SOL_SOCKET , socket.SO_REUSEADDR , True ) _ _ _ _ _ _
#Port multiplexing server exit port immediately release
#socket.SOL_SOCKET represents the current socket
#socket._RetAddress multiplexing options
#True determines multiplexing
# 2. Bind the port number
# The first parameter indicates the ip address, generally it is not necessary to set the top to indicate any ip of the machine
#The second parameter indicates the port number
tcp_server_socket.bind (( ' ' , 9090 ) )
# 3. Set up monitoring
# 128: Indicates the maximum number of connections waiting to be established
tcp_server_socket.listen ( 128 ) _ _
# 4. Wait for the client's connection request
#Every time the client and server establish a successful connection, a new socket will be returned
while True :
new_client , ip_port = tcp_server_socket.accept ( ) _
sub_thresd = threading . Thread ( target = handle , args = ( ip_port , new_client ))
sub_thresd.start ( ) _


# 7. Close the socket
tcp_server_socket.close ( ) _


Copyright statement: The content of this article is contributed by Alibaba Cloud's real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00