Curso-lenguaje-python/python-ofensivo/08_librerias/03_requests_11_sesion_with.py

18 lines
438 B
Python

#!/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)