Skip to content

Commit d11d4fc

Browse files
author
Cursor
committed
Add pycurl extension for proxy header support
- Add ProxyCurl class wrapping pycurl with proxy header capabilities - Support sending custom headers to proxy via CURLOPT_PROXYHEADER - Capture proxy CONNECT response headers via HEADERFUNCTION callback - Add convenience functions (get, post, etc.) matching existing API style - Add documentation in docs/pycurl.rst
1 parent e70bf3d commit d11d4fc

3 files changed

Lines changed: 488 additions & 0 deletions

File tree

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ We currently provide extensions to the following packages:
1414
* :doc:`requests <requests>` - Simple HTTP library for Python
1515
* :doc:`aiohttp <aiohttp>` - Async HTTP client/server framework
1616
* :doc:`httpx <httpx>` - Modern HTTP client library
17+
* :doc:`pycurl <pycurl>` - Python interface to libcurl
1718

1819
Purpose
1920
-------
@@ -50,6 +51,7 @@ Contents
5051
requests
5152
aiohttp
5253
httpx
54+
pycurl
5355

5456
Indices and tables
5557
==================

docs/pycurl.rst

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
PycURL
2+
======
3+
4+
The ``pycurl_proxy`` module provides proxy header support for PycURL.
5+
6+
Installation
7+
------------
8+
9+
First, install PycURL::
10+
11+
pip install pycurl
12+
13+
Then you can use the proxy header extension.
14+
15+
Usage
16+
-----
17+
18+
Using the ProxyCurl Class
19+
~~~~~~~~~~~~~~~~~~~~~~~~~
20+
21+
The ``ProxyCurl`` class wraps pycurl to provide easy proxy header handling:
22+
23+
.. code-block:: python
24+
25+
from python_proxy_headers.pycurl_proxy import ProxyCurl
26+
27+
# Create a ProxyCurl instance with proxy headers
28+
curl = ProxyCurl(proxy_headers={'X-ProxyMesh-Country': 'US'})
29+
30+
# Make a request through a proxy
31+
response = curl.get(
32+
'https://httpbin.org/ip',
33+
proxy='http://user:pass@proxy.example.com:8080'
34+
)
35+
36+
# Access the response
37+
print(response.status_code)
38+
print(response.text)
39+
40+
# Access headers from the proxy's CONNECT response
41+
print(response.proxy_headers)
42+
print(response.proxy_status_code)
43+
44+
Using Convenience Functions
45+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
46+
47+
For one-off requests, use the module-level functions:
48+
49+
.. code-block:: python
50+
51+
from python_proxy_headers import pycurl_proxy
52+
53+
response = pycurl_proxy.get(
54+
'https://httpbin.org/ip',
55+
proxy='http://proxy.example.com:8080',
56+
proxy_headers={'X-Custom-Header': 'value'}
57+
)
58+
59+
print(response.text)
60+
print(response.proxy_headers)
61+
62+
API Reference
63+
-------------
64+
65+
ProxyCurl Class
66+
~~~~~~~~~~~~~~~
67+
68+
.. py:class:: ProxyCurl(proxy_headers=None)
69+
70+
PycURL wrapper with proxy header support.
71+
72+
:param proxy_headers: Dict of headers to send to the proxy server
73+
74+
.. py:method:: request(method, url, proxy=None, proxy_headers=None, headers=None, data=None, timeout=None, verify=True)
75+
76+
Make an HTTP request with proxy header support.
77+
78+
:param method: HTTP method (GET, POST, etc.)
79+
:param url: Target URL
80+
:param proxy: Proxy URL (e.g., 'http://user:pass@proxy:8080')
81+
:param proxy_headers: Headers to send to the proxy (merged with instance headers)
82+
:param headers: Headers to send to the origin server
83+
:param data: Request body for POST/PUT
84+
:param timeout: Request timeout in seconds
85+
:param verify: Whether to verify SSL certificates
86+
:returns: ProxyResponse object
87+
88+
.. py:method:: get(url, **kwargs)
89+
.. py:method:: post(url, **kwargs)
90+
.. py:method:: put(url, **kwargs)
91+
.. py:method:: delete(url, **kwargs)
92+
.. py:method:: head(url, **kwargs)
93+
.. py:method:: options(url, **kwargs)
94+
.. py:method:: patch(url, **kwargs)
95+
96+
ProxyResponse Class
97+
~~~~~~~~~~~~~~~~~~~
98+
99+
.. py:class:: ProxyResponse
100+
101+
Response object containing body and headers from both proxy and origin.
102+
103+
.. py:attribute:: status_code
104+
:type: int
105+
106+
HTTP status code from the origin server.
107+
108+
.. py:attribute:: headers
109+
:type: dict
110+
111+
Headers from the origin server response.
112+
113+
.. py:attribute:: content
114+
:type: bytes
115+
116+
Response body as bytes.
117+
118+
.. py:attribute:: proxy_headers
119+
:type: dict
120+
121+
Headers from the proxy's CONNECT response (HTTPS only).
122+
123+
.. py:attribute:: proxy_status_code
124+
:type: int or None
125+
126+
Status code from the proxy's CONNECT response (HTTPS only).
127+
128+
.. py:attribute:: text
129+
:type: str
130+
131+
Response body decoded as UTF-8.
132+
133+
.. py:method:: raise_for_status()
134+
135+
Raise an exception if the status code indicates an error.

0 commit comments

Comments
 (0)