Created
September 30, 2016 07:16
-
-
Save ojii/0d072ab3d86d5fc1d54c8da45c7fee64 to your computer and use it in GitHub Desktop.
awsgi2 (asynchronouse, wsgi-like, http2) app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from awsgi2 import run, build_context, THeaders, TRequestReader, TSendHeaders, TResponse | |
async def app(headers: THeaders, | |
request: TRequestReader, | |
send_headers: TSendHeaders) -> TResponse: | |
if headers[':method'] not in ('GET', 'POST'): | |
send_headers({ | |
':status': '405', | |
'content-length': '0', | |
}) | |
else: | |
body = b'' | |
async for chunk in request: | |
body += chunk | |
data = json.dumps({ | |
'headers': headers, | |
'body': body.decode('utf-8') | |
}, indent=4).encode('utf-8') | |
send_headers({ | |
':status': '200', | |
'content-type': 'application/json', | |
'content-length': len(data), | |
}) | |
yield data | |
if __name__ == '__main__': | |
ssl_context = build_context('cert.crt', 'cert.key') | |
run(ssl_context, app, 8765, 'localhost') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment