Typing stubs for pycurl

7.45.7.20260408 · active · verified Mon Apr 13

types-pycurl provides static type annotations (stubs) for the pycurl library, enabling static type checkers like MyPy and PyRight to analyze code that uses pycurl for network operations. It is part of the typeshed project, which centrally maintains type stubs for Python's standard library and many third-party packages. This package is automatically released, typically on a daily basis, to ensure compatibility with the corresponding pycurl runtime versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to make a basic HTTPS GET request using pycurl, capture the response body into a BytesIO object, and handle potential errors. It includes the recommended practice of using the `certifi` package to specify an up-to-date CA certificate bundle for secure connections. The `types-pycurl` package, when installed, provides type checking for `pycurl` usage in this code.

import pycurl
from io import BytesIO
import certifi
import os

buffer = BytesIO()
c = pycurl.Curl()

# Set the URL and write data to the buffer
c.setopt(pycurl.URL, 'https://example.com')
c.setopt(pycurl.WRITEDATA, buffer)

# Configure SSL certificates using certifi for secure connections
# (optional, but recommended for HTTPS)
if os.environ.get('PYCURL_CA_BUNDLE_PATH'):
    c.setopt(pycurl.CAINFO, os.environ.get('PYCURL_CA_BUNDLE_PATH'))
elif os.path.exists(certifi.where()):
    c.setopt(pycurl.CAINFO, certifi.where())

try:
    c.perform()
    body = buffer.getvalue().decode('utf-8')
    print(f"HTTP Status Code: {c.getinfo(pycurl.HTTP_CODE)}")
    print(f"Response body length: {len(body)} characters")
    # print(body[:500]) # Print first 500 characters of the body
except pycurl.error as e:
    # Troubleshooting tip: Enable verbose logging for more details
    # c.setopt(pycurl.VERBOSE, True)
    print(f"An error occurred: {e}")
finally:
    c.close()

view raw JSON →