Python入门:socket连续接收客户端数据

服务端启动

将下面的文件保存为py文件,通过python运行,该代码会等待客户端连接

from socketserver import ThreadingMixIn,ForkingMixIn,StreamRequestHandler
import socket,select

s = socket.socket()
#host = socket.gethostname()
host = '127.0.0.1'
print(host)
port = 8088
s.bind((host,port))
s.listen(5)
inputs = [s]
while True:
    rs,ws,es = select.select(inputs,[],[])
    for r in rs:
        if r is s:
            c,addr = s.accept()
            inputs.append(c)
            print(addr)
        else:
            try:
                data = r.recv(1024)
                disconnected = not data
            except:
                disconnected = True
            if disconnected:
                inputs.remove(r)
            else:
                print(data)

客户端连接

可以使用telnet 127.0.0.1 8088
Python入门:socket连续接收客户端数据_第1张图片

你可能感兴趣的:(python,socket,python)