Python I/O多路复用简单实例

server端

import  socket

server1 = socket.socket()
server1.bind(('127.0.0.1',8000))
server1.listen()

server2 = socket.socket()
server2.bind(('127.0.0.1',8001))
server2.listen()

server = [server1,server2]
while True:
    import select
    r_list,w_list,x_list = select.select(server,[],[],1)
    # print(r_list)
    for item in r_list:
        conn,addr = item.accept()
        receive = conn.recv(1024)
        content =str(receive,'utf-8')
        print(content)

client1

import socket

client1 = socket.socket()
client1.connect(('127.0.0.1',8000))
client1.sendall(bytes('你好这是8000',encoding='utf-8'))
client1.close()

client2

import socket

client1 = socket.socket()
client1.connect(('127.0.0.1',8001))
client1.sendall(bytes('你好这是80002',encoding='utf-8'))
client1.close()

测试结果

你好这是8000
你好这是80002

你可能感兴趣的:(Python,运维)