Gaurav Sharma Gaurav Sharma - 1 year ago 213
Python Question

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.

Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download