programming:python:requests
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
programming:python:requests [2021/03/18 19:32] – angelegt frank | programming:python:requests [2023/06/08 17:06] (aktuell) – Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== Requests ====== | ||
+ | ===== einfache http-Abfragen ===== | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | <code python> | ||
+ | import requests #scriptname must not be requests.py or http.py! | ||
+ | |||
+ | url=" | ||
+ | |||
+ | try: | ||
+ | response = requests.get(url) | ||
+ | # If the response was successful, no Exception will be raised | ||
+ | response.raise_for_status() | ||
+ | except requests.HTTPError as e: | ||
+ | # | ||
+ | status_code = e.response.status_code | ||
+ | print(" | ||
+ | except requests.Timeout as e: | ||
+ | print(' | ||
+ | except requests.ConnectionError as e: | ||
+ | print(f" | ||
+ | except Exception as e: | ||
+ | print(f' | ||
+ | else: | ||
+ | print(' | ||
+ | # | ||
+ | print(response.headers[' | ||
+ | json_data=response.json() | ||
+ | print(json_data[' | ||
+ | |||
+ | #get-param | ||
+ | response = requests.get( | ||
+ | ' | ||
+ | params={' | ||
+ | headers={' | ||
+ | ) | ||
+ | json_response = response.json() | ||
+ | repository = json_response[' | ||
+ | print(f' | ||
+ | |||
+ | |||
+ | requests.post(' | ||
+ | requests.put(' | ||
+ | requests.delete(' | ||
+ | requests.head(' | ||
+ | requests.patch(' | ||
+ | requests.options(' | ||
+ | </ | ||
+ | |||
+ | https:// | ||
+ | |||
+ | print(response.status_code) | ||
+ | |||
+ | Asynchrone (parallele) Abfragen: | ||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | ==== Authentifikation ==== | ||
+ | |||
+ | < | ||
+ | response = requests.get(url, | ||
+ | </ | ||
+ | ==== parallele Abfragen ==== | ||
+ | <code python> | ||
+ | import requests | ||
+ | import asyncio | ||
+ | import concurrent.futures | ||
+ | |||
+ | class multiplerequests(): | ||
+ | |||
+ | def basequery(self, | ||
+ | print(url) | ||
+ | response=requests.get(url) | ||
+ | # | ||
+ | ctype=response.headers[' | ||
+ | # | ||
+ | data=response.text | ||
+ | # | ||
+ | ret={" | ||
+ | # | ||
+ | return ret # | ||
+ | |||
+ | async def multiquery_helper(self, | ||
+ | results=[] | ||
+ | with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: | ||
+ | |||
+ | loop = asyncio.get_event_loop() | ||
+ | futures = [ | ||
+ | loop.run_in_executor( | ||
+ | executor, | ||
+ | self.basequery, | ||
+ | u | ||
+ | ) | ||
+ | for u in urls | ||
+ | ] | ||
+ | for response in await asyncio.gather(*futures): | ||
+ | #pass | ||
+ | results.append(response) | ||
+ | |||
+ | # | ||
+ | return results | ||
+ | def multiquery(self, | ||
+ | loop = asyncio.get_event_loop() | ||
+ | data=loop.run_until_complete(self.multiquery_helper(urls)) | ||
+ | return data | ||
+ | </ | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ==== github ==== | ||
+ | |||
+ | <code python> | ||
+ | import requests | ||
+ | |||
+ | token=" | ||
+ | headers = { | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | |||
+ | def ghrequest(url, | ||
+ | # headers = { | ||
+ | # ' | ||
+ | # ' | ||
+ | # } | ||
+ | if not ' | ||
+ | headers=None | ||
+ | |||
+ | if session: | ||
+ | response = session.get(url) | ||
+ | else: | ||
+ | response = requests.get(url, | ||
+ | return response | ||
+ | |||
+ | def getBranches(repo, | ||
+ | url=" | ||
+ | return ghrequest(url, | ||
+ | |||
+ | def getForks(repo, | ||
+ | url=" | ||
+ | return ghrequest(url, | ||
+ | |||
+ | def getClones(repo, | ||
+ | url=" | ||
+ | return ghrequest(url, | ||
+ | |||
+ | session = requests.Session() | ||
+ | session.headers.update(headers) | ||
+ | |||
+ | repos={' | ||
+ | for r in repos: | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | res=getClones(r, | ||
+ | print(res.status_code," | ||
+ | </ | ||