Skip to content

Instantly share code, notes, and snippets.

@mashony
Created June 28, 2021 14:53
Show Gist options
  • Save mashony/cde3175da1e60d34d18cfe3a4669f41e to your computer and use it in GitHub Desktop.
Save mashony/cde3175da1e60d34d18cfe3a4669f41e to your computer and use it in GitHub Desktop.
aiohttp cli-serv header errors
import asyncio
import os
import aiohttp
HOST = os.getenv('HOST', '0.0.0.0')
PORT = int(os.getenv('PORT', 8080))
URL = f'http://{HOST}:{PORT}/'
headers = {
"SomeOtherHeader": "somecoolstr",
"CustomHeader": "somebrokenstr\n",
"AHeader": "somecoolstr",
}
async def main():
async with aiohttp.ClientSession() as session:
async with session.get(URL, headers=headers) as resp:
print(resp.status)
print(await resp.text())
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
Request.headers {'Host': '0.0.0.0:8080', 'SomeOtherHeader': 'somecoolstr', 'CustomHeader': 'somebrokenstr'}
import asyncio
import os
import logging
import aiohttp.web
HOST = os.getenv('HOST', '0.0.0.0')
PORT = int(os.getenv('PORT', 8080))
async def testhandler(request):
print("Request.headers", dict(request.headers))
return aiohttp.web.Response(text='Test handle')
def main():
loop = asyncio.get_event_loop()
app = aiohttp.web.Application(loop=loop)
logging.basicConfig(level=logging.DEBUG)
app.router.add_route('GET', '/', testhandler)
aiohttp.web.run_app(app, host=HOST, port=PORT)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment