Python Urllib2 Http Error 401 Authorization Required
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you python http error 401: unauthorized might have Meta Discuss the workings and policies of this site python urllib.error.httperror http error 401 unauthorized About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or
Python 3 Urllib.error.httperror Http Error 401 Unauthorized
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of
Python3 Urllib.error.httperror: Http Error 401: Unauthorized
6.2 million programmers, just like you, helping each other. Join them; it 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 httppasswordmgrwithdefaultrealm() also tried it on another computer with python 2.5 and encountered the exact same behavior. I followed the 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
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
Typeerror: 'bytes' Object Is Not Callable
site About Us Learn more about Stack Overflow the company Business Learn more python sharepoint unauthorized about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss python urllib2 authentication example 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 a minute: Sign up How do I http://stackoverflow.com/questions/5131403/http-basic-authentication-doesnt-seem-to-work-with-urllib2-in-python ask for 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 http://stackoverflow.com/questions/1554745/how-do-i-ask-for-an-authenticated-url-directly-with-python oldest votes up vote 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 "
here for a quick overview of the site Help Center Detailed answers to any questions http://stackoverflow.com/questions/12202156/http-basic-authentication-is-failing-in-python-script you might have Meta Discuss the workings and policies of this https://docs.python.org/3/howto/urllib2.html site About 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 Stack Overflow Community Stack Overflow is a http error community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up HTTP Basic Authentication is failing in python script up vote 1 down vote favorite I am trying to connect to a REST resource and retrieve the data using Python script (Python 3.2.3). When I http error 401 run the script I am getting error as HTTP Error 401: Unauthorized. Please note that I am able to access the given REST resource using REST client using Basic Authentication. In the REST Client I have specified the hostname, user and password details (realm is not required). Below is the code and complete error. Your help is very much appreciated. Code: import urllib.request # set up authentication info auth_handler = urllib.request.HTTPBasicAuthHandler() auth_handler.add_password(realm=None, uri=r'http://hostname/', user='administrator', passwd='administrator') opener = urllib.request.build_opener(auth_handler) urllib.request.install_opener(opener) res = opener.open(r'http://hostname:9004/apollo-api/nodes') nodes = res.read() Error Traceback (most recent call last): File "C:\Python32\scripts\get-nodes.py", line 12, in
revision of this HOWTO, available at urllib2 - Le Manuel manquant. Introduction¶ Related Articles You may also find useful the following article on fetching web resources with Python: Basic Authentication A tutorial on Basic Authentication, with examples in Python. urllib.request is a Python module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols. It also offers a slightly more complex interface for handling common situations - like basic authentication, cookies, proxies and so on. These are provided by objects called handlers and openers. urllib.request supports fetching URLs for many "URL schemes" (identified by the string before the ":" in URL - for example "ftp" is the URL scheme of "ftp://python.org/") using their associated network protocols (e.g. FTP, HTTP). This tutorial focuses on the most common case, HTTP. For straightforward situations urlopen is very easy to use. But as soon as you encounter errors or non-trivial cases when opening HTTP URLs, you will need some understanding of the HyperText Transfer Protocol. The most comprehensive and authoritative reference to HTTP is RFC 2616. This is a technical document and not intended to be easy to read. This HOWTO aims to illustrate using urllib, with enough detail about HTTP to help you through. It is not intended to replace the urllib.request docs, but is supplementary to them. Fetching URLs¶ The simplest way to use urllib.request is as follows: import urllib.request with urllib.request.urlopen('http://python.org/') as response: html = response.read() If you wish to retrieve a resource via URL and store it in a temporary location, you can do so via the urlretrieve() function: import urllib.request local_filename, headers = urllib.request.urlretrieve('http://python.org/') html = open(local_filename) Many uses of urllib will be that simple (note that instead of an ‘http:' URL we could have used a URL starting with ‘ftp:', ‘file:', etc.). However, it's the purpose of this tutorial to explain the more complicated cases, concentrating on HTTP. HTTP is based on requests and responses - the client makes requests and servers send responses. urllib.request mirrors this with a Request object which represents the HTTP request you are making. In its simplest form you create a Request object that specifies the URL you want to fetch. Calling urlopen with this Request object returns a response object for the URL request