import socket,select
s = socket.socket()
port = 1234
ip = "192.168.0.108"
s.bind((ip,port))
s.listen(5)
inputs = [s]
while True:
rs,ws,es = select.select(inputs, [], [])
for r in rs:
print('for lookup is runing.....')
if r is s:
print('begin exec accept.....')
c,addr = s.accept()
print('Got connection from', addr)
inputs.append(c)
else:
try:
data = r.recv(1024)
disconnected = not data
except socket.error:
disconnected = True
if disconnected:
print(r.getpeername(),'disconnected--go')
inputs.remove(r)
r.close()
else:
print(data)
import socket,select
s = socket.socket()
ip="192.168.0.108"
port=1234
s.bind((ip,port))
s.listen(5)
fdmap = {s.fileno():s}
p = select.poll()
p.register(s)
while True:
events = p.poll()
for fd, event in events:
if fd == s.fileno():
c,addr = s.accept()
print('Got connection from',addr)
p.register(c)
fdmap[c.fileno()] = c
elif event & select.POLLIN:
data = fdmap[fd].recv(1024)
if not data:
print(fdmap[fd].getpeername(),"diconnected")
p.unregister(fd)
del fdmap[fd]
else:
print(data)