Różnice
Różnice między wybraną wersją a wersją aktualną.
| Poprzednia rewizja po obu stronach Poprzednia wersja Nowa wersja | Poprzednia wersja | ||
| programowanie:python:http [2024/08/26 19:02] – sases | programowanie:python:http [2024/10/09 19:18] (aktualna) – sases | ||
|---|---|---|---|
| Linia 1: | Linia 1: | ||
| ====== Python HTTP ====== | ====== Python HTTP ====== | ||
| + | |||
| + | ===== request ===== | ||
| + | <code python> | ||
| + | import requests | ||
| + | |||
| + | html = requests.get(" | ||
| + | print(html.text) | ||
| + | </ | ||
| \\ | \\ | ||
| - | [[https:// | ||
| - | [[https:// | ||
| \\ | \\ | ||
| + | ==== get ==== | ||
| <code python> | <code python> | ||
| import requests | import requests | ||
| - | html = requests.get(" | + | url = 'http://example.com/ |
| - | print(html.text) | + | response = requests.get(url) |
| + | |||
| + | if response.status_code == 200: | ||
| + | data = response.json() | ||
| + | print(data) | ||
| + | |||
| + | key_value = data[" | ||
| + | |||
| + | if key_value is not None: | ||
| + | print(key_value) | ||
| + | else: | ||
| + | print(" | ||
| + | else: | ||
| + | | ||
| </ | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | |||
| + | ==== post ==== | ||
| + | <code python> | ||
| + | import requests | ||
| + | |||
| + | url = ' | ||
| + | data = { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | response = requests.post(url, | ||
| + | |||
| + | if response.status_code == 201: | ||
| + | print(" | ||
| + | print(response.json()) | ||
| + | else: | ||
| + | print(f' | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | |||
| + | ==== patch ==== | ||
| + | <code python> | ||
| + | import requests | ||
| + | |||
| + | url = ' | ||
| + | data = { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | response = requests.patch(url, | ||
| + | |||
| + | if response.status_code == 200: | ||
| + | print(" | ||
| + | print(response.json()) | ||
| + | else: | ||
| + | print(f' | ||
| + | </ | ||
| + | |||