Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Created August 23, 2021 15:16
Show Gist options
  • Save kgriffs/5e002cb483cb67be39dc5b95e5b0c6b0 to your computer and use it in GitHub Desktop.
Save kgriffs/5e002cb483cb67be39dc5b95e5b0c6b0 to your computer and use it in GitHub Desktop.
Falcon - Stream aiohttp response example
# Courtesy of Vytautas Liuolia @vytas7
import aiohttp
import falcon.asgi
class Proxy(object):
UPSTREAM = 'https://falconframework.org'
async def handle(self, req, resp):
headers = dict(req.headers, Via='Falcon')
for name in ('host', 'connection', 'referer'):
headers.pop(name, None)
async with aiohttp.ClientSession() as session:
async with session.get(
self.UPSTREAM + req.path, headers=headers) as response:
resp.data = await response.read()
resp.content_type = response.headers.get(
'Content-Type', falcon.MEDIA_HTML)
resp.status = response.status
app = falcon.asgi.App()
app.add_sink(Proxy().handle)
@kgriffs
Copy link
Author

kgriffs commented Aug 23, 2021

TODO: Explore ways to add native support to the framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment