本文整理匯總了Python中tornado.httpserver方法的典型用法代碼示例。如果您正苦於以下問題:Python tornado.httpserver方法的具體用法?Python tornado.httpserver怎麽用?Python tornado.httpserver使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tornado
的用法示例。
在下文中一共展示了tornado.httpserver方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: listen
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def listen(self, port, address="", **kwargs):
"""為應用程序在給定端口上啟動一個HTTP server.
這是一個方便的別名用來創建一個 `.HTTPServer` 對象並調用它
的listen方法. `HTTPServer.listen <.TCPServer.listen>`
不支持傳遞關鍵字參數給 `.HTTPServer` 構造器. 對於高級用途
(e.g. 多進程模式), 不要使用這個方法; 創建一個
`.HTTPServer` 並直接調用它的
`.TCPServer.bind`/`.TCPServer.start` 方法.
注意在調用這個方法之後你仍然需要調用
``IOLoop.current().start()`` 來啟動該服務.
返回 `.HTTPServer` 對象.
.. versionchanged:: 4.3
現在返回 `.HTTPServer` 對象.
"""
# import is here rather than top level because HTTPServer
# is not importable on appengine
from tornado.httpserver import HTTPServer
server = HTTPServer(self, **kwargs)
server.listen(port, address)
return server
示例2: listen
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def listen(self, port, address="", **kwargs):
"""Starts an HTTP server for this application on the given port.
This is a convenience alias for creating an `.HTTPServer`
object and calling its listen method. Keyword arguments not
supported by `HTTPServer.listen <.TCPServer.listen>` are passed to the
`.HTTPServer` constructor. For advanced uses
(e.g. multi-process mode), do not use this method; create an
`.HTTPServer` and call its
`.TCPServer.bind`/`.TCPServer.start` methods directly.
Note that after calling this method you still need to call
``IOLoop.instance().start()`` to start the server.
"""
# import is here rather than top level because HTTPServer
# is not importable on appengine
from tornado.httpserver import HTTPServer
server = HTTPServer(self, **kwargs)
server.listen(port, address)
示例3: listen
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def listen(self, port, address="", **kwargs):
"""Starts an HTTP server for this application on the given port.
This is a convenience alias for creating an `.HTTPServer`
object and calling its listen method. Keyword arguments not
supported by `HTTPServer.listen <.TCPServer.listen>` are passed to the
`.HTTPServer` constructor. For advanced uses
(e.g. multi-process mode), do not use this method; create an
`.HTTPServer` and call its
`.TCPServer.bind`/`.TCPServer.start` methods directly.
Note that after calling this method you still need to call
``IOLoop.current().start()`` to start the server.
Returns the `.HTTPServer` object.
.. versionchanged:: 4.3
Now returns the `.HTTPServer` object.
"""
# import is here rather than top level because HTTPServer
# is not importable on appengine
from tornado.httpserver import HTTPServer
server = HTTPServer(self, **kwargs)
server.listen(port, address)
return server
示例4: listen
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def listen(self, port, address="", **kwargs):
"""Starts an HTTP server for this application on the given port.
This is a convenience alias for creating an HTTPServer object
and calling its listen method. Keyword arguments not
supported by HTTPServer.listen are passed to the HTTPServer
constructor. For advanced uses (e.g. preforking), do not use
this method; create an HTTPServer and call its bind/start
methods directly.
Note that after calling this method you still need to call
IOLoop.instance().start() to start the server.
"""
# import is here rather than top level because HTTPServer
# is not importable on appengine
from tornado.httpserver import HTTPServer
server = HTTPServer(self, **kwargs)
server.listen(port, address)
示例5: http_server
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def http_server(request, io_loop, _unused_port):
"""Start a tornado HTTP server.
You must create an `app` fixture, which returns
the `tornado.web.Application` to be tested.
Raises:
FixtureLookupError: tornado application fixture not found
"""
http_app = request.getfixturevalue(request.config.option.app_fixture)
server = tornado.httpserver.HTTPServer(http_app)
server.add_socket(_unused_port[0])
def _stop():
server.stop()
if hasattr(server, 'close_all_connections'):
io_loop.run_sync(server.close_all_connections,
timeout=request.config.option.async_test_timeout)
request.addfinalizer(_stop)
return server
示例6: https_server
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def https_server(request, io_loop, _unused_port):
"""Start a tornado HTTPS server.
You must create an `app` fixture, which returns
the `tornado.web.Application` to be tested.
Raises:
FixtureLookupError: tornado application fixture not found
"""
https_app = request.getfixturevalue(request.config.option.app_fixture)
ssl_options = request.getfixturevalue(request.config.option.ssl_options_fixture)
server = tornado.httpserver.HTTPServer(https_app, ssl_options=ssl_options)
server.add_socket(_unused_port[0])
def _stop():
server.stop()
if hasattr(server, 'close_all_connections'):
io_loop.run_sync(server.close_all_connections,
timeout=request.config.option.async_test_timeout)
request.addfinalizer(_stop)
return server
示例7: shutdown
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def shutdown(ioloop, server):
''' 關閉server
:param server: tornado.httpserver.HTTPServer
'''
logging.info(
"HTTP interpreter service will shutdown in %ss...", 1)
server.stop()
deadline = time.time() + 1
def stop_loop():
''' 嘗試關閉loop
'''
now = time.time()
if now < deadline and (ioloop._callbacks or ioloop._timeouts):
ioloop.add_timeout(now + 1, stop_loop)
else:
# 處理完現有的 callback 和 timeout 後
ioloop.stop()
logging.info('Shutdown!')
stop_loop()
示例8: main
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def main():
''' main 函數
'''
# 開啟 search_engin_server
ioloop = tornado.ioloop.IOLoop.instance()
server = tornado.httpserver.HTTPServer(Application(), xheaders=True)
server.listen(options.port)
def sig_handler(sig, _):
''' 信號接收函數
'''
logging.warn("Caught signal: %s", sig)
shutdown(ioloop, server)
signal.signal(signal.SIGTERM, sig_handler)
signal.signal(signal.SIGINT, sig_handler)
ioloop.start()
示例9: cookies
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def cookies(self):
"""An alias for `self.request.cookies <.httpserver.HTTPRequest.cookies>`."""
return self.request.cookies
示例10: run_wsgi
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def run_wsgi(wsgi_app, port):
"""Runs wsgi (Flask) app using tornado web server."""
container = tornado.wsgi.WSGIContainer(wsgi_app)
app = tornado.web.Application([
(r'.*', tornado.web.FallbackHandler, dict(fallback=container)),
])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()
示例11: run_wsgi_unix
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def run_wsgi_unix(wsgi_app, socket):
"""Runs wsgi (Flask) app using tornado unixsocket web server."""
container = tornado.wsgi.WSGIContainer(wsgi_app)
app = tornado.web.Application([
(r'.*', tornado.web.FallbackHandler, dict(fallback=container)),
])
http_server = tornado.httpserver.HTTPServer(app)
unix_socket = tornado.netutil.bind_unix_socket(socket)
http_server.add_socket(unix_socket)
tornado.ioloop.IOLoop.instance().start()
示例12: main
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
示例13: start
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def start(self, port=None, timeout=1):
"""Starts a server in a thread using the WSGI application provided.
Will wait until the thread has started calling with an already serving
application will simple return.
Args:
port: Number of the port to use for the application, will find an open
port if a nonzero port is not provided.
timeout: Http timeout in seconds. Note that this is only respected under
tornado v4.
Raises:
RuntimeError: if server is already started.
"""
if self._server_thread is not None:
raise RuntimeError('start() called on running background server.')
self._port = port or portpicker.pick_unused_port()
# Support both internal & external colab (tornado v3 vs. v4)
# TODO(b/35548011): remove tornado v3 handling
if tornado.version[0] >= '4':
kwds = {'idle_connection_timeout': timeout, 'body_timeout': timeout}
else:
kwds = {}
self._server = tornado.httpserver.HTTPServer(self._app, **kwds)
self._ioloop = tornado.ioloop.IOLoop()
def start_server(httpd, ioloop, port):
# TODO(b/147233568): Restrict this to local connections.
host = '' # Bind to all
ioloop.make_current()
httpd.listen(port=port, address=host)
ioloop.start()
self._server_thread = threading.Thread(
target=start_server,
kwargs={
'httpd': self._server,
'ioloop': self._ioloop,
'port': self._port
})
started = threading.Event()
self._ioloop.add_callback(started.set)
self._server_thread.start()
started.wait()
示例14: run
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def run(self):
self.ioloop = IOLoop.instance()
self.alive = True
PeriodicCallback(self.watchdog, 1000, io_loop=self.ioloop).start()
# Assume the app is a WSGI callable if its not an
# instance of tornado.web.Application or is an
# instance of tornado.wsgi.WSGIApplication
app = self.wsgi
if not isinstance(app, tornado.web.Application) or \
isinstance(app, tornado.wsgi.WSGIApplication):
app = WSGIContainer(app)
# Monkey-patching HTTPConnection.finish to count the
# number of requests being handled by Tornado. This
# will help gunicorn shutdown the worker if max_requests
# is exceeded.
httpserver = sys.modules["tornado.httpserver"]
if hasattr(httpserver, 'HTTPConnection'):
old_connection_finish = httpserver.HTTPConnection.finish
def finish(other):
self.handle_request()
old_connection_finish(other)
httpserver.HTTPConnection.finish = finish
sys.modules["tornado.httpserver"] = httpserver
server_class = tornado.httpserver.HTTPServer
else:
class _HTTPServer(tornado.httpserver.HTTPServer):
def on_close(instance, server_conn):
self.handle_request()
super(_HTTPServer, instance).on_close(server_conn)
server_class = _HTTPServer
if self.cfg.is_ssl:
server = server_class(app, io_loop=self.ioloop,
ssl_options=self.cfg.ssl_options)
else:
server = server_class(app, io_loop=self.ioloop)
self.server = server
for s in self.sockets:
s.setblocking(0)
if hasattr(server, "add_socket"): # tornado > 2.0
server.add_socket(s)
elif hasattr(server, "_sockets"): # tornado 2.0
server._sockets[s.fileno()] = s
server.no_keep_alive = self.cfg.keepalive <= 0
server.start(num_processes=1)
self.ioloop.start()
示例15: main
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import httpserver [as 別名]
def main():
define(name='port', default=8000, type=int, help='run on the given port')
parse_command_line()
logger.info('====== web server starting at http://0.0.0.0:{} ======'.format(options.port))
if constants.DEBUG:
logger.info('debug mode is enabled!!!')
app = make_web_app()
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
http_server.start()
tornado.ioloop.IOLoop.instance().start()