#
# Simple Web Server
#
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.options import define, options
import signal
define("port", default=8000, help="run on the given port",
type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
param = self.get_argument('param', 'Hello World')
self.write(param)
class Application(tornado.web.Application):
def
__init__(self):
handlers = [
(r"/", IndexHandler),
]
settings = dict(
debug=True,
)
tornado.web.Application.__init__(self, handlers,
**settings)
self.is_closing = False
def
signal_handler(self, signum, frame):
print('exiting...')
self.is_closing = True
def
try_exit(self):
if self.is_closing:
# clean up here
tornado.ioloop.IOLoop.instance().stop()
print('exit success')
if __name__ == "__main__":
print("Start
options.port={}".format(options.port))
tornado.options.parse_command_line()
#app =
tornado.web.Application(handlers=[(r"/",
IndexHandler)],debug=True)
#app =
tornado.web.Application([(r"/", IndexHandler)],debug=True)
app =
Application()
signal.signal(signal.SIGINT, app.signal_handler)
http_server =
tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.PeriodicCallback(app.try_exit, 100).start()
tornado.ioloop.IOLoop.current().start()
|