Web

Web#

This page explores the concepts behind various Python packages related to the web.

Requests#

The Requests package provides a more convenient interfaces for urllib3. This package hides a lot of ConnectionPool management inside it, which makes it easier to understand, but reduces the flexibility.

Check more detailed explanation in the Requests page.


The following cell shows how to make a GET request to google.com in just two lines of code, including the import.

import requests
requests.get("https://google.com")
<Response [200]>

urllib3#

The urllib3 is a package for implementing network communication.

For more information:


The following cell shows how to make a request to the http://google.com using urllib3.

import urllib3
pool_manager = urllib3.PoolManager()
resp = pool_manager.request("GET", "http://google.com")
resp.status
200

Authlib#

Authlib is the package that implement OAuth and OpenID protocols in python.

Check more on the Authlib page.


The following cell show how to obtain the Bearer token with OAuth2.

Note. You must run the keycloak, which plays the role of OAuth2 provider.

from authlib.integrations.requests_client import OAuth2Session
client = OAuth2Session(client_id="admin-cli")
token = client.fetch_token(
    "http://localhost:8080/realms/master/protocol/openid-connect/token",
    username="admin",
    password="admin",
    grant_type="password",
)
token
{'access_token': 'eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJrTnA4VkMtYjB5RXh1N2VWQ21MTjNjdW5rWXVSTXExZFpkeUJQaU9WOWs4In0.eyJleHAiOjE3NzE3NzAzNDUsImlhdCI6MTc3MTc3MDI4NSwianRpIjoib25sdHJvOjUwNWZlNGFhLTg2ZjQtZjZjMi03MGEwLTJjZWFjNmEwYTJjNSIsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6ODA4MC9yZWFsbXMvbWFzdGVyIiwidHlwIjoiQmVhcmVyIiwiYXpwIjoiYWRtaW4tY2xpIiwic2lkIjoiZjZiZjExMzQtZTA0Yy1kOTliLWE3YjUtYjk5ZGQxNDhmOGZkIiwic2NvcGUiOiJlbWFpbCBwcm9maWxlIn0.ANicSYy3B6hoycEL1olJmsJ1h4UMCsus0X_lHtOs7sZb_EhMfRf5R_R03SD7Uwz8tT0Qp1Tww4dS58kyOb6b--QJz2WxS24t6yjUMJbH4NpQW-YzVsXwX_44VUBTHlNzKKeJDE_Wp-9UV7pXWMrGCtwzQxKrFf-QT3ULFHeYpFco71Xm3wer7n9H5LDYI2JEbUvToVKygPyuqg-MX14Xy4QkzUEcj6VZO6-R0-gWzMKAflj5EvTpb-NTl_ze-yzF6SXkhQaCTtZAYw4FW1qzvWVcvT2xB2kkqHStHUy1C5uU26wOgh9ekqyQYqFjph598HQvlUhCFYLkuZsEA8NNoA',
 'expires_in': 60,
 'refresh_expires_in': 1800,
 'refresh_token': 'eyJhbGciOiJIUzUxMiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJmODBlNDZjZS1iZTY3LTRmOTItOTMyMi1mZmE5MjJiYjViYjgifQ.eyJleHAiOjE3NzE3NzIwODUsImlhdCI6MTc3MTc3MDI4NSwianRpIjoiZjVkODI3NjAtMzBkYS01MmM2LWEwNDEtZDVmYTA0YmU5YmI5IiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgwL3JlYWxtcy9tYXN0ZXIiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjgwODAvcmVhbG1zL21hc3RlciIsInR5cCI6IlJlZnJlc2giLCJhenAiOiJhZG1pbi1jbGkiLCJzaWQiOiJmNmJmMTEzNC1lMDRjLWQ5OWItYTdiNS1iOTlkZDE0OGY4ZmQiLCJzY29wZSI6IndlYi1vcmlnaW5zIGVtYWlsIHByb2ZpbGUgcm9sZXMgYWNyIGJhc2ljIn0.wQ5cFFc2E72JcHLZIzx1wtqss0oM-E29m5FXo7l1o6wWHRHmMdQgWKai7sHtm9tycddrarZ7KmuN_a--9az8aA',
 'token_type': 'Bearer',
 'not-before-policy': 0,
 'session_state': 'f6bf1134-e04c-d99b-a7b5-b99dd148f8fd',
 'scope': 'email profile',
 'expires_at': 1771770346}

Note The token here is not just a dictionary, it is a special authlib object that represents the token.

type(token)
authlib.oauth2.rfc6749.wrappers.OAuth2Token