python多线程实现tcp socket
服务器代码
#!/usr/bin/env python3
# coding=utf-8
from socket import *
from threading import Thread
class TcpServer(object):
"""Tcp服务器"""
def __init__(self, Port):
"""初始化对象"""
self.code_mode = "utf-8" #收发数据编码/解码格式
self.server_socket = socket(AF_INET, SOCK_STREAM) #创建socket
self.server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, True) #设置端口复用
self.server_socket.bind(("", Port)) #绑定IP和Port
self.server_socket.listen(100) #设置为被动socket
print("Listen...")
def run(self):
"""运行"""
while True:
client_socket, client_addr = self.server_socket.accept() #等待客户端连接
print("{} online".format(client_addr))
tr = Thread(target=self.recv_data, args=(client_socket, client_addr)) #创建线程为客户端服务
tr.start() #开启线程
self.server_socket.close()
def recv_data(self, client_socket, client_addr):
"""收发数据"""
while True:
data = client_socket.recv(1024).decode(self.code_mode)
if data:
print("{}:{}".format(client_addr, data))
client_socket.send(data.encode(self.code_mode))
else: #客户端断开连接
print("{} offline".format(client_addr))
break
client_socket.close()
def main():
print("\033c", end="") #清屏
port = int(input("请输入要绑定的Port:"))
my_server = TcpServer(port)
my_server.run()
if __name__ == "__main__":
main()
客户端代码
#!/usr/bin/env python3
# coding=utf-8
from socket import *
from threading import Thread
class TcpClient(object):
"""Tcp客户端"""
def __init__(self, IP="127.0.0.1", Port=8888):
"""初始化对象"""
self.code_mode = "utf-8" #收发数据编码/解码格式
self.IP = IP
self.Port = Port
self.my_socket = socket(AF_INET, SOCK_STREAM) #创建socket
def run(self):
"""启动"""
self.my_socket.connect((self.IP, self.Port)) #连接服务器
tr = Thread(target=self.recv_data) #创建线程收数据
ts = Thread(target=self.send_data) #创建线程发数据
tr.start() #开启线程
ts.start()
def recv_data(self):
"""收数据"""
while True:
data = self.my_socket.recv(1024).decode(self.code_mode)
if data:
print("\r>>{}\n<<".format(data), end="")
else:
break
self.my_socket.close()
def send_data(self):
"""发数据"""
while True:
data = input("<<")
self.my_socket.send(data.encode(self.code_mode))
def main():
print("\033c", end="") #清屏
ip = input("请输入服务器IP:")
port = int(input("请输入服务器Port:"))
my_socket = TcpClient(ip, port)
my_socket.run()
if __name__ == "__main__":
main()