zoukankan      html  css  js  c++  java
  • 039.Python使用TCP实现多用户并发


    使用TCP实现多用户并发

    在前面的实验中,TCP建立连接时,只能允许一个用户连接,当第二个用户建立连接时,发送的信息,服务端是没有办法接受,只有当第一个用户退出时,才能接受到第二个用户的请求,并实现通信

    但是UDP可以实现多用户

    1 UDP的多并发

    服务端

    复制代码
    #服务端
    import socket
    
    #创建对象  socket.SOCK_DGRAM 代表UDP协议
    sk = socket.socket(type=socket.SOCK_DGRAM)
    
    #在网络中注册该主机
    sk.bind ( ("127.0.0.1",9000) )
    
    #udp服务器,第一次启动时,一定是先接收数据,在发送数据
    while True:
            msg,cli_addr = sk.recvfrom(1024)
            print (msg.decode("utf-8"))
            print (cli_addr)
            #发送消息
            message = input("Please input your vaule>>>:")
            sk.sendto(message.encode("utf-8"),cli_addr)
    #关闭udp连接
    sk.close()
    复制代码

    客户端

    复制代码
    import socket
    sk = socket.socket(type=socket.SOCK_DGRAM)
    
    #sendto (要发送的消息,(IP地址,端口号))
    while True:
            message = input("Please input the value:")
            sk.sendto (message.encode("utf-8"),("127.0.0.1",9000) )
            #接收数据
            msg,ser_addr = sk.recvfrom(1024)
            print (msg.decode("utf-8"))
    #关闭连接
    sk.close()
    复制代码

    一个client建立连接

    复制代码
    [root@node10 tcp]# python3 server.py
    Hi,I am the first client
    ('127.0.0.1', 34937)
    Please input your vaule>>>:Hi first client
    [root@node10 tcp]# python3 client.py
    Please input the value:Hi,I am the first client
    Hi first client
    复制代码

    再使用一个client连接

    三个之间进行通信

    复制代码
    服务端
    [root@node10 tcp]# python3 server.py
    Hi,I am the first client
    ('127.0.0.1', 44633)
    Please input your vaule>>>:Hi first client
    Hi
    ('127.0.0.1', 44633)
    Please input your vaule>>>:Hi
    Hi,I am the first client2
    ('127.0.0.1', 56953)
    Please input your vaule>>>:Hi,welcome,client 2
    thank you
    ('127.0.0.1', 56953)
    Please input your vaule>>>:yes
    OK
    ('127.0.0.1', 44633)
    Please input your vaule>>>:ok
    第一个clent
    
    [root@node10 tcp]# python3 client.py
    Please input the value:Hi,I am the first client
    Hi first client
    Please input the value:Hi
    Hi
    Please input the value:OK
    ok
    
    第二个client
    [root@node10 tcp]# python3 client.py
    Please input the value:Hi,I am the first client2
    Hi,welcome,client 2
    Please input the value:thank you
    yes
    复制代码

    2 调用sockerserver模块

    服务端

    复制代码
    #服务端
    import socketserver
    import socket
    class MyServer(socketserver.BaseRequestHandler):
            def handle(self):
                    #自定义逻辑
                    print ("This is tcp funaction")
    
    #生成一个对象
    server = socketserver.ThreadingTCPServer( ("127.0.0.1",9000),MyServer )
    
    #循环调用
    server.serve_forever()
    复制代码

    客户端

    import socket
    sk = socket.socket()
    sk.connect( ("127.0.0.1",9000) )
    sk.close()

    执行

    [root@node10 tcp]# python3 server.py
    This is tcp funaction
                                           #这里一直阻塞,等待客户端连接
    [root@node10 tcp]# python3 client.py

    因为客户端没有循环,执行就直接退出,无法看出并发

    3 BaseRequestHandle的底层逻辑

    服务端

    复制代码
    #服务端
    import socketserver
    import socket
    class MyServer(socketserver.BaseRequestHandler):
            def handle(self):
                    #自定义逻辑
                    print ("This is tcp funaction")
                    print (self.request)
                    print (self.client_address)
    
    #生成一个对象
    server = socketserver.ThreadingTCPServer( ("127.0.0.1",9000),MyServer )
    
    #循环调用
    server.serve_forever()
    复制代码

    执行

    复制代码
    This is tcp funaction
    <socket.socket fd=4, 
    family=AddressFamily.AF_INET,
    type=SocketKind.SOCK_STREAM,
    proto=0,
    laddr=('127.0.0.1', 9000),
    raddr=('127.0.0.1', 44428)> ('127.0.0.1', 44428)
    复制代码

    4 添加循环不退出

    服务端

    复制代码
    #服务端
    import socketserver
    import socket
    class MyServer(socketserver.BaseRequestHandler):
            def handle(self):
                    #自定义逻辑
                    print ("This is tcp funaction")
                    conn = self.request
                    while True:
                            msg = conn.recv(1024)
                            print(msg.decode("utf8"))
                            conn.send(b"world")
    #生成一个对象
    server = socketserver.ThreadingTCPServer( ("127.0.0.1",9000),MyServer )
    
    #循环调用
    server.serve_forever()
    复制代码

    客户端

    复制代码
    import socket
    sk = socket.socket()
    sk.connect( ("127.0.0.1",9000) )
    while True:
            # 发消息
            sk.send(b"hello")
    
            # 接收服务器消息
            msg = sk.recv(1024)
            print(msg.decode("utf-8"))
    sk.close()
    复制代码

    执行服务端,然后开启三个客户端

    结果如下

     

     多并发已经完成

    学习记录,小白一枚
  • 相关阅读:
    RocketMq(三、重试机制)
    RocketMq(二、生产者、消费者demo)
    VUE(四、ajax-axios)
    配置git DiffMerge工具
    Gerrit 删除项目
    html元素
    UML类图细节
    json 转 javaBean
    将文件的编码格式转换为utf-8
    第四篇:Eclipse Android app 工程迁移到 Android Studio
  • 原文地址:https://www.cnblogs.com/wangsirde0428/p/14322658.html
Copyright © 2011-2022 走看看