Update requests exercise

This commit is contained in:
Manuel Vergara 2024-01-14 00:15:52 +01:00
parent 17d758d75b
commit 72ccded05d
12 changed files with 195 additions and 0 deletions

View File

@ -0,0 +1,34 @@
#!/usr/bin/env python3
"""
Documentación Librería requests: https://requests.readthedocs.io/en/latest/
"""
import requests
response = requests.get('https://google.es')
print(f"\n[+] Status code: {response.status_code}")
print(f"\n[+] Headers: {response.headers['content-type']}")
print(f"\n[+] Encoding: {response.encoding}")
print(f"\n[+] Guardando código fuente en index.html")
with open('index.html', 'w') as f:
f.write(response.text)
# https://httpbin.org/get
values = {'name': 'John Doe', 'age': 22, 'method': 'get'}
response = requests.get('https://httpbin.org/get', params=values)
print(f"\n[+] URL final: {response.url}")
print(f"\n[+] Código fuente: {response.text}")
# https://httpbin.org/post
payload = {'name': 'John Doe', 'age': 22, 'method': 'post'}
response = requests.post('https://httpbin.org/post', params=payload)
print(f"\n[+] URL final: {response.url}")
print(f"\n[+] Código fuente: {response.text}")

View File

@ -0,0 +1,16 @@
#!/usr/bin/env python3
"""
Documentación Librería requests: https://requests.readthedocs.io/en/latest/
"""
import requests
# https://httpbin.org/post
payload = {'name': 'John Doe', 'age': 22, 'method': 'post'}
headers = {'User-Agent': 'my-invent-app/1.1.0'}
response = requests.post('https://httpbin.org/post', params=payload, headers=headers)
print(f"\n[+] URL final: {response.url}")
print(f"\n[+] Código fuente: {response.text}")

View File

@ -0,0 +1,26 @@
#!/usr/bin/env python3
import requests
try:
response = requests.get('https://google.ese', timeout=1)
response.raise_for_status()
except requests.Timeout:
print(f"\n[!] La web ha excedido el límite de tiempo de espera")
except requests.HTTPError as http_err:
print(f"[!] Error HTTP: {http_err}")
except requests.RequestException as err:
print(f"\n[!] Error: {err}")
else:
print(f"\n[+] No ha habido ningún error en la solicitud")

View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
import requests
response = requests.get('https://httpbin.org/get')
data = response.json()
if 'headers' in data and 'User-Agent' in data['headers']:
user_agent = data['headers']['User-Agent']
else:
print(f"\n[!] No existe el campo User-Agent en la respuesta\n")

View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
import requests
# from requests.auth import HTTPBasicAuth
# Con esta librería se podría detallar el método llamándolo así:
# auth=HTTPBasicAuth('foo', 'bar')
# Paǵina para practicar autenticación: https://httpbin.org/basic-auth/foo/bar
response = requests.get('https://httpbin.org/basic-auth/foo/bar', auth=('foo', 'bar'))
print(f"\n[+] Código de error: {response.status_code}\n")
print(response.text)

View File

@ -0,0 +1,11 @@
#!/usr/bin/env python3
import requests
url = 'https://httpbin.org/cookies'
cookies = dict(cookies_are="working")
response = requests.get(url, cookies=cookies)
print(response.text)

View File

@ -0,0 +1,11 @@
#!/usr/bin/env python3
import requests
url = 'https://httpbin.org/post'
my_file = {'archivo': open('example.txt', 'r')}
response = requests.post(url, files=my_file)
print(response.text)

View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
import requests
url = 'https://httpbin.org/cookies'
set_cookies_url = 'https://httpbin.org/cookies/set/my_cookie/123123'
# Crear una sesión para arrastrar la cookie
s = requests.Session()
response = s.get(set_cookies_url)
response = s.get(url)
print(response.text)

View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
from requests import Request, Session
url = 'https://httpbin.org/get'
s = Session()
headers = {'Custom-Header':'my_custom_header'}
req = Request('GET', url, headers=headers)
prepped = req.prepare()
prepped.headers['Custom-Header'] = 'my_header_changed'
prepped.headers['Another-Header'] = 'other_header'
response = s.send(prepped)
print(response.text)

View File

@ -0,0 +1,16 @@
#!/usr/bin/env python3
import requests
url = 'http://github.com'
# r = requests.get(url, allow_redirects=False # Para que no haga reenvíos)
r = requests.get(url)
# print(r.url)
for request in r.history:
print(f"\n[+] Hemos pasado por el dominio {request.url} con un código de estado {request.status_code}")
print(f"\n[+] URL final: {r.url} con el código de estado: {r.status_code}")

View File

@ -0,0 +1,17 @@
#!/usr/bin/env python3
import requests
# htpps://httpbin.org/basic-auth/foo/bar
# Con with arrastramos una sesión
with requests.Session() as session:
session.auth = ('foo', 'bar')
response1 = session.get('https://httpbin.org/basic-auth/foo/bar')
print(response1.text)
# En la segunda sesión estaremos autenticados
response2 = session.get('https://httpbin.org/basic-auth/foo/bar')
print(response2.text)

View File

@ -0,0 +1 @@
Hola, soy el contenido del archivo