Fix Python – CURL alternative in Python


Asked By – Gaurav Sharma

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.

Now we will see solution for issue: CURL alternative in Python


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.

This question is answered By – blwy10

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0