PycURL - A Python Interface To The cURL library

7.45.7 · active · verified Thu Apr 09

PycURL is a Python interface to the `libcurl` library, providing fast and feature-rich capabilities for network operations. It supports numerous protocols including HTTP, HTTPS, FTP, and more, exposing most of `libcurl`'s functionality like SSL, authentication, and proxy options. PycURL is often chosen for performance-critical applications and I/O multiplexing due to its speed. The current version is 7.45.7, with releases occurring periodically to incorporate `libcurl` updates and security fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to perform a basic GET request using PycURL. It initializes a Curl object, sets the URL, and uses `io.BytesIO` to capture the response body. Error handling for PycURL specific errors is included. It also highlights an optional step for using `certifi` for robust HTTPS certificate verification.

import pycurl
from io import BytesIO

def fetch_url(url):
    buffer = BytesIO()
    c = pycurl.Curl()
    c.setopt(c.URL, url)
    c.setopt(c.WRITEDATA, buffer)
    # Optional: For HTTPS, use certifi for certificate bundle
    # import certifi
    # c.setopt(c.CAINFO, certifi.where())
    
    try:
        c.perform()
        status_code = c.getinfo(pycurl.RESPONSE_CODE)
        response_body = buffer.getvalue().decode('utf-8', errors='ignore')
        print(f"Status Code: {status_code}")
        print(f"Response Body (first 200 chars):\n{response_body[:200]}")
    except pycurl.error as e:
        print(f"PycURL error: {e}")
    finally:
        c.close()

# Example usage
fetch_url('https://httpbin.org/get')

view raw JSON →