def open(self, url, data=None): code = None result = None try: start_time = time.time() result = self.req_session.get(url) except (requests.exceptions.RequestException, Exception), e: reraise_exception(HTTPClientError('Internal HTTP error "%s": %r' % (url, e)), sys.exc_info())
def get_map(self, query): if self.res_range and not self.res_range.contains(query.bbox, query.size, query.srs): raise BlankImage() if self.coverage and not self.coverage.intersects(query.bbox, query.srs): raise BlankImage() try: resp = self.render(query) except RuntimeError, ex: log.error('could not render Mapnik map: %s', ex) reraise_exception(SourceError(ex.args[0]), sys.exc_info())
def get_map(self, query): if self.res_range and not self.res_range.contains(query.bbox, query.size, query.srs): raise BlankImage() if self.coverage and not self.coverage.intersects(query.bbox, query.srs): raise BlankImage() try: resp = self._get_map(query) if self.transparent_color: resp = make_transparent(resp, self.transparent_color, self.transparent_color_tolerance) resp.opacity = self.opacity return resp except HTTPClientError, e: log.warn("could not retrieve WMS map: %s", e) reraise_exception(SourceError(e.args[0]), sys.exc_info())
def open(self, url, data=None): code = None result = None req = urllib2.Request(url, data=data) for key, value in self.header_list: req.add_header(key, value) try: start_time = time.time() if self._timeout is not None: result = self.opener.open(req, timeout=self._timeout) else: result = self.opener.open(req) except HTTPError, e: code = e.code reraise_exception(HTTPClientError('HTTP Error "%s": %d' % (url, e.code), response_code=code), sys.exc_info())
def get_map(self, query): if self.res_range and not self.res_range.contains(query.bbox, query.size, query.srs): raise BlankImage() if self.coverage and not self.coverage.intersects(query.bbox, query.srs): raise BlankImage() try: resp = self._get_map(query) if self.transparent_color: resp = make_transparent(resp, self.transparent_color, self.transparent_color_tolerance) resp.opacity = self.opacity return resp except HTTPClientError, e: log.warn('could not retrieve WMS map: %s', e) reraise_exception(SourceError(e.args[0]), sys.exc_info())
def open(self, url, data=None): code = None result = None req = urllib2.Request(url, data=data) for key, value in self.header_list: req.add_header(key, value) try: start_time = time.time() if self._timeout is not None: result = self.opener.open(req, timeout=self._timeout) else: result = self.opener.open(req) except HTTPError, e: code = e.code reraise_exception( HTTPClientError('HTTP Error "%s": %d' % (url, e.code), response_code=code), sys.exc_info())
def get_map(self, query): if self.grid.tile_size != query.size: ex = InvalidSourceQuery( 'tile size of cache and tile source do not match: %s != %s' % (self.grid.tile_size, query.size) ) log_config.error(ex) raise ex if self.grid.srs != query.srs: ex = InvalidSourceQuery( 'SRS of cache and tile source do not match: %r != %r' % (self.grid.srs, query.srs) ) log_config.error(ex) raise ex if self.res_range and not self.res_range.contains(query.bbox, query.size, query.srs): raise BlankImage() if self.coverage and not self.coverage.intersects(query.bbox, query.srs): raise BlankImage() _bbox, grid, tiles = self.grid.get_affected_tiles(query.bbox, query.size) if grid != (1, 1): raise InvalidSourceQuery('BBOX does not align to tile') tile_coord = tiles.next() log.info("tile_coord request: "+ str(tile_coord)) try: return self.client.get_tile(tile_coord, format=query.format) except HTTPClientError, e: if self.error_handler: resp = self.error_handler.handle(e.response_code, query) if resp: return resp log.warn('could not retrieve tile: %s', e) reraise_exception(SourceError(e.args[0]), sys.exc_info())
def get_map(self, query): if self.grid.tile_size != query.size: ex = InvalidSourceQuery( 'tile size of cache and tile source do not match: %s != %s' % (self.grid.tile_size, query.size)) log_config.error(ex) raise ex if self.grid.srs != query.srs: ex = InvalidSourceQuery( 'SRS of cache and tile source do not match: %r != %r' % (self.grid.srs, query.srs)) log_config.error(ex) raise ex if self.coverage and not self.coverage.intersects( query.bbox, query.srs): raise BlankImage() _bbox, grid, tiles = self.grid.get_affected_tiles( query.bbox, query.size) if grid != (1, 1): raise InvalidSourceQuery('BBOX does not align to tile') tile_coord = tiles.next() try: return self.client.get_tile(tile_coord, format=query.format) except HTTPClientError, e: if self.error_handler: resp = self.error_handler.handle(e.response_code, query) if resp: return resp log.warn('could not retrieve tile: %s', e) reraise_exception(SourceError(e.args[0]), sys.exc_info())
self.req_session = requests.Session(timeout=timeout) def open(self, url, data=None): code = None result = None try: start_time = time.time() result = self.req_session.get(url) except (requests.exceptions.RequestException, Exception), e: reraise_exception(HTTPClientError('Internal HTTP error "%s": %r' % (url, e)), sys.exc_info()) else: code = result.status_code if not (200 <= code < 300): reraise_exception(HTTPClientError('HTTP Error "%s": %d' % (url, code), response_code=code), sys.exc_info()) if code == 204: raise HTTPClientError('HTTP Error "204 No Content"', response_code=204) return result finally: log_request(url, code, result, duration=time.time()-start_time, method='GET') def open_image(self, url, data=None): resp = self.open(url, data=data) if 'content-type' in resp.headers: if not resp.headers['content-type'].lower().startswith('image'): raise HTTPClientError('response is not an image: (%s)' % (resp.content)) return ImageSource(StringIO(resp.content))
class HTTPClient(object): def __init__(self, url=None, username=None, password=None, insecure=False, ssl_ca_certs=None, timeout=None, headers=None): if _urllib2_has_timeout: self._timeout = timeout else: self._timeout = None _set_global_socket_timeout(timeout) if url and url.startswith('https'): if insecure: ssl_ca_certs = None else: if ssl is None: raise ImportError( 'No ssl module found. SSL certificate ' 'verification requires Python 2.6 or ssl module. Upgrade ' 'or disable verification with http.ssl_no_cert_checks option.' ) if ssl_ca_certs is None: raise HTTPClientError( 'No ca_certs file set (http.ssl_ca_certs). ' 'Set file or disable verification with http.ssl_no_cert_checks option.' ) self.opener = create_url_opener(ssl_ca_certs, url, username, password) self.header_list = headers.items() if headers else [] def open(self, url, data=None): code = None result = None req = urllib2.Request(url, data=data) for key, value in self.header_list: req.add_header(key, value) try: start_time = time.time() if self._timeout is not None: result = self.opener.open(req, timeout=self._timeout) else: result = self.opener.open(req) except HTTPError, e: code = e.code reraise_exception( HTTPClientError('HTTP Error "%s": %d' % (url, e.code), response_code=code), sys.exc_info()) except URLError, e: if ssl and isinstance(e.reason, ssl.SSLError): e = HTTPClientError( 'Could not verify connection to URL "%s": %s' % (url, e.reason.args[1])) reraise_exception(e, sys.exc_info()) try: reason = e.reason.args[1] except (AttributeError, IndexError): reason = e.reason reraise_exception( HTTPClientError('No response from URL "%s": %s' % (url, reason)), sys.exc_info())
except URLError, e: if ssl and isinstance(e.reason, ssl.SSLError): e = HTTPClientError( 'Could not verify connection to URL "%s": %s' % (url, e.reason.args[1])) reraise_exception(e, sys.exc_info()) try: reason = e.reason.args[1] except (AttributeError, IndexError): reason = e.reason reraise_exception( HTTPClientError('No response from URL "%s": %s' % (url, reason)), sys.exc_info()) except ValueError, e: reraise_exception( HTTPClientError('URL not correct "%s": %s' % (url, e.args[0])), sys.exc_info()) except Exception, e: reraise_exception( HTTPClientError('Internal HTTP error "%s": %r' % (url, e)), sys.exc_info()) else: code = getattr(result, 'code', 200) if code == 204: raise HTTPClientError('HTTP Error "204 No Content"', response_code=204) return result finally: log_request(url, code, result,
code = e.code reraise_exception(HTTPClientError('HTTP Error "%s": %d' % (url, e.code), response_code=code), sys.exc_info()) except URLError, e: if ssl and isinstance(e.reason, ssl.SSLError): e = HTTPClientError('Could not verify connection to URL "%s": %s' % (url, e.reason.args[1])) reraise_exception(e, sys.exc_info()) try: reason = e.reason.args[1] except (AttributeError, IndexError): reason = e.reason reraise_exception(HTTPClientError('No response from URL "%s": %s' % (url, reason)), sys.exc_info()) except ValueError, e: reraise_exception(HTTPClientError('URL not correct "%s": %s' % (url, e.args[0])), sys.exc_info()) except Exception, e: reraise_exception(HTTPClientError('Internal HTTP error "%s": %r' % (url, e)), sys.exc_info()) else: code = getattr(result, 'code', 200) if code == 204: raise HTTPClientError('HTTP Error "204 No Content"', response_code=204) return result finally: log_request(url, code, result, duration=time.time()-start_time, method=req.get_method()) def open_image(self, url, data=None): resp = self.open(url, data=data) if 'content-type' in resp.headers: if not resp.headers['content-type'].lower().startswith('image'):