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.read())) return ImageSource(resp)
class CGIClient(object): def __init__(self, script, no_headers=False, working_directory=None): self.script = script self.working_directory = working_directory self.no_headers = no_headers def open(self, url, data=None): assert data is None, 'POST requests not supported by CGIClient' parsed_url = urlparse(url) environ = os.environ.copy() environ.update({ 'QUERY_STRING': parsed_url.query, 'REQUEST_METHOD': 'GET', 'GATEWAY_INTERFACE': 'CGI/1.1', 'SERVER_ADDR': '127.0.0.1', 'SERVER_NAME': 'localhost', 'SERVER_PROTOCOL': 'HTTP/1.0', 'SERVER_SOFTWARE': 'MapProxy', }) start_time = time.time() try: p = subprocess.Popen([self.script], env=environ, stdout=subprocess.PIPE, cwd=self.working_directory or os.path.dirname(self.script) ) except OSError, ex: if ex.errno == errno.ENOENT: raise SourceError('CGI script not found (%s)' % (self.script,)) elif ex.errno == errno.EACCES: raise SourceError('No permission for CGI script (%s)' % (self.script,)) else: raise stdout = p.communicate()[0] ret = p.wait() if ret != 0: raise HTTPClientError('Error during CGI call (exit code: %d)' % (ret, )) if self.no_headers: content = stdout headers = dict() else: headers, content = split_cgi_response(stdout) status_match = re.match('(\d\d\d) ', headers.get('Status', '')) if status_match: status_code = status_match.group(1) else: status_code = '-' size = len(content) content = StringIO(content) content.headers = headers log_request('%s:%s' % (self.script, parsed_url.query), status_code, size=size, method='CGI', duration=time.time()-start_time) return content
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())
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))