本文整理汇总了Python中wsgiref.util.is_hop_by_hop函数的典型用法代码示例。如果您正苦于以下问题:Python is_hop_by_hop函数的具体用法?Python is_hop_by_hop怎么用?Python is_hop_by_hop使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_hop_by_hop函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testHopByHop
def testHopByHop(self):
for hop in (
"Connection Keep-Alive Proxy-Authenticate Proxy-Authorization " "TE Trailers Transfer-Encoding Upgrade"
).split():
for alt in hop, hop.title(), hop.upper(), hop.lower():
self.assertTrue(util.is_hop_by_hop(alt))
# Not comprehensive, just a few random header names
for hop in ("Accept Cache-Control Date Pragma Trailer Via Warning").split():
for alt in hop, hop.title(), hop.upper(), hop.lower():
self.assertFalse(util.is_hop_by_hop(alt))
示例2: start_response
def start_response(self, status, headers, exc_info=None):
if exc_info:
# Log the error.
self._request.app.logger.error("Unexpected error", exc_info=exc_info)
# Attempt to modify the response.
try:
if self._response and self._response.prepared:
raise exc_info[1].with_traceback(exc_info[2])
self._response = None
finally:
exc_info = None
# Cannot start response twice.
assert not self._response, "Cannot call start_response() twice"
# Parse the status.
assert isinstance(status, str), "Response status should be str"
status_code, reason = status.split(None, 1)
status_code = int(status_code)
# Store the response.
self._response = StreamResponse(
status = status_code,
reason = reason,
)
# Store the headers.
for header_name, header_value in headers:
assert not is_hop_by_hop(header_name), "Hop-by-hop headers are forbidden"
self._response.headers.add(header_name, header_value)
# Return the stream writer interface.
return self.write
示例3: start_response
def start_response(self, status, response_headers, exc_info=None):
"""
Method to be passed to WSGI application object to start the response.
"""
if exc_info:
try:
raise Exception(exc_info)
# raise exc_info[0], exc_info[1], exc_info[2]
finally:
exc_info = None
elif self.response_dict:
#Will be caught by _error
raise WsgiAppError('start_response called a second time without exc_info! See PEP 333.')
#PEP 333 requires that an application NOT send any hop-by-hop headers.
#Therefore, we check for any of them in the headers the application
#returns. If so, an exception is raised to be caught by _error.
for key,value in response_headers:
if is_hop_by_hop(key):
raise WsgiAppError('Hop by hop header specified')
self.response_dict['headers'] = copy.copy(response_headers)
self.response_dict['statuscode'] = status
return self.write
示例4: __assembleResponseHeaders
def __assembleResponseHeaders(self, headers):
responseHeaders = []
for key, value in headers.iteritems():
if not is_hop_by_hop(key):
key = key.title()
responseHeaders.append((key, value))
return responseHeaders
示例5: main
def main():
response = flask.Response()
url, key, timeout = (request.args.get('u', ''), request.args.get('k', ''),
int(request.args.get('t', '30')))
if key and key not in ALLOW_KEYS:
return 'Invalid key!'
if url and key:
try:
header = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; \
rv:30.0) Gecko/20100101 Firefox/30.0",
"Accept": "text/html,application/xhtml+xml,\
application/xml;q=0.9,*/*;q=0.8"}
r = requests.get(url, timeout=timeout, headers=header)
header = {header: value for header, value in r.headers.items()
if not is_hop_by_hop(header)}
cookie_added = False
for header_name, header_value in header.items():
if header_name == 'Set-Cookie' and cookie_added:
response.headers['Set-Cookie'] = header_value
else:
response.headers['Set-Cookie'] = header_value
if header_name == 'Set-Cookie':
cookie_added = True
return r.text
except socket.timeout:
pass
except Exception as e:
return repr(e)
else:
return "I need KEY and URL, check your fomat :("
示例6: Home
def Home():
resp = bottle.response
qry = bottle.request.query
url, k, timeout = qry.u, qry.k, int(qry.get('t', '30'))
if k and k != ALLOW_KEYS:
return 'Auth Key is invalid!'
if url and k:
url = urllib.unquote(url)
try:
req = urllib2.Request(url)
req.add_header('User-Agent', "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)")
req.add_header('Accept', "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
ret = urllib2.urlopen(req, timeout=timeout)
content = ret.read()
headers = [(n,v) for n,v in ret.info().items() if not is_hop_by_hop(n)]
cookieAdded = False
for n, v in headers:
if n == 'Set-Cookie' and cookieAdded:
resp.add_header(n, v)
else:
resp.set_header(n, v)
if n == 'Set-Cookie':
cookieAdded = True
return content
except socket.timeout:
pass
except Exception as e:
print("ERR : %s : %s" % (type(e), str(e)))
bottle.abort(400)
else:
return "<html><head><title>Forwarder Url</title></head><body>Forwarder(%s) : thisurl?k=AUTHKEY&t=timeout&u=url</body></html>" % __Version__
示例7: start_response
def start_response(self, status, headers,exc_info=None):
"""'start_response()' callable as specified by PEP 333"""
if exc_info:
try:
if self.headers_sent:
# Re-raise original exception if headers sent
raise exc_info[0], exc_info[1], exc_info[2]
finally:
exc_info = None # avoid dangling circular ref
elif self.headers is not None:
raise AssertionError("Headers already set!")
assert type_is(status, StringType),"Status must be a string"
assert len(status)>=4,"Status must be at least 4 characters"
assert int(status[:3]),"Status message must begin w/3-digit code"
assert status[3]==" ", "Status message must have a space after code"
if __debug__:
for name,val in headers:
assert type_is(name, StringType),"Header names must be strings"
assert type_is(val, StringType),"Header values must be strings"
assert not is_hop_by_hop(name),"Hop-by-hop headers not allowed"
self.status = status
self.headers = self.headers_class(headers)
return self.write
示例8: set_response_headers
def set_response_headers(response, response_headers):
for header, value in response_headers.items():
if is_hop_by_hop(header) or header.lower() == 'set-cookie':
continue
response[header.title()] = value
logger.debug('Response headers: %s', getattr(response, '_headers'))
示例9: __assembleRequestHeaders
def __assembleRequestHeaders(self):
headers = {}
for key, value in self.environ.iteritems():
if key.startswith('HTTP_'):
key = key[5:].replace('_', '-').title()
if not is_hop_by_hop(key):
headers[key] = value
headers['Host'] = self.__backend['host']
return headers
示例10: do_proxy
def do_proxy(self, method):
orig_url = self.request.url
orig_body = self.request.body
(scm, netloc, path, params, query, _) = urlparse.urlparse(orig_url)
if 'Authorization' not in self.request.headers :
headers = {}
else:
auth_header = self.request.headers['Authorization']
auth_parts = auth_header.split(' ')
user_pass_parts = base64.b64decode(auth_parts[1]).split(':')
username = user_pass_parts[0]
password = user_pass_parts[1]
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
headers = {'Authorization': "Basic %s" % base64string}
path_parts = path.split('/')
if path_parts[1] == 'gtap':
path_parts = path_parts[2:]
path_parts.insert(0,'')
path = '/'.join(path_parts).replace('//','/')
path_parts = path.split('/')
if path_parts[1] == 'api' or path_parts[1] == 'search':
sub_head = path_parts[1]
path_parts = path_parts[2:]
path_parts.insert(0,'')
new_path = '/'.join(path_parts).replace('//','/')
new_netloc = sub_head + '.twitter.com'
else:
new_path = path
new_netloc = 'twitter.com'
if new_path == '/' or new_path == '':
global gtap_message
gtap_message = gtap_message.replace('#app_url#', netloc)
gtap_message = gtap_message.replace('#gtap_version#', gtap_vrsion)
self.my_output( 'text/html', gtap_message )
else:
new_url = urlparse.urlunparse(('https', new_netloc, new_path.replace('//','/'), params, query, ''))
logging.debug(new_url)
logging.debug(orig_body)
data = urlfetch.fetch(new_url, payload=orig_body, method=method, headers=headers, allow_truncated=True)
logging.debug(data.headers)
try :
self.response.set_status(data.status_code)
except Exception:
logging.debug(data.status_code)
self.response.set_status(503)
self.response.headers.add_header('GTAP-Version', gtap_vrsion)
for res_name, res_value in data.headers.items():
if is_hop_by_hop(res_name) is False and res_name!='status':
self.response.headers.add_header(res_name, res_value)
self.response.out.write(data.content)
示例11: _get_environ
def _get_environ(self, request):
# Resolve the path info.
path_info = request.match_info["path_info"]
script_name = request.path[:len(request.path)-len(path_info)]
# Special case: If the app was mounted on the root, then the script name will
# currently be set to "/", which is illegal in the WSGI spec. The script name
# could also end with a slash if the WSGIHandler was mounted as a route
# manually with a trailing slash before the path_info. In either case, we
# correct this according to the WSGI spec by transferring the trailing slash
# from script_name to the start of path_info.
if script_name.endswith("/"):
script_name = script_name[:-1]
path_info = "/" + path_info
# Read the body.
body = (yield from request.read())
# Parse the connection info.
server_name, server_port = parse_sockname(request.transport.get_extra_info("sockname"))
remote_addr, remote_port = parse_sockname(request.transport.get_extra_info("peername"))
# Detect the URL scheme.
url_scheme = self._url_scheme
if url_scheme is None:
url_scheme = "http" if request.transport.get_extra_info("sslcontext") is None else "https"
# Create the environ.
environ = {
"REQUEST_METHOD": request.method,
"SCRIPT_NAME": quote(script_name), # WSGI spec expects URL-quoted path components.
"PATH_INFO": quote(path_info), # WSGI spec expects URL-quoted path components.
"QUERY_STRING": request.query_string,
"CONTENT_TYPE": request.headers.get("Content-Type", ""),
"CONTENT_LENGTH": str(len(body)),
"SERVER_NAME": server_name,
"SERVER_PORT": server_port,
"REMOTE_ADDR": remote_addr,
"REMOTE_HOST": remote_addr,
"REMOTE_PORT": remote_port,
"SERVER_PROTOCOL": "HTTP/{}.{}".format(*request.version),
"wsgi.version": (1, 0),
"wsgi.url_scheme": url_scheme,
"wsgi.input": io.BytesIO(body),
"wsgi.errors": self._stderr,
"wsgi.multithread": True,
"wsgi.multiprocess": False,
"wsgi.run_once": False,
}
# Add in additional HTTP headers.
for header_name in request.headers:
header_name = header_name.upper()
if not(is_hop_by_hop(header_name)) and not header_name in ("CONTENT-LENGTH", "CONTENT-TYPE"):
header_value = ",".join(request.headers.getall(header_name))
environ["HTTP_" + header_name.replace("-", "_")] = header_value
# All done!
return environ
示例12: parse_requests_result
def parse_requests_result(result):
headers = result.headers
for key, val in headers.iteritems():
if is_hop_by_hop(key):
headers.pop(key)
elif key.lower() == 'content-encoding' and 'zip' in val:
headers.pop(key)
status_code = result.status_code
output = result.content
if 'Content-Length' in headers:
# 让wsgi模块自行计算解压之后的字节大小
headers.pop('Content-Length')
return status_code, headers, output
示例13: proxy_app
def proxy_app(environ, start_response):
"""Formards the request to the real server
"""
# Rebuilding the request headers from environ
req_headers = {}
not_relayed_headers = ('HTTP_ACCEPT_ENCODING', 'HTTP_HOST', 'HTTP_PROXY_CONNECTION')
for name, value in ((name, value) for name, value in environ.iteritems()
if name.startswith('HTTP_') and name not in not_relayed_headers):
# HTTP_XX_XX -> xx-xx
name = '-'.join(w.lower() for w in name[5:].split('_'))
req_headers[name] = value
# Some headers are not prefixed with HTTP
for name in ('CONTENT_TYPE', 'CONTENT_LENGTH'):
value = environ.get(name, None)
if value:
name = '-'.join(w.lower() for w in name.split('_'))
req_headers[name] = value
# Add or change headers
custom_headers.pre_hook(req_headers)
# Proxying to the destination server
scheme = environ.get('wsgi.url_scheme', 'http')
connection_class = scheme2connection.get(scheme, httplib.HTTPConnection)
conn = connection_class(environ['HTTP_HOST'])
req_meth = environ['REQUEST_METHOD']
conn.request(req_meth, '{0[PATH_INFO]}?{0[QUERY_STRING]}'.format(environ), headers=req_headers)
if req_meth == 'POST':
# We need to relay the body too
input_ = environ['wsgi.input']
length = int(environ.get('CONTENT_LENGTH', '0'))
payload = input_.read(length) # Oops, could be a biiiig file
conn.send(payload)
# Transform / relay the response
response = conn.getresponse()
txt_status = httplib.responses.get(response.status, "Unknown status {0}".format(response.status))
status = '{0} {1}'.format(response.status, txt_status)
# Remove so-called "hop by hop" headers
resp_headers = [(n, v) for n, v in response.getheaders() if not is_hop_by_hop(n)]
# Notify response headers if required
custom_headers.post_hook(resp_headers)
# Replying to browser
start_response(status, resp_headers)
return iterstreamer(response)
示例14: proxy
def proxy():
url = request.GET.get("url")
headers = ["{0}: {1}".format(k,v) for k,v in request.headers.iteritems() if k.lower() not in STRIP_HEADERS]
try:
headtuple, content = curl_http(url, headers)
import ipdb;ipdb.set_trace()
headret = [(k,v) for k,v in headtuple if not is_hop_by_hop(k)]
html = content.replace("<body>", "<body><!-- test -->")
log.debug(html)
raise HTTPResponse(output = content, header = headret)
except pycurl.error, e:
log.debug("curl error: " + str(e))
markurl(url)
redirect(url)
示例15: proxy_request
def proxy_request():
""" Called from a bottle request context.
Proxies the call transparently to the graphite server.
"""
url = config.graphite_url+request.path+'?'+request.query_string
if request.method == 'POST':
r = requests.post(url, data=request.body.read(), headers=request.headers, stream=True)
else:
r = requests.get(url, headers=request.headers, stream=True)
# Relay headers intact
for k,v in r.headers.items():
if not is_hop_by_hop(k):
response.set_header(k,v)
response.status = r.status_code
return r.raw.read()