Skip to content

Instantly share code, notes, and snippets.

@wklken
Forked from superduper/tornado-unix-socket.py
Created January 12, 2016 06:59
Show Gist options
  • Save wklken/c5bf3d1a2bb1b9cc6d16 to your computer and use it in GitHub Desktop.
Save wklken/c5bf3d1a2bb1b9cc6d16 to your computer and use it in GitHub Desktop.
Tornado web server with unix socket support
import tornado.ioloop
import tornado.web
from tornado.options import options, define
from tornado.netutil import bind_unix_socket
from tornado.httpserver import HTTPServer
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
define('listen_address', group='webserver', default='127.0.0.1', help='Listen address')
define('listen_port', group='webserver', default=8888, help='Listen port')
define('unix_socket', group='webserver', default=None, help='Path to unix socket to bind')
if __name__ == "__main__":
if options.unix_socket:
server = HTTPServer(application)
socket = bind_unix_socket(options.unix_socket)
server.add_socket(socket)
else:
application.listen(options.listen_port, address=options.listen_address)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment