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

G

gsxg

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
 
M

Miki

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,
 
G

gsxg

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
 
H

half.italian

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
 
K

kyosohma

And yet it is.

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
 
L

Lawrence D'Oliveiro

gsxg said:
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")
 
D

Dennis Lee Bieber

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
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
H

Hendrik van Rooyen

gsxg said:
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
 
A

Arnaud Delobelle

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.

[...]
 
D

Dennis Lee Bieber

Ah yes, Windows! I didn't think about it. Yes, I reckon it's unlikely
that curses is implemented for MS Windows.
What seems to have gotten lost is that the original post explicitly
mentions Linux... (whereas msvcrt is obviously Windows based and
unusable)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top