Python Urllib2.urlopen Error 500
Contents |
here for a quick overview of the site Help urllib2.httperror http error 500 internal server error python Center Detailed answers to any questions you might have Meta
Python Httperror Exception
Discuss the workings and policies of this site About Us Learn more about Stack urllib python 3 Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask
Python Requests
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 a minute: Sign up 500 error with urllib.request.urlopen up vote 5 down vote favorite 2 The following code: req = urllib.request.Request(url=r"http://borel.slu.edu/cgi-bin/cc.cgi?foirm_ionchur=im&foirm=Seol&hits=1&format=xml",headers={'User-Agent':' Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'}) handler = urllib.request.urlopen(req) is giving me the following exception: Traceback (most recent call last): File "C:/Users/Foo/lang/old/test.py", line 46, in
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 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 community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Overriding urllib2.HTTPError or urllib.error.HTTPError and reading response HTML anyway up vote 68 down http://stackoverflow.com/questions/15912257/500-error-with-urllib-request-urlopen vote favorite 18 I receive a 'HTTP Error 500: Internal Server Error' response, but I still want to read the data inside the error HTML. With Python 2.6, I normally fetch a page using: import urllib2 url = "http://google.com" data = urllib2.urlopen(url) data = data.read() When attempting to use this on the failing URL, I get the exception urllib2.HTTPError: urllib2.HTTPError: HTTP Error 500: Internal Server Error How can I http://stackoverflow.com/questions/2233687/overriding-urllib2-httperror-or-urllib-error-httperror-and-reading-response-html fetch such error pages (with or without urllib2), all while they are returning Internal Server Errors? Note that with Python 3, the corresponding exception is urllib.error.HTTPError. python urllib2 urllib http-error share|improve this question edited Oct 7 at 4:36 A-B-B 8,35033331 asked Feb 10 '10 at 0:55 Backus 74621227 add a comment| 3 Answers 3 active oldest votes up vote 131 down vote accepted The HTTPError is a file-like object. You can catch it and then read its contents. try: resp = urllib2.urlopen(url) contents = resp.read() except urllib2.HTTPError, error: contents = error.read() share|improve this answer answered Feb 10 '10 at 1:18 Joe Holloway 15.5k125881 2 Once we have done error.read(), error.read() subsequently returns empty string. Sometimes this messes up code elsewhere. How can we politely put the contents of the error back for others? –Colonel Panic Apr 9 '11 at 21:07 1 @Matt I've never tried this, but since it's a file-like object, you might be able to do a error.seek(0) to reset the "file pointer" to the beginning of the stream. Not every file-like object is required to implement the random access portion of the I/O interface, so not sure if it works. If it doesn't, you might consider asking this in its own q
revision of this HOWTO, available at urllib2 - Le Manuel manquant. Introduction¶ Related Articles You may also find useful the following article on fetching web https://docs.python.org/3/howto/urllib2.html 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 https://bytes.com/topic/python/answers/32021-urllib2-urlopen-req-error 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 error 500 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 python urllib2.urlopen error 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
question and get tips & solutions from a community of 418,626 IT Pros & Developers. It's quick & easy. urllib2.urlopen(req) error........ P: n/a John F Dutcher Can anyone comment on why the code shown in the Python error is in some way incorrect...or is there a problem with Python on my hoster's site ?? The highlites don't seem to show here...but line #80 and line # 38 are the first line offenders. --> --> --> HTTPError Python 2.2.2: /usr/bin/python Fri Jun 4 13:58:00 2004 A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. /home/euromill/public_html/scgi-bin/euro3.py 78 write_dig_err(ls) 79 x = x + 1 80 process_request(ls) 81 82 process_request =