Python Httperror Http Error 401 Authorization Required
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings python urllib.error.httperror http error 401 unauthorized and policies of this site About Us Learn more about Stack Overflow python 3 urllib.error.httperror http error 401 unauthorized the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation python3 urllib.error.httperror: http error 401: unauthorized Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it
Httppasswordmgrwithdefaultrealm()
only takes a minute: Sign up HTTP Basic Authentication Doesn't Seem to Work with urllib2 in python up vote 7 down vote favorite 4 I'm trying to download a page protected with basic authentication using urllib2. I'm using python 2.7 but I also tried it on another computer with python 2.5 and encountered the exact same behavior. I followed the typeerror: 'bytes' object is not callable example given in this guide as closely as I could and here is the code that I produced: import urllib2 passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, "http://authenticationsite.com/', "protected", "password") authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) f = opener.open("http://authenticationsite.com/content.html") print f.read() f.close() Unfortunately the server isn't mine so I can't share the details; I swapped them out above and below. When I run it I get the following Traceback: File "/usr/lib/python2.7/urllib2.py", line 397, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 510, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 435, in error return self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 518, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 401: Authorization Required Now, the interesting part is when I monitor the tcp traffic on the computer using ngrep: ngrep host 74.125.224.49 interface: wlan0 (192.168.1.0/255.255.255.0) filter: (ip) and ( host 74.125.224.49 ) #### T 192.168.1.74:34366 -74.125.224.49:80 [AP] GET /content.html HTTP/1.1..Accept-Encoding: identity..Host: authenticationsite.com..Connection: close..User-Agent: Python-urllib/2.7.... ## T 74.125.224.49:80 -192.168.1.74:34366 [AP] HTTP/1.1 401 Authorization Required..Date: Sun, 27 Feb 2011 03:39:3
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and python sharepoint unauthorized policies of this site About Us Learn more about Stack Overflow the
Python Urllib2 Authentication Example
company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags
Python Requests Download File
Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes http://stackoverflow.com/questions/5131403/http-basic-authentication-doesnt-seem-to-work-with-urllib2-in-python a minute: Sign up HTTP Error 401: Authorization Required while downloading a file from HTTPS website and saving it up vote 0 down vote favorite Basically i need a program that given a URL, it downloads a file and saves it. I know this should be easy but there are a couple of drawbacks here... First, it is part of a http://stackoverflow.com/questions/20361249/http-error-401-authorization-required-while-downloading-a-file-from-https-websi tool I'm building at work, I have everything else besides that and the URL is HTTPS, the URL is of those you would paste in your browser and you'd get a pop up saying if you want to open or save the file (.txt). Second, I'm a beginner at this, so if there's info I'm not providing please ask me. :) I'm using Python 3.3 by the way. I tried this: import urllib.request response = urllib.request.urlopen('https://websitewithfile.com') txt = response.read() print(txt) And I get: urllib.error.HTTPError: HTTP Error 401: Authorization Required Any ideas? Thanks!! python python-3.x authentication https share|improve this question edited Oct 31 '15 at 12:37 J.F. Sebastian 182k42337492 asked Dec 3 '13 at 20:42 user3063129 312 add a comment| 4 Answers 4 active oldest votes up vote 4 down vote accepted You can do this easily with the requests library. import requests response = requests.get('https://websitewithfile.com/text.txt',verify=False, auth=('user', 'pass')) print(response.text) to save the file you would type with open('filename.txt','w') as fout: fout.write(response.text): (I would suggest you always set verify=True in the resquests.get() command) Here is the documentation: share|improve this answer edited Oct 3
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About http://stackoverflow.com/questions/1554745/how-do-i-ask-for-an-authenticated-url-directly-with-python Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the http://ask.metafilter.com/67876/What-am-I-missing-re-pythonurllib2 Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How do I ask for http error an authenticated url directly with python up vote 0 down vote favorite I want to get to an authenticated page using urllib2. I'm hoping there's a hack to do it directly. something like: urllib2.urlopen('http://username:pwd@server/page') If not, how do I use authentication? python authentication urllib2 share|improve this question asked Oct 12 '09 at 13:57 Guy 4,033215177 add a comment| 2 Answers 2 active oldest votes up vote http error 401 2 down vote accepted It depends on the type of authentication used. A simple example is Http Authentication If the site uses cookies for auth you need to add a cookiejar and login over http there are many more auth schemes, so find out which you need. share|improve this answer answered Oct 12 '09 at 14:07 Jochen Ritzel 58.6k9113149 I'll use the example on the python doc –Guy Oct 12 '09 at 14:14 add a comment| up vote 1 down vote AFAIK, there isn't a trivial way of doing this. Basically, you make a request and the server responds with a 401 authorization required, which urllib2 translates into an exception. Traceback (most recent call last): File "
MetaFilter querying the hive mind Log In Sign Up MetaFilter AskMeFi FanFare Projects Music Jobs IRL MetaTalk More Best Of Podcast Chat Labs Search MetaFilter… Menu Home FAQ About Archives Tags Popular Random What am I missing re: python/urllib2? July 26, 2007 12:29 PM Subscribe Why can't I get python's urllib2 library to fetch a page using HTTP basic authentication? I'm playing around with python, and trying to use the urllib2 library to fetch a page that's password protected with a simple .htaccess file. From what I can tell the opener object, when it encounters a 401, is supposed to check the authentication object for a URI match, and if it finds one, retry the URL with the specified username/password. That doesn't seem to be happening and I'm not comfortable enough with python to know the best way to go about debugging this, and I'm not even sure I'm approaching this right (am I supposed to handle the 401 errors myself?) Working code is appreciated, but I'm clearly missing something obvious here, so an explanation of what's going on would be more appreciated. My Code:
#!/usr/local/bin/python
import urllib2
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, "alanstorm.com/testbed", 'admin','the-password')
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
f = opener.open("http://alanstorm.com/testbed/password-test/test.txt")
print(f.read());
The page http://alanstorm.com/testbed/password-test/test.txt Username: admin Password: the-password the .htaccess
AuthUserFile /path/to/htpass
AuthName grrrr
AuthType Basic
require user admin
The Error Message
Traceback (most recent call last):
File "./my-script.py", line 34, in ?
f = opener.open("http://alanstorm.com/testbed/password-test/test.txt")
File "/usr/local/lib/python2.4/urllib2.py", line 364, in open
response = meth(req, response)
File "/usr/local/lib/python2.4/urllib2.py", line 471, in http_response
response = self.parent.error(
File "/usr/local/lib/python2.4/urllib2.py", line 402, in error
return self._call_chain(*args)
File "/usr/local/lib/python2.4/urllib2.py", line 337, in _call_chain
result = func(*args)
File "/usr/local/lib/pyth