• socketserver实现并发通讯


    socketserver实现并发通讯

    #server端
    import socketserver
    
    class Myserver(socketserver.BaseRequestHandler):
        def handle(self): #自动的触发了handle方法,并且sele.request=conn,handle不能变,一直是这个
            while True:
                msg = self.request.recv(1024).decode("utf-8")
                self.request.send(msg.upper().encode("utf-8"))
                print(msg)
    
    server = socketserver.ThreadingTCPServer(("127.0.0.1",9000),Myserver)
    server.serve_forever()
    
    #client端
    import socket
    sk = socket.socket()
    sk.connect(("127.0.0.1",9000))
    while True:
        sk.send(b"hello")
        msg =sk.recv(1024)
        print(msg)
    
    
    sk.close
    
    #client1端
    import socket
    sk = socket.socket()
    sk.connect(("127.0.0.1",9000))
    while True:
        sk.send(b"hello.mo")
        msg =sk.recv(1024)
        print(msg)
    
    
    sk.close
    
    #client2端
    import socket
    sk = socket.socket()
    sk.connect(("127.0.0.1",9000))
    while True:
        sk.send(b"hello.yang")
        msg =sk.recv(1024)
        print(msg)
    
    
    sk.close
    
    记录学习的点点滴滴
  • 相关阅读:
    BZOJ1477 青蛙的约会
    Code Style
    线段树合并
    动态开点
    主席树
    启发式合并
    树的重心
    树的直径
    扩展欧几里得
    裴蜀定理
  • 原文地址:https://www.cnblogs.com/yangzilaing/p/14868973.html
Copyright © 2020-2023  润新知