带kbhi套接字的Python线程

2024-07-08 02:45:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道怎么才能msvcrt.kbhit()在python中可以打印出数据,只要我点击指定的键就可以按。看起来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

Tags: 数据代码selfifdefchannelconnectionwill
1条回答
网友
1楼 · 发布于 2024-07-08 02:45:22

大多数时候,您的程序阻塞了recv调用,因此在接收到某些数据之前,它不会执行kbhit+getch。如果需要立即处理键盘输入,则可能需要使套接字非阻塞,并轮询循环中的套接字和键盘,并按显示的方式处理来自它们的数据。在

相关问题 更多 >

    热门问题