14

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

Mike Woodhouse
  • 51,832
  • 12
  • 88
  • 127
kich
  • 734
  • 2
  • 9
  • 23

2 Answers2

33

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)
yurez
  • 2,826
  • 1
  • 28
  • 22
shazow
  • 17,147
  • 1
  • 34
  • 35
  • 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? – kassold May 20 '14 at 16:53
  • I guess is like this: headers = {'Authorization':'Basic %s' % b64encode('user:pass'), 'Cache-Control':'no-cache,max-age=0', 'Pragma':'no-cache'} – kassold May 20 '14 at 17:07
  • 1
    @kassold 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 https://github.com/shazow/urllib3/issues/393) – shazow May 20 '14 at 22:23
  • 2
    @kassold 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'))` – S0AndS0 Feb 25 '19 at 08:45
  • i got this error : `HTTPConnectionPool(host='0.0.0.0', port=5555): Max retries exceeded with url: /dashboard?json=1 (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))` – Budi Mulyo Mar 20 '19 at 03:45
-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)
Nigel
  • 59
  • 4
  • 6
    Please add some explanations on how your snippet attempts to answer the question. I also recommend reading [how to answer a question](https://www.stackoverflow.com/help/how-to-answer). – ndrwnaguib Aug 17 '20 at 20:00
  • 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... – Jove Kuang Apr 05 '21 at 21:39
  • Thanks Jove - I've removed that line (must have left it in for testing) – Nigel Apr 07 '21 at 11:22
  • 1
    Thank you, HTTPBasicAuth worked for me. – reza_khalafi Feb 02 '22 at 07:49