Package: aio.http.server
aio.http.serverHTTP server for theaioasyncio frameworkBuild statusInstallationRequires python >= 3.4Install with:pipinstallaio.http.serverQuick start - Hello world http serverCreate a web server that says helloSave the following into a file “hello.conf”[server/my_server]factory=aio.http.server.factoryport=8080protocol=my_example.protocolAnd save the following into a file named my_example.pyimportasyncioimportaiohttpimportaio.app@aio.app.server.protocoldefprotocol(name):loop=asyncio.get_event_loop()webapp=aiohttp.web.Application(loop=loop)@asyncio.coroutinedefhandle_hello_world(webapp):returnaiohttp.web.Response(body=b"Hello, world")webapp.router.add_route("GET","/",handle_hello_world)returnwebapp.make_handler()Run with the aio run commandaiorun-chello.confaio.http.server usageConfigurationCreate a server config with the aio.http.server.factory factory and suppressing normal output>>> config = """
... [aio]
... log_level = ERROR
...
... [server/test]
... factory: aio.http.server.factory
... port: 7070
... """Running an http serverBy default the http server will respond with a 404 as there are no routes set up>>> import asyncio
>>> import aiohttp
>>> from aio.app.runner import runner
>>> import aio.testing>>> @aio.testing.run_forever(sleep=1)
... def run_http_server():
...     runner(['run'], config_string=config)
...
...     def call_http_server():
...         result = yield from (
...             yield from aiohttp.request(
...                "GET", "http://localhost:7070")).read()
...         print(result)
...
...     return call_http_server>>> run_http_server()
b'404: Not Found'The server object is accessible from the aio.app.servers[{name}] var>>> import aio.app>>> aio.app.servers['test']
<Server sockets=[<socket.socket...laddr=('0.0.0.0', 7070)...]>Lets clear the app>>> aio.app.clear()Running the server with a custom protocolIf you specify a protocol in the “server/” config, the http server will use that function as a protocol factory.The function should be a coroutine and is called with the name of the server>>> config_with_protocol = """
... [aio]
... log_level = ERROR
...
... [server/test]
... factory = aio.http.server.factory
... protocol = aio.http.server.tests._example_http_protocol
... port = 7070
... """We need to decorate the protocol with aio.app.server.protocol>>> @aio.app.server.protocol
... def http_protocol(name):
...     loop = asyncio.get_event_loop()
...     http_app = aiohttp.web.Application(loop=loop)
...     http_app['name'] = name
...
...     @asyncio.coroutine
...     def handle_hello_world(http_app):
...         return aiohttp.web.Response(body=b"Hello, world")
...
...     http_app.router.add_route("GET", "/", handle_hello_world)
...     return http_app.make_handler()>>> aio.http.server.tests._example_http_protocol = http_protocol>>> @aio.testing.run_forever(sleep=1)
... def run_http_server():
...     runner(['run'], config_string=config_with_protocol)
...
...     def call_http_server():
...         result = yield from (
...             yield from aiohttp.request(
...                "GET", "http://localhost:7070")).read()
...
...         print(result)
...
...     return call_http_server>>> run_http_server()
b'Hello, world'