Created
August 23, 2021 15:16
-
-
Save kgriffs/5e002cb483cb67be39dc5b95e5b0c6b0 to your computer and use it in GitHub Desktop.
Falcon - Stream aiohttp response example
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
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: Explore ways to add native support to the framework.