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

v0.7.1

  • Package as a module instead of as a single file

v0.7.2

  • Update existing routes when adding new 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.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

aioserver-0.7.3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aioserver-0.7.3.tar.gz
  • Upload date:
  • Size: 6.9 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.3.tar.gz
Algorithm Hash digest
SHA256 bad85d0cfab8e807b819c8199829ddcd3fe8a336170aee40ccf603c98e6f5512
MD5 9d0dd4909dd1a26bad246a73cd340f05
BLAKE2b-256 8cbf773cd19bd8234dac0883b40f4a7005797ccfba73ad29f398a0fd9b14e2bf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aioserver-0.7.3-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d5833edc836a36e0d01c8ae3d8c3e92c0a82b2279e81f7d7cf3e76da90e16e03
MD5 3aac20a4a4958ed189c8f00b74d7e618
BLAKE2b-256 e5ef0a123950d6d3c1cc65da2d3dfd65ea45f12d282475468252c32ff18e73ce

See more details on using hashes here.

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