Question
Asked By – Gaurav Sharma
I have a cURL call that I use in PHP:
curl -i -H 'Accept: application/xml' -u login:key "https://app.streamsend.com/emails"
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
Answer
import urllib2
manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
manager.add_password(None, 'https://app.streamsend.com/emails', 'login', 'key')
handler = urllib2.HTTPBasicAuthHandler(manager)
director = urllib2.OpenerDirector()
director.add_handler(handler)
req = urllib2.Request('https://app.streamsend.com/emails', headers = {'Accept' : 'application/xml'})
result = director.open(req)
# result.read() will contain the data
# result.info() will contain the HTTP headers
# To get say the content-length header
length = result.info()['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