<sub id="miph7"></sub>
<table id="miph7"></table>

        1. socketserver 实现TCP并发

          import socketserver # 导入socketsrever模块
          import subprocess # 导入subprocess模块
          import struct          # 导入struct模块
          class Myserver(socketserver.BaseRequestHandler): # 创建类
              def handle(self):
                  while True:    # 通讯循环
                      try:           # 处理异常
                          data = self.request.recv(1024) # 接收信息
                          print("客户端发来信息:%s"%data) # 打印信息
                          if not data:break    # 判断 data 为空则结束循环
                          eer = subprocess.Popen(data.decode("utf-8"),shell=True,  # 创建subprocess对象
                                  stderr = subprocess.PIPE,  # 异常信息
                                  stdout = subprocess.PIPE, # 输出信息
                                  stdin = subprocess.PIPE)  # 输入信息
                          res = eer.stderr.read()  # 读异常信息并赋值给res
                          if res:           # 判断 res为真则执行 否则执行else
                              res_a = res
                          else:
                              res_a = eer.stdout.read()
                          if not res_a: # 判断 res_a 为空则执行
                              res_a = ("执行成功".encode("utf-8"))
                          lenth = len(res_a) # 取res_a的长度并赋值给lenth
                          lenht = struct.pack("i",lenth) # 以int类型打包lenth并赋值给lenht
                          self.request.send(lenht)  # 发送信息
                          self.request.send(res_a) # 发送信息
                      except Exception:     # 处理异常
                              break               # 结束循环
          if __name__ == "__main__":
              s = socketserver.ThreadingTCPServer(("127.0.0.1",8080),Myserver) # 多线程
              # s = socketserver.ForkingTCPServer(("127.0.0.1",8080),Myserver) # 多进程 Linux
              s.serve_forever()
          

           

          import socket # 导入socket模块
          import struct  # 导入struct模块
          a = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 创建socket对象
          a.connect(("127.0.0.1",8080)) # 链接ip和端口
          while True:                               # 通讯循环
              msg = input(">>>:")          # 等待用户输入
              if not msg:continue            # 判断 msg 为空则跳出本次循环,重新循环
              if msg == "quit":break       # 判断 smg == "quit"则结束循环
              a.send(msg.encode("utf-8")) # 发送信息
              print("客户端已发送信息")        # 打印信息
              data = a.recv(4)                  # 接收信息
              lenth = struct.unpack("i",data)[0] # 以int类型解包并赋值给lenth
              res_size = 0       # 设置变量
              res_msg = b""   # 设置变量
              while res_size < lenth: # res_size 小于 lenth 则循环
                  res_msg += a.recv(1024) # res_msg = res_msg + a.recv(1024)
                  res_size = len(res_msg)    # res_size = res_msg的长度
              print("服务端发来信息:%s"%res_msg.decode("utf-8")) # 打印信息 Linux "utf-8"  wid "gbk"
          a.close() # 关闭链接
          
          相关文章
          相关标签/搜索
          二四六天天好彩免费资枓大全