即时通讯建立一个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检查来自新连接对等方的传入消息。我只需要一种发送信息的方式。

最佳答案

简而言之,你需要使用curses
这比仅仅调用input()并接收响应要困难得多,但这正是您需要的。有一个很好的资源叫做Curses Programming with Python这是最好的开始。

关于python - 非阻塞式键盘输入python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34126256/

10-11 22:33
查看更多