【发布时间】:2015-12-07 04:04:40
【问题描述】:
当对等点不断侦听传入连接(新对等点)并通过终端(用户输入)向其他对等点发送命令时,我正在构建一个 p2p 系统。我一直在寻找新的同伴时很难从键盘上寻找用户输入。
print 'Listening...'
while not shutdown:
while sys.stdin in select.select([sys.stdin], [], [], 0)[0]: #look for keyboard input... not working
line = sys.stdin.readline()
if line:
send_message(line)
else: # an empty line means stdin has been closed
print('eof')
exit(0)
try: # listen for other peers
clientsock,clientaddr = mySocket.accept()
print 'Incoming connection from', clientaddr
clientsock.settimeout(None)
t = threading.Thread( target = HandlePeer, args = [clientsock] )
t.start()
except KeyboardInterrupt:
print "shutting down"
shutdown = True
continue
except Exception,e:
print 'error in peer connection %s %s' % (Exception,e)
mySocket.close()
HandlePeer 检查来自新连接的对等方的传入消息。我只需要一种发送消息的方式。
【问题讨论】: