Python網路編程 - TCP伺服器多線程

相關標籤:1.Empower Your Web with Python
2. Shodan and Python API

概述


TCP 伺服器同時與多個用戶端建立通訊端串連,需要為每個用戶端分配一個線程來維護串連。

實現步驟



1. 匯入 socket 包
import socket

2. 建立伺服器通訊端
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. 綁定連接埠號碼
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. 設定監聽
Indicates the maximum number of connections waiting to be established 128
tcp_server_socket.listen ( 128 ) _ _

5. 等待用戶端的串連請求
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. 接收資料
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_content = "Problem in progress"
send_data = send_content.encode ( ' utf -8' )
new_client.send ( send_data ) _ _
new_client.close ( ) _

8. 關閉通訊端
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 ( ) _


著作權聲明:本文內容由阿里雲實名註冊使用者自發貢獻,著作權歸原作者所有。阿里雲開發人員社區不擁有著作權,不承擔相應的法律責任。具體規則請查看《阿里雲開發人員社區使用者服務協議》和《阿里雲開發人員社區智慧財產權保護指引》。如果您發現本社區中有涉嫌抄襲的內容,請填寫侵權投訴表單進行舉報。一經查實,本社區將立刻刪除涉嫌侵權內容。

相關文章

探索更多特惠

  1. 簡訊服務(SMS) & 郵件服務

    50000封電子郵件套餐包低至1.99美元,120條短訊息僅需1美元

phone 聯絡我們
Hi, I'm Alibaba Cloud AI Assistant!
I can help with questions and solutions.