archiving community contributions on YouTube: unpublished captions, title and description translations and caption credits
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

86 lignes
2.9 KiB

  1. from os import system
  2. from os.path import isfile
  3. from time import sleep
  4. HEROKU = False
  5. if isfile("../Procfile") and isfile("../requirements.txt"):
  6. print("Heroku detected... using 20 threads instead of 50.")
  7. HEROKU = True
  8. if HEROKU:
  9. if not "aioquic" in open("../requirements.txt").read():
  10. print("Installing aioquic on this Heroku instance since it wasn't installed on deploy...")
  11. system("python3 -m pip install --user aioquic")
  12. sleep(5)
  13. import asyncio
  14. from typing import cast
  15. from urllib.parse import urlparse
  16. from aioquic.h3.connection import H3_ALPN
  17. from aioquic.asyncio.client import connect
  18. from aioquic.quic.configuration import QuicConfiguration
  19. from http3_base import HttpClient, prepare_response, perform_http_request
  20. from urllib.parse import urlparse
  21. class HTTP3Response:
  22. def __init__(self, input) -> None:
  23. headers, content, url, redirect = input
  24. self.content = content
  25. try:
  26. self.text = content.decode()
  27. except:
  28. print("Text decoding error")
  29. self.text = ""
  30. self.headers = {}
  31. for k, v in headers.items():
  32. self.headers[k.decode()] = v.decode()
  33. try:
  34. self.status_code = int(headers[b":status"])
  35. self.url = url
  36. except:
  37. print("Status code not included as header, defaulting to 200")
  38. self.status_code = 200
  39. self.ok = self.status_code < 400
  40. async def main(address, headers={}):
  41. parsed = urlparse(address)
  42. configuration = QuicConfiguration(
  43. is_client=True, alpn_protocols=H3_ALPN
  44. )
  45. async with connect(parsed.netloc, port=443, configuration=configuration, create_protocol=HttpClient) as client:
  46. client = cast(HttpClient, client)
  47. redirect = False
  48. while True:
  49. events = await perform_http_request(client=client, url=address, headers=headers)
  50. oheaders, ocontent = prepare_response(events)
  51. statuscode = int(oheaders[b":status"])
  52. if statuscode >= 300 and statuscode < 400 and b"location" in oheaders.keys():
  53. redirect = True
  54. origurl = address
  55. parsedorig = urlparse(origurl)
  56. address = oheaders[b"location"].decode()
  57. parsednew = urlparse(address)
  58. if not parsednew.scheme and not parsednew.netloc:
  59. address = parsedorig.scheme + "://" + parsedorig.netloc + address
  60. else:
  61. break
  62. return HTTP3Response((oheaders, ocontent, address, redirect))
  63. def get(url, headers={}, params={}):
  64. plist = []
  65. for item in params:
  66. k, v = item
  67. plist.append(str(k)+"="+str(v))
  68. if plist:
  69. pstring = "?"+"&".join(plist)
  70. else:
  71. pstring = ""
  72. url = url+pstring
  73. loop = asyncio.new_event_loop()
  74. return loop.run_until_complete(main(url, headers=headers))