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] [-d DATA] [-H HEADER] [--json JSON] [-L] [-o OUTPUT]
[-X REQUEST] [-v]
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
-d DATA, --data DATA Add POST data
-H HEADER, --header HEADER
Add custom request header (format: `Header: Value`)
--json JSON Add JSON POST data
-L, --location Follow redirects
-o OUTPUT, --output OUTPUT
Write to file instead of stdout
-X REQUEST, --request REQUEST
Request command to use (e.g. HTTP method)
-v, --verbose Verbose logging
This can also be used with the Requests library if
PYCURLREQUESTS_REQUESTS environment variable is set to a non-null value.
Documentation
This library aims to be API compatible with Requests, thus the Requests documentation should be mostly applicable.
Adapters
PycURL support is implemented as a transport adapter. This means it's possible to use PycURL with the Requests library itself!
import pycurl
import requests
from pycurl_requests.adapters import PyCurlHttpAdapter
with requests.Session() as session:
curl = pycurl.Curl()
session.mount('https://', PyCurlHttpAdapter(curl))
session.mount('http://', PyCurlHttpAdapter(curl))
response = session.get('http://example.com')
cURL options
It is possible customize cURL's behaviour using the curl attribute on a
Session object.
For example, to make a request without requesting the body:
import pycurl
import pycurl_requests as requests
with requests.Session() as session:
session.curl.setopt(pycurl.NOBODY, 1)
response = session.get('http://example.com')
See the pycurl.Curl object documentation
for all possible curl attribute methods.
cURL exceptions
All pycurl.error exceptions
are mapped to a requests.RequestException
(or one of its subclasses).
For convenience, the original pycurl.error error message and
cURL error code will be set on the exception
object as the curl_message 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_message, 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 textcurl.header_in— Header data received from the peercurl.header_out— Header data sent to the peer
Known limitations
- No support for reading Cookies
- No support for client-side certificates
- No support for proxies
- No support for link headers (e.g.
Response.links) - No support for sending multi-part encoded files
- Basic support for
Sessionobjects (e.g.requests.Session)
License
Licensed under the MIT License.
Release files for pycurl-requests 0.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pycurl_requests-0.5.2.tar.gz | 26.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pycurl_requests-0.5.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 56.7 kB
Release files / pycurl_requests-0.5.2.tar.gz
| Download URL | pycurl_requests-0.5.2.tar.gz |
|---|---|
| Size | 26.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c75ada6d5164e8c18534d2fac14dd4c8e71e1e29b7b016d7d134c1f4764da394
|
|
BLAKE2b-256 checksum How to use checksums |
f51e80332b11d4bcea23ec36f6d04c37aea8cf3df7ea8aef5d17d9c06b085074
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|
Release files / pycurl_requests-0.5.2-py3-none-any.whl
| Download URL | pycurl_requests-0.5.2-py3-none-any.whl |
|---|---|
| Size | 30.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
39c9554ed46a237dcb2cfadd4b746aa53eb400ca55939367abc89b4728d59234
|
|
BLAKE2b-256 checksum How to use checksums |
0df9459fdc069632b14a5d780ace21f1b4d5297b1ea68860ed563ed62437e442
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|