CURL alternative in Python


I have a cURL call that I use in PHP:

curl -i -H 'Accept: application/xml' -u login:key ""

I need a way to do the same thing in Python. Is there an alternative to cURL in Python? I know of urllib but I have no idea how to use it.

Asked By: Gaurav Sharma



If you are using a command to just call curl like that, you can do the same thing in Python with subprocess. Example:['curl', '-i', '-H', '"Accept: application/xml"', '-u', 'login:key', '""'])

Or you could try PycURL if you want to have it as a more structured api like what PHP has.

Answered By: unholysampler
import urllib2

manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
manager.add_password(None, '', 'login', 'key')
handler = urllib2.HTTPBasicAuthHandler(manager)

director = urllib2.OpenerDirector()

req = urllib2.Request('', headers = {'Accept' : 'application/xml'})

result =
# will contain the data
# will contain the HTTP headers

# To get say the content-length header
length =['Content-Length']

Your cURL call using urllib2 instead. Completely untested.

Answered By: blwy10

Here’s a simple example using urllib2 that does a basic authentication against GitHub’s API.

import urllib2


# simple wrapper function to encode the username & pass
def encodeUserData(user, password):
    return "Basic " + (user + ":" + password).encode("base64").rstrip()

# create the request object and set some headers
req = urllib2.Request(url)
req.add_header('Accept', 'application/json')
req.add_header("Content-type", "application/x-www-form-urlencoded")
req.add_header('Authorization', encodeUserData(u, p))
# make the request and print the results
res = urllib2.urlopen(req)

Furthermore if you wrap this in a script and run it from a terminal you can pipe the response string to ‘mjson.tool’ to enable pretty printing.

>> | python -mjson.tool

One last thing to note, urllib2 only supports GET & POST requests.

If you need to use other HTTP verbs like DELETE, PUT, etc you’ll probably want to take a look at PYCURL

Answered By: braitsch

You can use the Requests library. Install it with

pip install requests

You can find its documentation at

Answered By: Gudbergur
import requests

url = 'https://example.tld/'
auth = ('username', 'password')

r = requests.get(url, auth=auth)

This is the simplest I’ve been able to get it.

Answered By: tadamhicks

Some example, how to use urllib for that things, with some sugar syntax. I know about requests and other libraries, but urllib is standard lib for python and doesn’t require anything to be installed separately.

Python 2/3 compatible.

import sys
if sys.version_info.major == 3:
  from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, Request, build_opener
  from urllib.parse import urlencode
  from urllib2 import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, Request, build_opener
  from urllib import urlencode

def curl(url, params=None, auth=None, req_type="GET", data=None, headers=None):
  post_req = ["POST", "PUT"]
  get_req = ["GET", "DELETE"]

  if params is not None:
    url += "?" + urlencode(params)

  if req_type not in post_req + get_req:
    raise IOError("Wrong request type "%s" passed" % req_type)

  _headers = {}
  handler_chain = []

  if auth is not None:
    manager = HTTPPasswordMgrWithDefaultRealm()
    manager.add_password(None, url, auth["user"], auth["pass"])

  if req_type in post_req and data is not None:
    _headers["Content-Length"] = len(data)

  if headers is not None:

  director = build_opener(*handler_chain)

  if req_type in post_req:
    if sys.version_info.major == 3:
      _data = bytes(data, encoding='utf8')
      _data = bytes(data)

    req = Request(url, headers=_headers, data=_data)
    req = Request(url, headers=_headers)

  req.get_method = lambda: req_type
  result =

  return {
    "httpcode": result.code,

Usage example:

Post data:
  curl("", req_type="POST", data='cascac')

Pass arguments (
  curl("", params={'q': 'show'}, req_type="POST", data='cascac')

HTTP Authorization:
  curl("", auth={"user": "username", "pass": "password"})

Function is not complete and possibly is not ideal, but shows a basic representation and concept to use. Additional things could be added or changed by taste.

12/08 update

Here is a GitHub link to live updated source.
Currently supporting:

  • authorization

  • CRUD compatible

  • automatic charset detection

  • automatic encoding(compression) detection

Answered By: Reishin

If it’s running all of the above from the command line that you’re looking for, then I’d recommend HTTPie. It is a fantastic cURL alternative and is super easy and convenient to use (and customize).

Here’s is its (succinct and precise) description from GitHub;

HTTPie (pronounced aych-tee-tee-pie) is a command line HTTP client.
Its goal is to make CLI interaction with web services as
human-friendly as possible.

It provides a simple http command that allows for sending arbitrary
HTTP requests using a simple and natural syntax, and displays
colorized output. HTTPie can be used for testing, debugging, and
generally interacting with HTTP servers.

The documentation around authentication should give you enough pointers to solve your problem(s). Of course, all of the answers above are accurate as well, and provide different ways of accomplishing the same task.

Just so you do NOT have to move away from Stack Overflow, here’s what it offers in a nutshell.

Basic auth:

$ http -a username:password
Digest auth:

$ http --auth-type=digest -a username:password
With password prompt:

$ http -a username

Answered By: stuxnetting
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.