| 
									
										
										
										
											2008-06-18 20:49:58 +00:00
										 |  |  | """Exception classes raised by urllib.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-25 16:47:37 +02:00
										 |  |  | The base exception class is URLError, which inherits from OSError.  It | 
					
						
							| 
									
										
										
										
											2008-06-18 20:49:58 +00:00
										 |  |  | doesn't define any behavior of its own, but is the base class for all | 
					
						
							|  |  |  | exceptions defined in this package. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HTTPError is an exception class that is also a valid HTTP response | 
					
						
							|  |  |  | instance.  It behaves this way because HTTP protocol errors are valid | 
					
						
							|  |  |  | responses, with a status code, headers, and a body.  In some contexts, | 
					
						
							|  |  |  | an application may want to handle an exception like a regular | 
					
						
							|  |  |  | response. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import urllib.response | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-01 23:20:31 +08:00
										 |  |  | __all__ = ['URLError', 'HTTPError', 'ContentTooShortError'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-25 16:47:37 +02:00
										 |  |  | class URLError(OSError): | 
					
						
							|  |  |  |     # URLError is a sub-type of OSError, but it doesn't share any of | 
					
						
							| 
									
										
										
										
											2008-06-18 20:49:58 +00:00
										 |  |  |     # the implementation.  need to override __init__ and __str__. | 
					
						
							| 
									
										
										
										
											2017-04-16 10:46:38 +03:00
										 |  |  |     # It sets self.args for compatibility with other OSError | 
					
						
							| 
									
										
										
										
											2008-06-18 20:49:58 +00:00
										 |  |  |     # subclasses, but args doesn't have the typical format with errno in | 
					
						
							|  |  |  |     # slot 0 and strerror in slot 1.  This may be better than nothing. | 
					
						
							|  |  |  |     def __init__(self, reason, filename=None): | 
					
						
							|  |  |  |         self.args = reason, | 
					
						
							|  |  |  |         self.reason = reason | 
					
						
							|  |  |  |         if filename is not None: | 
					
						
							|  |  |  |             self.filename = filename | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __str__(self): | 
					
						
							|  |  |  |         return '<urlopen error %s>' % self.reason | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 18:35:54 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-18 20:49:58 +00:00
										 |  |  | class HTTPError(URLError, urllib.response.addinfourl): | 
					
						
							|  |  |  |     """Raised when HTTP error occurs, but also acts like non-error return""" | 
					
						
							|  |  |  |     __super_init = urllib.response.addinfourl.__init__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, url, code, msg, hdrs, fp): | 
					
						
							|  |  |  |         self.code = code | 
					
						
							|  |  |  |         self.msg = msg | 
					
						
							|  |  |  |         self.hdrs = hdrs | 
					
						
							|  |  |  |         self.fp = fp | 
					
						
							|  |  |  |         self.filename = url | 
					
						
							|  |  |  |         # The addinfourl classes depend on fp being a valid file | 
					
						
							|  |  |  |         # object.  In some cases, the HTTPError may not have a valid | 
					
						
							|  |  |  |         # file object.  If this happens, the simplest workaround is to | 
					
						
							|  |  |  |         # not initialize the base classes. | 
					
						
							|  |  |  |         if fp is not None: | 
					
						
							|  |  |  |             self.__super_init(fp, hdrs, url, code) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __str__(self): | 
					
						
							|  |  |  |         return 'HTTP Error %s: %s' % (self.code, self.msg) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 18:35:54 -03:00
										 |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         return '<HTTPError %s: %r>' % (self.code, self.msg) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-07 10:50:32 -05:00
										 |  |  |     # since URLError specifies a .reason attribute, HTTPError should also | 
					
						
							|  |  |  |     #  provide this attribute. See issue13211 for discussion. | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def reason(self): | 
					
						
							|  |  |  |         return self.msg | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-10 02:09:35 -08:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def headers(self): | 
					
						
							|  |  |  |         return self.hdrs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @headers.setter | 
					
						
							|  |  |  |     def headers(self, headers): | 
					
						
							|  |  |  |         self.hdrs = headers | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 18:35:54 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-18 20:49:58 +00:00
										 |  |  | class ContentTooShortError(URLError): | 
					
						
							| 
									
										
										
										
											2015-04-22 18:35:54 -03:00
										 |  |  |     """Exception raised when downloaded size does not match content-length.""" | 
					
						
							| 
									
										
										
										
											2008-06-18 20:49:58 +00:00
										 |  |  |     def __init__(self, message, content): | 
					
						
							|  |  |  |         URLError.__init__(self, message) | 
					
						
							|  |  |  |         self.content = content |