Skip to main content

aio.http.server

HTTP server for the aio asyncio framework

Build status

https://travis-ci.org/phlax/aio.http.server.svg?branch=master

Installation

Requires python >= 3.4

Install with:

pip install aio.http.server

Quick start - Hello world http server

Create a web server that says hello

Save the following into a file “hello.conf”

[server/my_server]
factory = aio.http.server.factory
port = 8080
protocol = my_example.protocol

And save the following into a file named my_example.py

import asyncio
import aiohttp

import aio.app

@aio.app.server.protocol
def protocol(name):
    loop = asyncio.get_event_loop()
    webapp = aiohttp.web.Application(loop=loop)

    @asyncio.coroutine
    def handle_hello_world(webapp):
        return aiohttp.web.Response(body=b"Hello, world")

    webapp.router.add_route("GET", "/", handle_hello_world)
    return webapp.make_handler()

Run with the aio run command

aio run -c hello.conf

aio.http.server usage

Configuration

Create 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 server

By 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 protocol

If 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'

Release files for aio.http.server 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aio.http.server 0.1.4
File Size Uploaded
aio.http.server-0.1.4.tar.gz 4.2 kB Details

Release files / aio.http.server-0.1.4.tar.gz

Download URL aio.http.server-0.1.4.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7e416ad3d553007252b09602336770dc1dc9d715fc5c3d28a485cbad1736167d
BLAKE2b-256 checksum
How to use checksums
bdf231c2c65ec006b7954aa627bb6c44b17e8d209dac9be6c9b0fd4a1c3fce1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.4 This release

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page