Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
- #from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
- from SocketServer import ThreadingMixIn
- import sys
- import os
- #class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
- # """Handle requests in a separate thread."""
- class StopableHTTPServer(HTTPServer):
- stop = False
- # def force_stop(self):
- # self.server_close()
- # self.stopped = True
- #self.create_dummy_request()
- def serve_forever(self):
- self.stop = False
- while not self.stop:
- self.handle_request()
- #self.server_close()
- class MyHandler(BaseHTTPRequestHandler):
- _key = ''
- def do_QUIT(self):
- """send 200 OK response, and set server.stop to True"""
- self.send_response(200)
- self.end_headers()
- self.server.stop = True
- def do_GET(self):
- if 'stop' in self.path:
- print "Callling stop"
- self.do_QUIT()
- return
- else:
- try:
- f = open(self._key, 'r')
- self.send_response(200)
- self.send_header('Content-type', 'text/plain')
- self.end_headers()
- data=f.read()
- self.wfile.write(data)
- f.close()
- except IOError:
- self.send_error(404,'File Not Found: %s' % self.path)
- #thread.sleep(10)
- return
- def stop_server (port):
- """send QUIT request to http server running on localhost:<port>"""
- conn = httplib.HTTPConnection("localhost:%d" % port)
- conn.request("QUIT", "/")
- conn.getresponse()
- HOST_NAME = '127.0.0.1'
- PORT_NUMBER = int(sys.argv[2])
- RUN=True
- key=sys.argv[1]
- #print "==== PROXY: Using signal file " + file
- if __name__ == '__main__':
- MyHandler._key=key
- server = StopableHTTPServer(('',PORT_NUMBER), MyHandler)
- #while RUN:
- #try:
- server.serve_forever()
- #except:
- # print "Stopping"
- # server.force_stop()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment