温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python TCPServer 多线程多客户端通信的实现

发布时间:2020-10-01 07:17:42 来源:脚本之家 阅读:169 作者:东南飘雪 栏目:开发技术

最简单、原始的TCP通信demo

服务端Http请求:

import socket

# 创建一个servicesocke
serviceSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 给服务器绑定地址(ip地址,端口号)
serviceSocket.bind(("192.168.171.1", 80))

print("等待客户端接入")
# sock 是客户端的socket信息
# addr 是客户端的地址(ip,端口)
sock, addr = serviceSocket.accept()
print(f"sock from client:{sock}")
print(f"addr of client:{addr}")

while True:
  # 接收客户端的请求
  recvData = sock.recv(1024)
  print("客户端说:%s" % (recvData.decode("utf-8")))
  sendData = input("服务器说:")
  # 发送(回复)数据给客户端
  sock.send(sendData.encode("utf-8"))

客户端Http请求:

import socket

# 创建客户端socket
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 连接服务器
clientSocket.connect(("192.168.171.1", 80))

while True:
  # 发送消息给服务器
  sendData = input("客户端说:")
  if sendData == "bye":
    clientSocket.send(sendData.encode("utf-8")) # 编码:将数据装换成二进制形式
    break
  clientSocket.send(sendData.encode("utf-8"))
  recvData = clientSocket.recv(1024)
  print("服务器说:%s" % (recvData.decode("utf-8"))) # 解码:将二进制转换成字符

1、在TCP中,客户端的实现流程:

创建客户端的socket对象建立与服务器之间的联系发送请求接收数据关闭连接

2、服务端的实现流程:

创建服务端的socket对象绑定服务端的地址设置监听器等待客户端的连接接收客户端的请求返回处理的结果到客户端

ThreadingTCPServer 多线程多客户端通信自动重连demo

socketserver继承图:

Python TCPServer 多线程多客户端通信的实现

TCPServer

# from socketserver import TCPServer, BaseRequestHandler, ThreadingTCPServer
from socketserver import TCPServer, StreamRequestHandler, ThreadingMixIn
import traceback

# class MyBaseRequestHandler(BaseRequestHandler):
class MyBaseRequestHandler(StreamRequestHandler):

  def handle(self):
    self.addr = self.request.getpeername()
    self.server.users[self.addr[1]] = self.request
    message = "IP " + self.addr[0] + ":" + str(self.addr[1]) + " Connected..."
    print(message)

    while True:
      try:
        data = self.request.recv(1024).decode('UTF-8', 'ignore').strip()
        print(f'receive from {self.client_address}:{data}')

        back_data = (f"response\"" + data + "\":\n").encode("utf8")
        self.request.sendall(back_data)
      except:
        traceback.print_exc()
        break

# 源码:class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
# 从ThreadingMixIn和TCPServer继承,实现多线程
class MyThreadingTCPServer(ThreadingMixIn, TCPServer):
  def __init__(self, server_address, RequestHandlerClass):
    TCPServer.__init__(self, server_address, RequestHandlerClass)
    self.users = {}


class MyTCPserver():
  def __init__(self, server_addr='192.168.1.109', server_port=23):
    self.server_address = server_addr
    self.server_port = server_port
    self.server_tuple = (self.server_address, self.server_port)

  def run(self):
    # server = TCPServer(self.server_tuple, MyBaseRequestHandler)
    server = MyThreadingTCPServer(self.server_tuple, MyBaseRequestHandler)
    server.serve_forever()


if __name__ == '__main__':
  myserver = MyTCPserver()
  myserver.run()

在telnet 下开启开启两个客户端,本电脑的IP为192.168.1.109,开两个客户端后,TCPServer的终端出现同一个IP但是不同端口的连接:

Python TCPServer 多线程多客户端通信的实现

TCPClient

import socket
import time

class MyClient:
  host = '192.168.1.109'
  port = 23
  bufsiz = 1024
  addr = None
  skt = None

  def __init__(self, host=None, port=None):
    if host != None:
      self.host = host

    if port != None:
      self.port = port

    if self.addr == None:
      self.addr = (self.host, self.port)

    self.doConnection()

  def doConnection(self):
    try:
      self.skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      self.skt.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
      print(self.addr)
      self.skt.connect(self.addr)
    except:
      pass

  def run(self):
    while True:
      try:
        _time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        self.skt.sendall(f'{_time}:i am clent1 '.encode('utf-8'))
        data = self.skt.recv(self.bufsiz)
        print(data.decode('utf-8', 'ignore'))
        if not data:
          break
        print(data.strip())
        time.sleep(5)
      except socket.error:
        print('socket error, reconnection') # 自动重连
        time.sleep(3)
        self.doConnection()
      except:
        print('other error')

    self.skt.close()


myclient = MyClient()
myclient.run()

上面用的是telnet工具来作为客户端,这里是用代码实现模拟的客户端。

Python TCPServer 多线程多客户端通信的实现

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI