authentication with urllib3
Asked Answered
L

2

16

I am trying to connect to a webpage using urllib3. The code is provided below.

import urllib3
http=urllib3.PoolManager()
fields={'username':'abc','password':'xyz'}
r=http.request('GET',url,fields)

If we assume that url is some webpage which needs to be authenticated using username and password, am i using the right code to authenticate ?

I have did this using urllib2 very comfortably but i was not able to do the same thing using urllib3.

Many Thanks

Lolland answered 4/7, 2012 at 21:58 Comment(0)
D
36

Assuming you're trying to do Basic Authentication, then you need to put the username and password encoded in an Authorization header. Here's one way to do that using the urllib3.make_headers helper:

import urllib3

http = urllib3.PoolManager()
url = '...'
headers = urllib3.make_headers(basic_auth='abc:xyz')
r = http.request('GET', url, headers=headers)
Dehlia answered 9/7, 2012 at 2:56 Comment(5)
When using make_headers, how do I set a 'Cache-Control' header? If that is not possible, how do I construct a header with both authentication and caching control?Tonie
I guess is like this: headers = {'Authorization':'Basic %s' % b64encode('user:pass'), 'Cache-Control':'no-cache,max-age=0', 'Pragma':'no-cache'}Tonie
@Tonie Our make_headers helpers doesn't have a mode for modifying the cache control. I would be +1 to adding something like this if you'd be interested in making a PR. :) (Opened an issue here github.com/shazow/urllib3/issues/393)Dehlia
@Tonie could try headers = {'Cache-Control': 'no-cache,max-age=0',...} then use update to pull-in make_headers generated dictionary, eg headers.update(urllib3.util.make_headers(basic_auth = 'abc:123'))Wounded
i got this error : HTTPConnectionPool(host='0.0.0.0', port=5555): Max retries exceeded with url: /dashboard?json=1 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f166f8daa20>: Failed to establish a new connection: [Errno 111] Connection refused'))Involute
N
-5

Below is a working example to authenticate to an API using the requests library (ubuntu with python 3.6). Hope it helps!

import json
import requests
from requests.auth import HTTPBasicAuth

def __init__(self):
    header = {"Content-Type": "application/json"}
    access_url = "https://your.login.url/context_path"
    data = {
    "key_to_send":"value_to_send"
    }

def get_token(self):
    self.data = json.dumps(self.data)
    encoded_data = json.dumps(self.data).encode('utf-8')
    self.response = requests.post(self.access_url, auth=HTTPBasicAuth(self.username, self.password), headers=self.header, data=self.data)
    # Show me what you found
    print(self.response.text)
Ninepins answered 17/8, 2020 at 18:4 Comment(4)
Please add some explanations on how your snippet attempts to answer the question. I also recommend reading how to answer a question.Elconin
it also seems like your code here is to use the "requests" lib. you imported the "urllib3" lib but your code wasn't really using that...Collective
Thanks Jove - I've removed that line (must have left it in for testing)Ninepins
Thank you, HTTPBasicAuth worked for me.Mcbroom

© 2022 - 2024 — McMap. All rights reserved.