本文整理汇总了Python中tornado.httpserver.HTTPServer方法的典型用法代码示例。如果您正苦于以下问题:Python httpserver.HTTPServer方法的具体用法?Python httpserver.HTTPServer怎么用?Python httpserver.HTTPServer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.httpserver
的用法示例。
在下文中一共展示了httpserver.HTTPServer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def setUp(self):
if IOLoop.configured_class().__name__ in ('TwistedIOLoop',
'AsyncIOMainLoop'):
# TwistedIOLoop only supports the global reactor, so we can't have
# separate IOLoops for client and server threads.
# AsyncIOMainLoop doesn't work with the default policy
# (although it could with some tweaks to this test and a
# policy that created loops for non-main threads).
raise unittest.SkipTest(
'Sync HTTPClient not compatible with TwistedIOLoop or '
'AsyncIOMainLoop')
self.server_ioloop = IOLoop()
sock, self.port = bind_unused_port()
app = Application([('/', HelloWorldHandler)])
self.server = HTTPServer(app, io_loop=self.server_ioloop)
self.server.add_socket(sock)
self.server_thread = threading.Thread(target=self.server_ioloop.start)
self.server_thread.start()
self.http_client = HTTPClient()
示例2: test_chunked_request_body
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def test_chunked_request_body(self):
# Chunked requests are not widely supported and we don't have a way
# to generate them in AsyncHTTPClient, but HTTPServer will read them.
self.stream.write(b"""\
POST /echo HTTP/1.1
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded
4
foo=
3
bar
0
""".replace(b"\n", b"\r\n"))
read_stream_body(self.stream, self.stop)
headers, response = self.wait()
self.assertEqual(json_decode(response), {u('foo'): [u('bar')]})
示例3: test_missing_key
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def test_missing_key(self):
"""A missing SSL key should cause an immediate exception."""
application = Application()
module_dir = os.path.dirname(__file__)
existing_certificate = os.path.join(module_dir, 'test.crt')
existing_key = os.path.join(module_dir, 'test.key')
self.assertRaises((ValueError, IOError),
HTTPServer, application, ssl_options={
"certfile": "/__mising__.crt",
})
self.assertRaises((ValueError, IOError),
HTTPServer, application, ssl_options={
"certfile": existing_certificate,
"keyfile": "/__missing__.key"
})
# This actually works because both files exist
HTTPServer(application, ssl_options={
"certfile": existing_certificate,
"keyfile": existing_key,
})
示例4: tornadoserver
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def tornadoserver():
setup_logging('tornadoserver')
app = create_app(parse_options())
fsh_folder = app.blueprints['flask_statics_helper'].static_folder
log_messages(app, OPTIONS['--port'], fsh_folder)
# Setup the application.
container = wsgi.WSGIContainer(app)
application = web.Application([
(r'/static/flask_statics_helper/(.*)', web.StaticFileHandler, dict(path=fsh_folder)),
(r'/(favicon\.ico)', web.StaticFileHandler, dict(path=app.static_folder)),
(r'/static/(.*)', web.StaticFileHandler, dict(path=app.static_folder)),
(r'.*', web.FallbackHandler, dict(fallback=container))
]) # From http://maxburstein.com/blog/django-static-files-heroku/
http_server = httpserver.HTTPServer(application)
http_server.bind(OPTIONS['--port'])
# Start the server.
http_server.start(0) # Forks multiple sub-processes
ioloop.IOLoop.instance().start()
示例5: main
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def main():
global config, output_wechat, app
config = ConfigParser()
config.read('config')
from .utility import output_wechat
app = Application()
parse_command_line()
try:
from .views import app
http_server = HTTPServer(app)
http_server.listen(config['base']['port'])
IOLoop.current().start()
except Exception as e:
print(e)
finally:
with open('config', 'w') as f:
config.write(f)
示例6: setUp
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def setUp(self):
self.server_ioloop = IOLoop()
event = threading.Event()
@gen.coroutine
def init_server():
sock, self.port = bind_unused_port()
app = Application([("/", HelloWorldHandler)])
self.server = HTTPServer(app)
self.server.add_socket(sock)
event.set()
def start():
self.server_ioloop.run_sync(init_server)
self.server_ioloop.start()
self.server_thread = threading.Thread(target=start)
self.server_thread.start()
event.wait()
self.http_client = HTTPClient()
示例7: test_chunked_request_body
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def test_chunked_request_body(self):
# Chunked requests are not widely supported and we don't have a way
# to generate them in AsyncHTTPClient, but HTTPServer will read them.
self.stream.write(
b"""\
POST /echo HTTP/1.1
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded
4
foo=
3
bar
0
""".replace(
b"\n", b"\r\n"
)
)
start_line, headers, response = self.io_loop.run_sync(
lambda: read_stream_body(self.stream)
)
self.assertEqual(json_decode(response), {u"foo": [u"bar"]})
示例8: _patch_httpserver
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def _patch_httpserver(self):
"""
重写httpserver的xheader配置,让gunicorn可以加载xheaders设置
:return:
"""
httpserver = sys.modules["tornado.httpserver"]
try:
xhs = settings.XHEADERS
except:
xhs = True
class TorngasHTTPServer(httpserver.HTTPServer):
def __init__(self, request_callback, xheaders=xhs, **kwargs):
super(TorngasHTTPServer, self).__init__(request_callback,
xheaders=xheaders,
**kwargs)
httpserver.HTTPServer = TorngasHTTPServer
sys.modules["tornado.httpserver"] = httpserver
示例9: load_httpserver
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def load_httpserver(self, sockets=None, **kwargs):
if not sockets:
from tornado.netutil import bind_sockets
if settings.IPV4_ONLY:
import socket
sockets = bind_sockets(options.port, options.address, family=socket.AF_INET)
else:
sockets = bind_sockets(options.port, options.address)
http_server = tornado.httpserver.HTTPServer(self.application, **kwargs)
http_server.add_sockets(sockets)
self.httpserver = http_server
return self.httpserver
示例10: run
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def run(self):
"""
Function to Run the server. Server runs on host: 127.0.0.1 and port: 2000 by default. Debug is also set to false
by default
Can be overriden by using the config.ini file
"""
define("port", default=self.port, help="Run on given port", type=int)
define("host", default=self.host, help="Run on given host", type=str)
define("debug", default=self.debug, help="True for development", type=bool)
parse_command_line()
print(Fore.GREEN + "Starting Bast Server....")
print(Fore.GREEN + "Bast Server Running on %s:%s" % (options.host, options.port))
application = Application(self.handler, debug=options.debug)
server = HTTPServer(application)
server.listen(options.port, options.host)
IOLoop.current().start()
示例11: setUp
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def setUp(self):
if IOLoop.configured_class().__name__ == 'TwistedIOLoop':
# TwistedIOLoop only supports the global reactor, so we can't have
# separate IOLoops for client and server threads.
raise unittest.SkipTest(
'Sync HTTPClient not compatible with TwistedIOLoop')
self.server_ioloop = IOLoop()
sock, self.port = bind_unused_port()
app = Application([('/', HelloWorldHandler)])
server = HTTPServer(app, io_loop=self.server_ioloop)
server.add_socket(sock)
self.server_thread = threading.Thread(target=self.server_ioloop.start)
self.server_thread.start()
self.http_client = HTTPClient()
示例12: test_missing_key
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def test_missing_key(self):
"""A missing SSL key should cause an immediate exception."""
application = Application()
module_dir = os.path.dirname(__file__)
existing_certificate = os.path.join(module_dir, 'test.crt')
self.assertRaises(ValueError, HTTPServer, application, ssl_options={
"certfile": "/__mising__.crt",
})
self.assertRaises(ValueError, HTTPServer, application, ssl_options={
"certfile": existing_certificate,
"keyfile": "/__missing__.key"
})
# This actually works because both files exist
HTTPServer(application, ssl_options={
"certfile": existing_certificate,
"keyfile": existing_certificate
})
示例13: test_unix_socket
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def test_unix_socket(self):
sockfile = os.path.join(self.tmpdir, "test.sock")
sock = netutil.bind_unix_socket(sockfile)
app = Application([("/hello", HelloWorldRequestHandler)])
server = HTTPServer(app, io_loop=self.io_loop)
server.add_socket(sock)
stream = IOStream(socket.socket(socket.AF_UNIX), io_loop=self.io_loop)
stream.connect(sockfile, self.stop)
self.wait()
stream.write(b"GET /hello HTTP/1.0\r\n\r\n")
stream.read_until(b"\r\n", self.stop)
response = self.wait()
self.assertEqual(response, b"HTTP/1.0 200 OK\r\n")
stream.read_until(b"\r\n\r\n", self.stop)
headers = HTTPHeaders.parse(self.wait().decode('latin1'))
stream.read_bytes(int(headers["Content-Length"]), self.stop)
body = self.wait()
self.assertEqual(body, b"Hello world")
stream.close()
server.stop()
示例14: setUp
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def setUp(self):
if IOLoop.configured_class().__name__ == 'TwistedIOLoop':
# TwistedIOLoop only supports the global reactor, so we can't have
# separate IOLoops for client and server threads.
raise unittest.SkipTest(
'Sync HTTPClient not compatible with TwistedIOLoop')
self.server_ioloop = IOLoop()
@gen.coroutine
def init_server():
sock, self.port = bind_unused_port()
app = Application([('/', HelloWorldHandler)])
self.server = HTTPServer(app)
self.server.add_socket(sock)
self.server_ioloop.run_sync(init_server)
self.server_thread = threading.Thread(target=self.server_ioloop.start)
self.server_thread.start()
self.http_client = HTTPClient()
示例15: run
# 需要导入模块: from tornado import httpserver [as 别名]
# 或者: from tornado.httpserver import HTTPServer [as 别名]
def run(self, quiet=None, server=''):
""" Start the tornado server, run forever.
'quiet' and 'server' arguments are no longer used, they are keep only for backward compatibility
"""
try:
loop = IOLoop()
http_server = HTTPServer(WSGIContainer(self.app))
http_server.listen(self.port)
loop.start()
except socket.error as serr:
# Re raise the socket error if not "[Errno 98] Address already in use"
if serr.errno != errno.EADDRINUSE:
raise serr
else:
logger.warning("""The webserver port {} is already used.
The SnapRobotServer is maybe already run or another software use this port.""".format(self.port))