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 联系我们
你好,我是AI助理。
可以解答问题、推荐解决方案等。