并发编程

并发编程

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

客户端-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()

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

posted @ 2019-06-28 23:21  Hello_Jack  阅读(139)  评论(0编辑  收藏  举报
# 页脚html代码 /*头部导航栏*/ #navigator { font-size:15px; border-bottom: 1px solid #ededed; border-top: 1px solid #ededed; height: 60px;/*导航栏高度,原始50*/ clear: both; margin-top: 25px; } /*导航栏设置,可以自定义导航栏的目录*/ #navList { min-height: 35px; float: left; } #navList li { /*每一个栏目节点*/ float: left; margin: 0 5px 0 0; /*这里原来是0 40px 0 0 */ } #navList a { /*栏目文字的格式*/ display: block; width: 5em; height: 22px; float: left; text-align: center; padding-top: 19px; }