zoukankan      html  css  js  c++  java
  • 处理并发问题socketserver

    socketserver

    server 服务端

    import socketserver

    class MyServer(socketserver.BaseRequestHandler):
      def handle(self): # self.request 相当于 conn
        while True:
          msg = self.request.recv(1024).decode('utf-8')
          if msg == 'q':
            self.request.close()
            break
          print(msg)
          info = input('>>>')
          self.request.send(info.encode('utf-8'))
        
    if __name__ == '__main__':
      # 实例化
      server = socketserver.ThreadingTCPServer(('127.0.0.1', 8080), MyServer)
      # 一直启动
      server.serve_forever()


    client1 客户端

    import socket
    sk = socket.socket()
    sk.connect(('127.0.0.1', 8080))
    while True:
      msg = input('>>>')
      if msg == 'q':
        break
      sk.send(msg.encode('utf-8'))
      ret = sk.recv(1024).decode('utf-8')
      print(ret)
    sk.close()


    client2

    import socket
    sk = socket.socket()
    sk.connect(('127.0.0.1', 8080))
    while True:
      msg = input('>>>')
      if msg == 'q':
        break
      sk.send(msg.encode('utf-8'))
      ret = sk.recv(1024).decode('utf-8')
      print(ret)
    sk.close()
  • 相关阅读:
    springcloud随便写点
    简单的数组排序
    简单wait(),notify()方法
    热词
    mysql日期函数
    springboot和jsp,以及引入jquery
    用户,权限,角色的关系
    通过字节码获取到的方法
    数据库并发事物
    在spring的业务层获取request,response
  • 原文地址:https://www.cnblogs.com/aqiuboke/p/10406198.html
Copyright © 2011-2022 走看看