Reputation: 11
I've run out of idea with how can msvcrt.kbhit() in python can print out the data as soon as I clicked the specified key need to pressed. It looks like that while loop needs to loop once more before it can print out my desire output. Can someone please help me. Here's my code:
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
Upvotes: 1
Views: 274
Reputation: 25693
Most of the time your program blocks in the recv
call so until some data is received it will not execute kbhit
+getch
. If you need to handle keyboard input immediately you probably need to make the socket non-blocking and poll both the socket and the keyboard in the loop, handling data from them as it appears.
Upvotes: 1