Results 1 to 6 of 6

Thread: Exit a continuous python script with key press?

  1. #1
    Join Date
    Oct 2008
    Beans
    18

    Exit a continuous python script with key press?

    So I have this python script which looks roughly like this:
    PHP Code:
    while 1:
        
    proc subprocess.Popen('sudo iwlist wlan0 scan'shell=Truestdout=subprocess.PIPE, )
        
    results proc.communicate()[0].split('\n')

        
    # do stuff with wireless scan results 
    How would I monitor for a keypress, like the escape button, so I can exit the while loop?

  2. #2
    Join Date
    Jun 2008
    Beans
    38

    Re: Exit a continuous python script with key press?

    You could enclose it within a try-except block, where except catches KeyboardInterrupt. The key to press will have to be Ctrl-C though.
    My blog about random things I do in Linux.

  3. #3
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    Re: Exit a continuous python script with key press?

    Perhaps you could write a small C module which get's the keypresses from X11, and then sends exit signal to the python app?

    I once wrote an application with C, which uses X11 to make fake key presses. Works probably the other way around too.
    Keyboard not found!

    Press any key to continue...

  4. #4
    Join Date
    Mar 2007
    Location
    Turkey
    Beans
    1,574

    Re: Exit a continuous python script with key press?

    Why not use the EOF combination?

  5. #5
    Join Date
    Oct 2008
    Beans
    18

    Re: Exit a continuous python script with key press?

    Quote Originally Posted by mentallaxative View Post
    You could enclose it within a try-except block, where except catches KeyboardInterrupt. The key to press will have to be Ctrl-C though.
    Sounds kinda hacky...8).

    That'll work fine for now, but I was just hoping there was a module I could use.

  6. #6
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Exit a continuous python script with key press?

    If you are okay with using Ctrl-C to break,
    then there is the signal module which you could use to catch the SIGINT signal:

    PHP Code:
    #!/usr/bin/env python
    # See http://www.linuxjournal.com/article/3946 listing #3

    import signal 
    import sys
    import subprocess

    def signal_handler
    (signalframe):
        print 
    'You pressed Ctrl-C!'
        
    sys.exit(0)
    signal.signal(signal.SIGINTsignal_handler)
    print 
    'Press Ctrl-C to stop'
    while 1:
        
    proc subprocess.Popen('sudo iwlist wlan0 scan'shell=Truestdout=subprocess.PIPE, )
        
    results proc.communicate()[0].split('\n')
        
    # do stuff with wireless scan results 
    Last edited by unutbu; November 29th, 2008 at 08:40 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •