7

how and with which python library is it possible to make an httprequest (https) with a user:password or a token?

basically the equivalent to curl -u user:pwd https://www.mysite.com/

thank you

4 Answers 4

4

use python requests : Http for Humans

import requests

requests.get("https://www.mysite.com/", auth=('username','pwd'))

you can also use digest auth...

Sign up to request clarification or add additional context in comments.

Comments

1

If you need to make thread-safe requests, use pycurl (the python interface to curl):

import pycurl
from StringIO import StringIO

response_buffer = StringIO()
curl = pycurl.Curl()

curl.setopt(curl.URL, "https://www.yoursite.com/")

# Setup the base HTTP Authentication.
curl.setopt(curl.USERPWD, '%s:%s' % ('youruser', 'yourpassword'))

curl.setopt(curl.WRITEFUNCTION, response_buffer.write)

curl.perform()
curl.close()

response_value = response_buffer.getvalue()

Otherwise, use urllib2 (see other responses for more info) as it's builtin and the interface is much cleaner.

Comments

0

class urllib2.HTTPSHandler A class to handle opening of HTTPS URLs.

21.6.7. HTTPPasswordMgr Objects These methods are available on HTTPPasswordMgr and HTTPPasswordMgrWithDefaultRealm objects.

HTTPPasswordMgr.add_password(realm, uri, user, passwd) uri can be either a single URI, or a sequence of URIs. realm, user and passwd must be strings. This causes (user, passwd) to be used as authentication tokens when authentication for realm and a super-URI of any of the given URIs is given. HTTPPasswordMgr.find_user_password(realm, authuri) Get user/password for given realm and URI, if any. This method will return (None, None) if there is no matching user/password.

For HTTPPasswordMgrWithDefaultRealm objects, the realm None will be searched if the given realm has no matching user/password.

Comments

0

Check our urllib2. The examples at the bottom will probably be of interest.

http://docs.python.org/library/urllib2.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.