SHARE
TWEET

Untitled

K0IN Jan 1st, 2020 18 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async def getDataFromServerGen(connection):
  2.     while True:
  3.         try:
  4.             chunk = await connection.content.read(2048) # can rise error if connection close!
  5.             if not chunk: break
  6.             yield chunk
  7.         except:
  8.             break
  9.  
  10. async def ForwardUrlStream( url, request_headers ):
  11.  
  12.     del request_headers["host"]
  13.  
  14.     session = ClientSession(headers=request_headers)
  15.     server_connection = await session.get(url)
  16.  
  17.     async def fwd(res):
  18.         async with server_connection:
  19.             async for chunk in getDataFromServerGen(server_connection):
  20.                 await res.write(chunk)
  21.         await session.close()
  22.    
  23.     allowed_headers = ['Accept-Ranges', 'Content-Type', 'Content-Length', 'Content-Range', 'Accept-Encoding', 'Accept']
  24.     response_headers = dict(filter(lambda elem: elem[0] in allowed_headers, server_connection.headers.items()))
  25.     return response.stream(fwd, content_type="video/mp4", status=server_connection.status, headers=response_headers)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top