HttpAlchemy is a Python library that provides a curl interface for interacting with Python HTTP client libraries like requests.
$ pip install httpalchemy
>>> from httpalchemy import curl
>>> response = curl("http://google.com")
>>> response
<CurlResponse [301 Moved Permanently]>
>>> decoded_body = response.text
>>> raw_body = response.content
>>> response.encoding
'UTF-8'
>>> response.raw # get raw response, `requests` library response by default
<Response [301]>
HTTPAlchemy uses the curl syntax.
There are several rules to follow.
- Options such as "-X" are converted to "_X"
- Options such as "--header" are converted to "__header"
curl http://example.com -d "test_data" # curl
curl("http://example.com", _d="test_data") # HTTPAlchemy
curl http://example.com -d "{'test': 'test'}" -H 'Content-Type: application/json' # curl
curl("http://example.com", _d="test_data", _H=["Content-Type: application/json"]) # HTTPAlchemy
curl ... -H "header1: value1" -H "header2: value2"
curl(..., _H=[("header1", "value1"), ("header2", "value2")])
HTTPAlchemy can accept headers in one of the following formats.
curl(_H={"header1": "value1", "header2": "value2"})
curl(_H=[("header1", "value1"), ("header2", "value2")])
curl(_H=["header1: value1", "header2: value2"])
HTTPAlchemy provides proxy support via the http_proxy
and https_proxy
environment variables.
$ export http_proxy="http://example.com"
$ export https_proxy="https://example.com"
$ python script.py