How can I capture 'Ctrl-D' in python interactive console? -


i have server runs in thread in background, , start using python -i can interactive console can type in commands , debug it. when hit ctrl-d, since server still running in background thread, console not quit. how can capture ctrl-d event can shut down server , quit gracefully? know how capture ctrl-c signals, due habbit of pressing ctrl-d 'stuck' terminal annoying.

thanks!

the server code (simplified) this:

import threading import atexit  class workerthread(threading.thread):     def __init__(self):         super(workerthread, self).__init__()         self.quit = false      def run(self):         while not self.quit:             pass      def stop(self):         self.quit = true  def q():     print "goodbye!"     t.stop()  atexit.register(q)  t = workerthread() t.start() 

and run using python -i test.py python console.

use raw_input (use input in python 3.x). pressing ctrl+d cause eoferror exception.

try:     raw_input() except eoferror:     pass 

update

use atexit - exit handlers:

import atexit  def quit_gracefully():     print 'bye'  atexit.register(quit_gracefully) 

Comments