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'm a Python noob and have no idea how to use it.

4/19/2010 12:55:12 PM

Accepted Answer

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.

4/19/2010 2:24:02 PM

You can use HTTP Requests that are described in the Requests: HTTP for Humans user guide.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow