Python HTTP requests

Requests API

Allow to send HTTP/1.1 requests.
No need to manually add query strings to your URLs, or to form-encode POST data.
Basic usage :

import requests
...
response = requests.get('https://api.github.com/events')

Session Object

A Session object has all the methods of the main Requests API.
Feature :
– persist some parameters across requests.
– persist cookies across all requests made from the Session instance
– use urllib3’s connection pooling (may improve performance).

Usage

    # create a session
    session = requests.Session()
 
    # set some persistent parameters
    # ex: set the certificate path
    session.verify = '/etc/foo/ca.crt'   
    # ex: set auth 
    session.auth = HTTPBasicAuth('username', 'password')
 
    # execute some requests
    # a first one
    resp = session.post('https://foo.bar/do')
    # a seconde one
    resp = session.get('https://foo.bar/get')

Send a JSON request

Processing a JSON response

The json() function : convert the response to a « json thing »

Just invoke json() on the response object:

resp = requests.get('http://foo.com/data')
jsonResult = resp.json()

Process a simple object returned as json

JSON simple objects (that is not an array) are converted into a dictionary.
Example with a json response as :

{'node': 'es02', 'disk.indices': '7.8gb', 'disk.percent': '84'}

We process it as any dictionary:

line = resp.json()
print('node=', line['node'])
print('disk.indices=', line['disk.indices'])
print('disk.percent=', line['disk.percent'])

Output :

node= es02
disk.indices= 7.9gb
disk.percent= 82

Process a array object returned as json

JSON array objects are converted into a list.
Element of the list (recursively) that are themselves array objects are again converted into a list.
Basic objects are converted to dictionary (as above). Example with a json response as :

[<br /> {"node":"es03","disk.indices":"10.9gb","disk.percent":"82"},<br /> {"node":"es02","disk.indices":"7.9gb","disk.percent":"82"},<br /> {"node":"es01","disk.indices":"3.3gb","disk.percent":"82"}<br />]

We process it as list at first level and dictionary at second level :

lines = resp.json()
for line in lines:
  print('new element found')
  print('node=', line['node'])
  print('disk.indices=', line['disk.indices'])
  print('disk.percent=', line['disk.percent'])

Output :

new element found
node= es01
disk.indices= 3.3gb
disk.percent= 82
new element found
node= es02
disk.indices= 7.9gb
disk.percent= 82
new element found
node= es03
disk.indices= 10.9gb
disk.percent= 82
 
Process finished with exit code 0

Common errors

Problem : protocol scheme is missing in the url.
Symptom : requests.get()/post()/... raise a InvalidSchema exception such as
requests.exceptions.InvalidSchema: No connection adapters were found for 'localhost:8000/foo'
Solution :
Prefix the url by the protocol : http, https…

Ce contenu a été publié dans Non classé. Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *