I am writing a ping app for my networking class. I have the ping part of the code working perfectly, my issue is in shutting down my "ping" server. If I run it with while True, not even ctrl-C will shut it down till the next ping request. I can set it to only run for 4 iterations or whatever, but I'd prefer to have the server run until I give a quit command.

I've tried using threads and threading, and neither one worked for me. This is all just basic command line stuff, so I'd like to be able to push 'q' and enter to quit.

thanks for any help you guys can give me.
let me know if you want to actually see my code.

9 Years
Discussion Span
Last Post by vidaj

You're looking to remotely shutdown a linux server? Or to quit a python program? sys.exit(rtn_code) can exit a python program... but I'm not sure if that's what you're looking for.


I use pyprocessing for this. For Python 2.5 it is pyprocessing, for 2.6 and above it is included in the distro and named multiprocessing. http://pyprocessing.berlios.de/ This will exit when you hit <Enter>.

import subprocess
from processing import Process

class test():

   def funct_1(self, name):
      subprocess.Popen("ping www.google.com", shell=True)
if __name__ == '__main__':
      p = Process(target=CT.funct_1, args=('P',))

      key = raw_input("Press <Enter> to exit (or is it <exit> to enter?)")

yes some more details would be more useful... such as what you are trying to shut down... a server or your program... and some snippets of code that you are having trouble with... that way we can see what you have attempted and what you have included in your program...


i am trying to shutdown a python script.

the python script is a ping server. i want the script to run in a while True loop until i tell it otherwise.

using threads and threading i could get it to quit on the NEXT ping message.

woooee has the right idea but here's the catch. because it's a school assignment, i have to make it run on their setup of python. woooee, is that module standard in ver 2.4.3?


Nope. Not a standard module until 2.6. I did remember that there is a way to do it with threads using queues and a variable assigned to the while loop that can be set to False. But I don't know what it is. A google should find it though.


Just a wild stab - could you use select to solve your problem?

Since it's a server, you have a socket that you listen to for incoming connections. you can use select to poll each socket to see if there's data incoming. sys.stdin works in the same way as a socket, so you can use select to check if there's data entered on the keyboard. If there is - just break the loop :)

Have not tested this code, but something along these lines should do the trick.

import sys, select

exitFlag = False
while not exitFlag:
    checkList = [sys.stdin, getListenSocket()]
    read, write, failed = select.select(checkList, [], [])
    if sys.stdin in read:
         exitFlag = True
    if getListenSocket() in read:

In handlePingConnection() you accept the incoming connection. socket.accept usually blocks the program until someone connects to it, but using select you can listen to several sockets at the same time, and specify a timeout if you want to do something more inside the loop. If select returns with your socket inside the read-list, socket.accept won't block because someone have already opened a connection :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.