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
Release files for aioserver 0.7.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aioserver-0.7.4.tar.gz | 7.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aioserver-0.7.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.9 kB
Release files / aioserver-0.7.4.tar.gz
| Download URL | aioserver-0.7.4.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
22dc9bc5c6badd59dfa844ca166c60c6762cf4f775a4685206c15b6e5d3b5e1a
|
|
BLAKE2b-256 checksum How to use checksums |
9cf31d5b9dd925def86d3dd9de58a24fee2066bad7eb812e641ebd27c536f525
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Release files / aioserver-0.7.4-py3-none-any.whl
| Download URL | aioserver-0.7.4-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f0699966ab6a5a4417293df1810eae4f8e6cb8b350f2b7299fb8f6f5db2ff3d2
|
|
BLAKE2b-256 checksum How to use checksums |
fd61071cd5f571b90bf9e440e8923310513e0f7f681031cfdd168ea11ae1844a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|