Python Http Error 401
Contents |
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 Us Learn more about Stack Overflow the company Business Learn python urllib.error.httperror http error 401 unauthorized more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users python 3 urllib.error.httperror http error 401 unauthorized Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping python3 urllib.error.httperror: http error 401: unauthorized each other. Join them; it only takes 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 httppasswordmgrwithdefaultrealm 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 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
Urllib2 Python
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 30 '15 at 23:35 answered Dec 3 '13 at 22:22 Back2Basics 2,088920 ok, this gave me: 'requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:550)' any thoughts on how to deal with this? –user3063129 Dec 4 '13 at 0:04 wow! i have added "verify=False" on line 2, and i get as output "response [200]", i change my question to: how get the file after that? –user3063129 Dec 4 '13 at 0:26 I updated the post. –Back2Basics Dec 4 '13 at 7:56 add a comment| up vote 1 down vote
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the
Urllib Python 3
workings and policies of this site About Us Learn more about typeerror: 'bytes' object is not callable Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions python urllib3 Jobs Documentation 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. http://stackoverflow.com/questions/20361249/http-error-401-authorization-required-while-downloading-a-file-from-https-websi 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 run the script I am getting error as HTTP Error 401: Unauthorized. Please note that I http://stackoverflow.com/questions/12202156/http-basic-authentication-is-failing-in-python-script 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 https://docs.python.org/3/howto/urllib2.html in Python. urllib.request is a Python module for fetching URLs (Uniform Resource Locators). It offers http://www.gossamer-threads.com/lists/python/python/641061 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 http error 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. http error 401 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 reques
Post #1 of 4 (6406 views) Permalink Problem with urllib2 and authentification Using this script for connect to Zope I have this error ---script: import urllib2 protocolo='http://' servidor='10.28.1.239/' pagina='manage' fullurl=protocolo+servidor+pagina aut=urllib2.HTTPBasicAuthHandler() aut.add_password(realm=None, uri=servidor, user='myadmin', passwd='mypass') opener=urllib2.build_opener(aut, urllib2.HTTPHandler(debuglevel=1)) print opener.open(fullurl).read() ---Error: connect: (10.28.1.239, 80) send: 'GET /manage HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: 10.28.1.239\r\nConnection: close\r\nUser-agent: Python-urllib/2.4\r\n\r\n' reply: 'HTTP/1.1 401 Unauthorized\r\n' header: Server: Zope/(Zope 2.10.5-final, python 2.4.4, win32) ZServer/1.1 header: Date: Tue, 22 Apr 2008 14:14:45 GMT header: Bobo-Exception-Line: 713 header: Content-Length: 884 header: Bobo-Exception-Value: See the server error log for details header: Content-Type: text/html; charset=iso-8859-15 header: Bobo-Exception-Type: Unauthorized header: Connection: close header: Bobo-Exception-File: HTTPResponse.py header: WWW-Authenticate: basic realm="Zope" Traceback (most recent call last): File "z.py", line 15, in ? print opener.open(fullurl).read() 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/python2.4/urllib2.py", line 480, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 401: Unauthorized why not send authentification? I try python 2.5 on slackware 12 too on python 2.4 and 2.5 on windows xp All same error -- ________________________________________ Lo bueno de vivir un dia mas es saber que nos queda un dia menos de vida -- http://mail