2013-03-20 37 views
1

我已经用尽了msvcrt.kbhit()在Python中如何可以打印出数据,只要我点击指定的键需要按下的想法用完了。它看起来像while循环需要再次循环,才能打印出我的愿望输出。有人可以帮帮我吗。这里是我的代码:Python套接字线程与kbhit

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 
+0

究竟是什么问题? – wRAR 2013-03-20 14:49:59

+0

当代码从客户端接收数据时,我按下键盘上的's'键打印停止不打印,因为我按下了键,它需要另一个数据才能在打印“停止”之前接收,以便在屏幕上打印 – 2013-03-20 14:54:04

回答

1

大部分时间在recv调用程序块,直到被接收的一些数据也不会执行kbhit + getch。如果您需要立即处理键盘输入,则可能需要使套接字非阻塞并轮询套接字和循环中的键盘,并在它们出现时处理它们中的数据。