0

To make a get request with Python I was using urllib.request.urlopen(url) but I am having problem passing username and password to http://httpbin.org/basic-auth/ali/pass

enter image description here

import urllib.request
data={"user": "ali","passwd":"pass"}

data = urllib.parse.urlencode(data)
data = data.encode('utf-8')

url = "http://httpbin.org/basic-auth/ali/pass"
response = urllib.request.Request(url,data=data)

print(urllib.request.urlopen(response))
AlixaProDev
  • 472
  • 1
  • 5
  • 13
  • Does this answer your question? [Login on a site using urllib](https://stackoverflow.com/questions/29048168/login-on-a-site-using-urllib) – Alex D Feb 07 '22 at 16:38
  • No it did not help. Actually I am having problem with auth. I just have to make a basic auth request. I am tring that dummy website. as i am learning. – AlixaProDev Feb 07 '22 at 16:52

1 Answers1

1

I tried using requests module which work perfectly but I am trying to learn about urllib.request module so that is why.

from requests.auth import HTTPBasicAuth
import requests
r=requests.get('http://httpbin.org/basic-auth/ali/pass', auth=HTTPBasicAuth('ali', 'pass'))
print(r.status_code)
AlixaProDev
  • 472
  • 1
  • 5
  • 13