Skip to main content

An async web framework for humans

Project description

Installation

pip install aioserver

Getting Started

Start a test server with the examples in the Usage section below:

make test

Make requests against the running test server:

curl -v localhost:8080
curl -v localhost:8080/found
curl -v localhost:8080/not-found
curl -v localhost:8080/server-error

Usage

Create Application

from aioserver import Application

app = Application()

Basic Routes

@app.get('/')
async def index(request):
    return {'message': 'Hello, world!'}

@app.get('/found')
async def found(request):
    return 302, {'Location': 'https://www.example.com/'}, {'message': 'Found'}

@app.get('/not-found')
async def not_found(request):
    return 404, {'message': 'Not Found'}

@app.get('/server-error')
async def server_error(request):
    return 500

CORS Headers

@app.cors('*')
@app.get('/cross-origin-resource-sharing')
async def cross_origin_resource_sharing(request):
    return {'message': 'Greetings from a different origin!'}

@app.cors('*', ['X-Custom-Header'])
@app.get('/cross-origin-header-sharing')
async def cross_origin_header_sharing(request):
    return 200, {'X-Custom-Header': 'share-this-header-too'}, {'message': 'Hello!'}

Session Cookie

from aioserver.middleware import hours

@app.get('/session-cookie')
@app.session(max_age=24 * hours)
async def session_cookie(request):
    print(f'session uuid {request.session}')
    return 200, {'message': 'Session UUID set as cookie for 24 hours.'}

Custom Middleware

Route-specific middleware:

@app.middleware
async def always_ok(request, handler):
    response = await handler(request)
    response.set_status(200, 'OK')
    return response

@always_ok
@app.get('/not-found-but-still-ok')
async def not_found_but_still_ok(request):
    return 404, {'message': 'Not found but still OK!'}

Global middleware:

async def strict_transport_security(request, handler):
    response = await handler(request)
    response.headers['Strict-Transport-Security'] = 'max-age=31536000'
    return response

app.use(strict_transport_security)

Run Application

app.run(host='127.0.0.1', port=8080)

Changelog

v0.2.0

  • Decorator-based request handlers

v0.4.0

  • Allow handler to specify HTTP response status
  • Allow handler to specify additional HTTP headers

v0.5.0

  • Serialize XML ElementTree as text/xml response

v0.6.0

  • Decorator-based CORS

v0.6.2

  • Fix project description

v0.7.0

  • Route-specific middleware using decorators
  • Global middleware using app.use(...)
  • Refactor CORS headers as middleware
  • Add session cookie as middleware

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioserver-0.7.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

aioserver-0.7.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file aioserver-0.7.0.tar.gz.

File metadata

  • Download URL: aioserver-0.7.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for aioserver-0.7.0.tar.gz
Algorithm Hash digest
SHA256 85e01fe7735ef58f6dccfc1ab6d87be2f2a5bf89875a7025904d070d943a7617
MD5 c30c67357121106c90753f75701d4e61
BLAKE2b-256 61afeaea640441a52438f175bdc7fbb7017337b71bbeb597775748fa2074d716

See more details on using hashes here.

Provenance

File details

Details for the file aioserver-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: aioserver-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for aioserver-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7b6faf7046e63b3a7e9bd4dc05ecf4e70f0b633045d39c2a5c305b087586242
MD5 a1efa01d40c16ad8677d6a17f02b7adb
BLAKE2b-256 3490e0845aa8dca3cd915b611089f47bd34cb2095ee8f07b940db97e01f79bfa

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page