• Python select示例


    import select
    import socket
    import sys
    import queue
    # Create a TCP/IP
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setblocking(0)
    # Bind the socket to the port
    server_address = ('localhost', 10000)
    print (sys.stderr, 'starting up on %s port %s' % server_address)
    server.bind(server_address)
    # Listen for incoming connections
    server.listen(5)
    # Sockets from which we expect to read
    inputs = [ server ]
    # Sockets to which we expect to write
    outputs = [ ]
    # Outgoing message queues (socket:Queue)
    message_queues = {}
    while inputs:
    
        # Wait for at least one of the sockets to be ready for processing
        print (sys.stderr, 'waiting for the next event')
        readable, writable, exceptional = select.select(inputs, outputs, inputs)
    
        # Handle inputs
        for s in readable:
    
            if s is server:
                # A "readable" socket is ready to accept a connection
                connection, client_address = s.accept()
                print (sys.stderr, '  connection from', client_address)
                connection.setblocking(0)
                inputs.append(connection)
    
                # Give the connection a queue for data we want to send
                message_queues[connection] = queue.Queue()
    
            else:
                data = s.recv(1024)
                if data:
                    # A readable client socket has data
                    print (sys.stderr, '  received "%s" from %s' %(data, s.getpeername()))
                    message_queues[s].put(data)
                    # Add output channel for response
                    if s not in outputs:
                        outputs.append(s)
    
                else:
                    # Interpret empty result as closed connection
                    print (sys.stderr, '  closing', client_address)
                    # Stop listening for input on the connection
                    if s in outputs:
                        outputs.remove(s)
                    inputs.remove(s)
                    s.close()
    
                    # Remove message queue
                    del message_queues[s]
    
        # Handle outputs
        for s in writable:
            try:
                next_msg = message_queues[s].get_nowait()
            except queue.Empty:
                # No messages waiting so stop checking for writability.
                print (sys.stderr, '  ', s.getpeername(), 'queue empty')
                outputs.remove(s)
            else:
                print (sys.stderr, '  sending "%s" to %s' %(next_msg, s.getpeername()))
                s.send(next_msg)
    
        # Handle "exceptional conditions"
        for s in exceptional:
            print (sys.stderr, 'exception condition on', s.getpeername())
            # Stop listening for input on the connection
            inputs.remove(s)
            if s in outputs:
                outputs.remove(s)
            s.close()
    
            # Remove message queue
            del message_queues[s]
    import socket
    import sys
    messages = ['This is the message. ',
                 'It will be sent ',
                 'in parts.',
                 ]
    
    server_address= ('localhost', 10000)
    
    # Create aTCP/IP socket
    
    socks = [socket.socket(socket.AF_INET, socket.SOCK_STREAM),
              socket.socket(socket.AF_INET,socket.SOCK_STREAM),
              ]
    
    # Connect thesocket to the port where the server is listening
    
    print (sys.stderr, 'connecting to %s port %s' % server_address)
    for s in socks:
        s.connect(server_address)
    for message in messages:
        # Send messages on both sockets
        for s in socks:
            print (sys.stderr, '%s: sending"%s"' %(s.getsockname(), message))
            s.send(bytes(message,encoding='utf-8'))
        # Read responses on both sockets
    
        for s in socks:
            data = s.recv(1024)
            print (sys.stderr, '%s: received"%s"' %(s.getsockname(), data))
            if not data:
                print (sys.stderr, 'closingsocket', s.getsockname())
                s.close()
  • 相关阅读:
    vue项目进行时,script标签中,methods事件中函数使用的async/await
    css阴影——box-shadow
    vue报错——Module not found: Error: Can't resolve 'less-loader sass' in ...
    vue组件如何引入外部.js/.css/.scss文件
    获得汉字的拼音或者拼音简写
    model转XML
    sql server 添加表注释、字段注释
    (转)SQL Server 监控统计阻塞脚本信息
    系统首页右下角弹框
    DropDownListExtend控件
  • 原文地址:https://www.cnblogs.com/python-study/p/5903617.html
Copyright © 2020-2023  润新知