Reputation: 33
I just wanna ask how to make a connection [user+pass] with A page that give you 401 response.
For example in php its seem like that
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://192.168.1.1/');
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$pass);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
$result = curl_exec($ch);
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
Upvotes: 0
Views: 170
Reputation: 7328
If you're looking for something simple, the requests
library is as simple as it could get. Here is a simple example of basic authentication from the docs:
>>> requests.get('https://api.github.com/user', auth=('user', 'pass'))
<Response [200]>
Upvotes: 1
Reputation: 572
You're looking for the Keyword Basic HTTP Authentication.
I do not recommend using 3rd party modules if you won't be going further than doing that. If you will, the requests
library already suggested is a great choice.
The following example is taken from the urllib2 docs:
import urllib2
# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
# If we knew the realm, we could use it instead of None.
top_level_url = "http://example.com/foo/"
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)
# use the opener to fetch a URL
opener.open(a_url)
# Install the opener.
# Now all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)
Upvotes: 0
Reputation: 365845
There are three good options here.
First, you can use urllib2
(Python 2) or urllib
(Python 3), which are built in, and pretty easy to use.
Second, you can use an even easier third-party library like requests
. (Often, code that takes a dozen lines to write with curl
or urllib
is a two-liner with requests
.)
Finally, since you already know how to use php's low-level libcurl wrappers, there are a few different third-party alternatives for Python that are nearly identical. See this search, and look through pycurl
, pycurl2
, and pyclibcurl
to see which one feels most familiar.
Upvotes: 0
Reputation: 10740
You could use the urllib2 module - http://docs.python.org/2/library/urllib2.html
Upvotes: 0