Skip to main content

A Requests-compatible interface for pycURL

Project description

PycURL Requests <pycurl://☤>

PycURL Requests is a Requests-compatible interface for PycURL.

Requirements

Installation

Latest release via pip:

pip install pycurl-requests [--user]

via Git:

git clone https://github.com/dcoles/pycurl-requests.git; cd pycurl-requests
python3 setup.py install [--user]

Quick-start

>>> import pycurl_requests as requests
>>> r = requests.get('https://api.github.com/repos/dcoles/pycurl-requests')
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf-8'
>>> r.encoding
'utf-8'
>>> r.text
'{\n  "id": 236427187,\n...'
>>> data = r.json()
>>> data['name']
'pycurl-requests'
>>> data['html_url']
'https://github.com/dcoles/pycurl-requests'
>>> data['description']
'A Requests-compatible interface for pycURL'

The library can also be used to run existing Python scripts that import the requests module. By running the script through the pycurl_requests helper, any use of the requests module will be automatically redirected to pycurl_requests.

python3 -m pycurl_requests -- script.py arg arg...

request tool

A basic curl-like command-line utility is included:

usage: request.py [-h] [-o OUTPUT] [-X REQUEST] [-H HEADER] [-d DATA]
                  [--json JSON]
                  url

A basic `curl`-like command-line HTTP utility

positional arguments:
  url                   URL of resource to connect to

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Write to file instead of stdout
  -X REQUEST, --request REQUEST
                        Request command to use (e.g. HTTP method)
  -H HEADER, --header HEADER
                        Add custom request header (format: `Header: Value`)
  -d DATA, --data DATA  Add POST data
  --json JSON           Add JSON POST data

This can also be used with the Requests library if REQUEST_REQUESTS environment variable is set to a non-zero value.

Documentation

This library aims to be API compatible with Requests, thus the Requests documentation should be mostly applicable.

cURL options

It is possible customize the behaviour of cURL by passing a custom pycurl.Curl handle into the request-like functions.

For example, to make a request without getting the body:

import pycurl
import pycurl_requests as requests

c = pycurl.Curl()
c.setopt(c.NOBODY, 1)
response = requests.get('http://example.com', curl=c)

cURL exceptions

All pycurl.error exceptions are mapped to a requests.RequestException (or one of its subclasses).

For convenience, the original pycurl.error's error string and cURL error code will be set on the exception object as the curl_error and curl_code attributes.

import pycurl_requests as requests

try:
    requests.get('http://connect_error')
except requests.RequestException as e:
    print('ERROR: {} (cURL error: {})'.format(e.curl_error, e.curl_code))

It is also possible to obtain the original pycurl.error using the __cause__ attribute.

Logging

Detailed log records from libcurl, including informational text and HTTP headers, can be shown by setting the curl logger (or sub-loggers) to DEBUG level:

import logging

logging.getLogger('curl').setLevel(logging.DEBUG)

Log records are split into dedicated sub-loggers for each type of record:

  • curl.text — Informational text
  • curl.header_in — Header data received from the peer
  • curl.header_out — Header data sent to the peer

Known limitations

Most of these features should be supported in the near future.

License

Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycurl-requests-0.0.3.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycurl_requests-0.0.3-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file pycurl-requests-0.0.3.tar.gz.

File metadata

  • Download URL: pycurl-requests-0.0.3.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

File hashes

Hashes for pycurl-requests-0.0.3.tar.gz
Algorithm Hash digest
SHA256 3402c8111d32ae3cc31362af8f979bb25df14f2978a97e080b861db7c9c0c07b
MD5 3c0ebc45030db63fad94b83ca9e35759
BLAKE2b-256 1566ca6248ad551e09f153bef77a7a0b7f058490dfe03aeb568e0dff0079f448

See more details on using hashes here.

File details

Details for the file pycurl_requests-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pycurl_requests-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

File hashes

Hashes for pycurl_requests-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c6a31e3197d8dfac15d387a7286ee136990a4cc06227fc8dc4bf9eb53bb72bc3
MD5 82b3f8b982b355aa9cbb12642b1b528c
BLAKE2b-256 8d6251dc6a4344181fcfad7cf96d73316c75a5aed411724da38188c85d4e35a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page