tags:

views:

440

answers:

3

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

+4  A: 

You can try pycurl

ghostdog74
+2  A: 

If you are using a command to just call curl like that, you can do the same thing in Python with subprocess. Example:

subprocess.call(['curl', '-i', '-H', '"Accept: application/xml"', '-u', 'login:key', '"https://app.streamsend.com/emails"'])

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

unholysampler
No. The cURL call is part of a program. If you could post the code that does the exact same thing being done in the curl call above, that would be great.
Gaurav
Added an example of what I meant by using subprocess based on your question, but I'm guessing you're looking for something more like PycURL.
unholysampler
+2  A: 
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.

blwy10