zoukankan      html  css  js  c++  java
  • 并发编程

    并发编程

    可以显示多个客户端,同时连接服务端。客户端能同时发送消息给服务器

    客户端-1
    import  socket
    import struct
    import time
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    
    addr = ('127.0.0.1',8000)
    
    client.connect(addr)
    
    while True:
        # 发送消息
        # msg = input("请输入你要讲的话>>>>")
        time.sleep(2)
        msg = 'dir'
        print(msg)
        client.send(bytes(msg.encode('utf8')))
    
        leng = client.recv(4)
        date_lens = struct.unpack('i', leng)[0]
        # 接受消息
        leng = client.recv(date_lens)
        print(leng.decode('gbk'))
    
    客户端-2
    import  socket
    import struct
    import time
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    
    addr = ('127.0.0.1',8000)
    
    client.connect(addr)
    
    while True:
        # 发送消息
        # msg = input("请输入你要讲的话>>>>")
        time.sleep(2)
        msg = 'dir'
        print(msg)
        client.send(bytes(msg.encode('utf8')))
    
        leng = client.recv(4)
        date_lens = struct.unpack('i', leng)[0]
        # 接受消息
        leng = client.recv(date_lens)
        print(leng.decode('gbk'))
    
    客户端-3
    import  socket
    import struct
    import time
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    
    addr = ('127.0.0.1',8000)
    
    client.connect(addr)
    
    while True:
        # 发送消息
        # msg = input("请输入你要讲的话>>>>")
        time.sleep(2)
        msg = 'dir'
        print(msg)
        client.send(bytes(msg.encode('utf8')))
    
        leng = client.recv(4)
        date_lens = struct.unpack('i', leng)[0]
        # 接受消息
        leng = client.recv(date_lens)
        print(leng.decode('gbk'))
    
    客户端-4
    import  socket
    import struct
    import time
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    
    addr = ('127.0.0.1',8000)
    
    client.connect(addr)
    
    while True:
        # 发送消息
        # msg = input("请输入你要讲的话>>>>")
        time.sleep(2)
        msg = 'dir'
        print(msg)
        client.send(bytes(msg.encode('utf8')))
    
        leng = client.recv(4)
        date_lens = struct.unpack('i', leng)[0]
        # 接受消息
        leng = client.recv(date_lens)
        print(leng.decode('gbk'))
    
    服务端
    import  socketserver
    import subprocess
    import struct
    
    class MyHandler(socketserver.BaseRequestHandler):	# 重写方法
        # 通信循环
        def handle(self):
            while True:
                # 接受消息
                client_msg = self.request.recv(1024)
                common = client_msg.decode('utf8')
                print(common)
                obj = subprocess.Popen(common,
                                       shell=True,
                                       stderr=subprocess.PIPE,
                                       stdout=subprocess.PIPE
                                       )
                stdout = obj.stdout.read()	# 拿到正常输出的信息
                stderr = obj.stderr.read()	# 拿到错误输出的信息
    
                msg_len = len(stderr) + len(stdout)	# 计算传输数据的长度
                one = struct.pack('i', msg_len)	#  转化
                print(one)
                self.request.send(one)
                self.request.send(stdout + stderr)
    
    
    if __name__ == '__main__':
        addr = ('127.0.0.1',8000)
        service = socketserver.ThreadingTCPServer(addr,MyHandler,bind_and_activate=True)
        service.serve_forever()
    

    当你运行服务端后,依次运行对应客户端文件,你会发现此时就是并发在处理数据。

  • 相关阅读:
    winget
    splunk单节点容器部署
    jumpserver容器化部署
    思科acl
    Java springboot-plus
    接口 Postman 上传图片测试
    EF 数据迁移 新
    电商 抓取淘宝分类 包含图片和名称
    思维导图 淘宝上新流程
    功能模块 上传视频 生成视频预览图
  • 原文地址:https://www.cnblogs.com/plf-Jack/p/11105245.html
Copyright © 2011-2022 走看看