我已经不知道如何在单击需要按下的指定键后,python 中的 msvcrt.kbhit() 打印出数据。看起来 while 循环需要再次循环才能打印出我想要的输出。有人可以帮帮我吗。这是我的代码:
def run(self):
global state
print "\nClient connection received!\n"
self.channel.send("Status: Server connection received")
while 1:
ctr = 1
while 1:
self.clientmess = self.channel.recv(Buffer)
if msvcrt.kbhit():
if msvcrt.getch() == 's':
print "stop"
break
#the codes belo is what i will want for self.clientmess will be so its not necessary I think to put
最佳答案
大多数时候,您的程序会阻塞在 recv
调用中,因此在接收到一些数据之前,它不会执行 kbhit
+getch
。如果您需要立即处理键盘输入,您可能需要使套接字成为非阻塞并在循环中轮询套接字和键盘,在出现时处理来自它们的数据。
关于使用 kbhit 的 Python 套接字线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15526484/