469,282 Members | 1,939 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Is there a simple way to exit a while loop on keystroke?

I am new to python, and have written a simple program to read a port
via telnet. I would like it to run until any key is pressed. Of
course I wouldn't mind requiring a specific keystroke in the future,
but I would think this is simpler for now.

I have used kbhit() and getch() many times in C, but I can't find
anything similar in Python. I am using Linux also, so the msvcrt
code isn't an option. I have tried sys.stdin.read(), but that hangs
UNTIL a key is pressed.

Thanks

Aug 31 '07 #1
8 19977
Hello,
I am new to python, and have written a simple program to read a port
via telnet. I would like it to run until any key is pressed. Of
course I wouldn't mind requiring a specific keystroke in the future,
but I would think this is simpler for now.

I have used kbhit() and getch() many times in C, but I can't find
anything similar in Python. I am using Linux also, so the msvcrt
code isn't an option. I have tried sys.stdin.read(), but that hangs
UNTIL a key is pressed.
You might want to look at http://docs.python.org/lib/module-curses.html

Another solution is to ask the user to hit CTRL-C
from time import sleep

try:
while 1:
print "BEEP"
sleep(1)
except KeyboardInterrupt:
print "BYE BYE"

HTH,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com

Aug 31 '07 #2
Thanks,
The curses library doesn't look to helpful to me. However using CTRL-
C is fine and is working nicely.

BTW, it should be "time.sleep(1)" in the example above, instead of
just
"sleep(1)" (Just in case any other newbies like me read this)

Thanks again

Aug 31 '07 #3
On Aug 31, 7:11 pm, gsxg <rha...@gmail.comwrote:
Thanks,
The curses library doesn't look to helpful to me.
And yet it is.

--
Arnaud

Aug 31 '07 #4
On Aug 31, 11:11 am, gsxg <rha...@gmail.comwrote:
Thanks,
The curses library doesn't look to helpful to me. However using CTRL-
C is fine and is working nicely.

BTW, it should be "time.sleep(1)" in the example above, instead of
just
"sleep(1)" (Just in case any other newbies like me read this)

Thanks again
Depends on how you import 'time'

import time
time.sleep(1)

from time import sleep
sleep(1)

~Sean

Aug 31 '07 #5
On Aug 31, 3:55 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
On Aug 31, 7:11 pm, gsxg <rha...@gmail.comwrote:
Thanks,
The curses library doesn't look to helpful to me.

And yet it is.

--
Arnaud
Maybe the OP is on Windows. The docs seem to indicate that the curses
module isn't for Windows (see excerpt below):

<quote>
While curses is most widely used in the Unix environment, versions are
available for DOS, OS/2, and possibly other systems as well. This
extension module is designed to match the API of ncurses, an open-
source curses library hosted on Linux and the BSD variants of Unix.
<unquote>
See also: http://docs.python.org/lib/module-curses.html

Oddly enough, I have it in my Windows distro, so it's rather
confusing. I've never used it, so I don't know if it plays nice on
Windows or not.

Mike

Aug 31 '07 #6
In message <11*********************@r29g2000hsg.googlegroups. com>, gsxg
wrote:
I am new to python, and have written a simple program to read a port
via telnet. I would like it to run until any key is pressed.
Did you mean "telnet" or did you mean "local terminal"? For a local
terminal, the following demo script should give you a starting point:

#!/usr/bin/python

import sys
import select
import termios
import tty

timeout = 0.0 # nonzero to wait that long for keypress

save_attrs = termios.tcgetattr(sys.stdin.fileno())
# save terminal settings for restoration--note that this script
# doesn't currently trap CTRL/C, so settings will not be properly
# restored if that is hit
tty.setcbreak(sys.stdin.fileno())
# or can use setraw to block CTRL/C
while True :
(input_ready, _, _) = select.select((sys.stdin,), (), (), timeout)
if sys.stdin in input_ready :
break
#end if
sys.stdout.write("Running...\n")
#end while
termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, save_attrs)
sys.stdout.write("Finished.\n")

Aug 31 '07 #7
"gsxg" <rh****@gmail.comwrote:
I am new to python, and have written a simple program to read a port
via telnet. I would like it to run until any key is pressed. Of
course I wouldn't mind requiring a specific keystroke in the future,
but I would think this is simpler for now.

I have used kbhit() and getch() many times in C, but I can't find
anything similar in Python. I am using Linux also, so the msvcrt
code isn't an option. I have tried sys.stdin.read(), but that hangs
UNTIL a key is pressed.
Unblock the stdin using the fcntl module. Then you get an IOError
if there is nothing.

def unblock(f):
"""Given file f , sets it unblock flag to true"""
fcntl.fcntl(f.fileno(),fcntl.F_SETFL,os.O_NONBLOCK )

f is the file object you get from the open...

hth - Hendrik

Sep 1 '07 #8
On Aug 31, 10:28 pm, kyoso...@gmail.com wrote:
On Aug 31, 3:55 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
On Aug 31, 7:11 pm, gsxg <rha...@gmail.comwrote:
Thanks,
The curses library doesn't look to helpful to me.
And yet it is.
--
Arnaud

Maybe the OP is on Windows. The docs seem to indicate that the curses
module isn't for Windows (see excerpt below):
Ah yes, Windows! I didn't think about it. Yes, I reckon it's unlikely
that curses is implemented for MS Windows.

[...]
--
Arnaud
Sep 1 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Redduck | last post: by
reply views Thread by adriann | last post: by
9 posts views Thread by somenath | last post: by
3 posts views Thread by Rhals | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.