本文整理汇总了Python中urllib2.parse_http_list函数的典型用法代码示例。如果您正苦于以下问题:Python parse_http_list函数的具体用法?Python parse_http_list怎么用?Python parse_http_list使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_http_list函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: http_response
def http_response(self, req, resp):
# code for after-fetch, to know whether to save to hard-drive (if stiking to http headers' will)
if resp.code == 304:
return resp
if ('cache-control' in resp.headers or 'pragma' in resp.headers) and self.force_min is None:
cache_control = parse_http_list(resp.headers.get('cache-control', ()))
cache_control += parse_http_list(resp.headers.get('pragma', ()))
cc_list = [x for x in cache_control if '=' not in x]
if 'no-cache' in cc_list or 'no-store' in cc_list or ('private' in cc_list and not self.private):
# kindly follow web servers indications
return resp
if resp.headers.get('Morss') == 'from_cache':
# it comes from cache, so no need to save it again
return resp
# save to disk
data = resp.read()
self._save(req.get_full_url(), resp.code, resp.msg, resp.headers, data, time.time())
fp = BytesIO(data)
old_resp = resp
resp = addinfourl(fp, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
return resp
示例2: test_parse_http_list
def test_parse_http_list(self):
tests = [('a,b,c', ['a', 'b', 'c']),
('path"o,l"og"i"cal, example', ['path"o,l"og"i"cal', 'example']),
('a, b, "c", "d", "e,f", g, h', ['a', 'b', '"c"', '"d"', '"e,f"', 'g', 'h']),
('a="b\\"c", d="e\\,f", g="h\\\\i"', ['a="b"c"', 'd="e,f"', 'g="h\\i"'])]
for string, list in tests:
self.assertEquals(urllib2.parse_http_list(string), list)
示例3: check_auth
def check_auth(self, header, method='GET'):
"Check a response to our auth challenge"
from urllib2 import parse_http_list
H, KD = self.get_algorithm_impls()
resp = parse_keqv_list(parse_http_list(header))
algo = resp.get('algorithm', 'MD5').upper()
if algo != self.algorithm:
return False, "unknown algo %s"%algo
user = resp['username']
realm = resp['realm']
nonce = resp['nonce']
# XXX Check the nonce is something we've issued
HA1 = self._user_hashes.get((user,realm))
if not HA1:
return False, "unknown user/realm %s/%s"%(user, realm)
qop = resp.get('qop')
if qop != 'auth':
return False, "unknown qop %r"%(qop)
cnonce, ncvalue = resp.get('cnonce'), resp.get('nc')
if not cnonce or not ncvalue:
return False, "failed to provide cnonce"
# Check the URI is correct!
A2 = '%s:%s'%(method, resp['uri'])
noncebit = "%s:%s:%s:%s:%s" % (nonce,ncvalue,cnonce,qop,H(A2))
respdig = KD(HA1, noncebit)
if respdig != resp['response']:
return False, "response incorrect"
print "all ok"
return True, "OK"
示例4: parse_oauth_header
def parse_oauth_header(header):
type, rest = header.split(" ", 1)
if type != "OAuth":
raise ValueError("Authorization is not of OAuth type")
return urllib2.parse_keqv_list(urllib2.parse_http_list(rest))
示例5: get
def get(self):
assert self.current_user
origin = self.get_argument("origin", None)
session = model.Session()
actions_param = self.get_argument("actions", None)
where = [model.UserConsent.user_id==self.current_user]
if origin is not None:
where.append(model.UserConsent.origin==origin)
if actions_param is not None:
actions = actions_param.split(",")
where.append(model.UserConsent.operation.in_(actions))
existing = session.query(model.UserConsent).filter(
and_(*where)).all()
result = {}
for e in existing:
origin_result = result.setdefault(e.origin, {})
origin_result[e.operation] = e.allowed
# work out if we are trying to serve up the html ui or just the data.
accepts = urllib2.parse_http_list(self.request.headers.get("Accept", ""))
try:
json_ndx = accepts.index("application/json")
except ValueError:
json_ndx = 10000
try:
html_ndx = accepts.index("text/html")
except ValueError:
html_ndx = 10000
if html_ndx < json_ndx:
self.render("consent.html", consent_items=result)
else:
self.write(result)
示例6: parse_options_header
def parse_options_header(value):
result = []
for item in urllib2.parse_http_list(value):
if item[:1] == item[-1:] == '"':
item = unquote_header_value(item[1:-1])
result.append(item)
return result
示例7: parse_authorization_header
def parse_authorization_header(header):
""" Parse requests authorization header into list.
Raise ValueError if some problem occurs. """
# digest is marked as part of header and causes problem
# parsing, so remove its
if not header.startswith("Digest "):
raise ValueError("Header do not start with Digest")
header = header[len("Digest ") :]
# Convert the auth params to a dict
items = urllib2.parse_http_list(header)
params = urllib2.parse_keqv_list(items)
required = ["username", "realm", "nonce", "uri", "response"]
for field in required:
if not params.has_key(field):
raise ValueError("Required field %s not found" % field)
# check for qop companions (sect. 3.2.2)
if params.has_key("qop") and not params.has_key("cnonce") and params.has_key("cn"):
raise ValueError("qop sent without cnonce and cn")
return params
示例8: parse_authorization_header
def parse_authorization_header(value):
"""Parse the Authenticate header
Returns nothing on failure, opts hash on success with type='basic' or 'digest'
and other params.
<http://nullege.com/codes/search/werkzeug.http.parse_authorization_header>
<http://stackoverflow.com/questions/1349367/parse-an-http-request-authorization-header-with-python>
<http://bugs.python.org/file34041/0001-Add-an-authorization-header-to-the-initial-request.patch>
"""
try:
(auth_type, auth_info) = value.split(' ', 1)
auth_type = auth_type.lower()
except ValueError as e:
return
if (auth_type == 'basic'):
try:
(username, password) = base64.b64decode(auth_info).split(':', 1)
except Exception as e:
return
return {'type':'basic', 'username': username, 'password': password}
elif (auth_type == 'digest'):
auth_map = urllib2.parse_keqv_list(urllib2.parse_http_list(auth_info))
print auth_map
for key in 'username', 'realm', 'nonce', 'uri', 'response':
if not key in auth_map:
return
if 'qop' in auth_map:
if not auth_map.get('nc') or not auth_map.get('cnonce'):
return
auth_map['type']='digest'
return auth_map
else:
# unknown auth type
return
示例9: compose
def compose(self, digest=None, basic=None, username=None, password=None,
challenge=None, path=None, method=None):
assert username and password
if basic or not challenge:
assert not digest
userpass = "%s:%s" % (username.strip(), password.strip())
return "Basic %s" % userpass.encode('base64').strip()
assert challenge and not basic
path = path or "/"
(_, realm) = challenge.split('realm="')
(realm, _) = realm.split('"', 1)
auth = urllib2.AbstractDigestAuthHandler()
auth.add_password(realm, path, username, password)
(token, challenge) = challenge.split(' ', 1)
chal = urllib2.parse_keqv_list(urllib2.parse_http_list(challenge))
class FakeRequest(object):
def get_full_url(self):
return path
def has_data(self):
return False
def get_method(self):
return method or "GET"
get_selector = get_full_url
retval = "Digest %s" % auth.get_authorization(FakeRequest(), chal)
return (retval,)
示例10: _parseHeader
def _parseHeader(self, authheader, request):
n = 7 # n = len("Digest ")
try:
authheader = authheader[n:].strip()
items = urllib2.parse_http_list(authheader)
request.env['__DIGEST_PARAMS__'] = urllib2.parse_keqv_list(items)
except Exception, e:
request.env['__DIGEST_PARAMS__'] = {}
示例11: test_parse_http_list
def test_parse_http_list(self):
tests = [
("a,b,c", ["a", "b", "c"]),
('path"o,l"og"i"cal, example', ['path"o,l"og"i"cal', "example"]),
('a, b, "c", "d", "e,f", g, h', ["a", "b", '"c"', '"d"', '"e,f"', "g", "h"]),
('a="b\\"c", d="e\\,f", g="h\\\\i"', ['a="b"c"', 'd="e,f"', 'g="h\\i"']),
]
for string, list in tests:
self.assertEqual(urllib2.parse_http_list(string), list)
示例12: parse_auth_header
def parse_auth_header(self, authorization):
values = {}
for value in urllib2.parse_http_list(authorization):
n, v = value.split("=", 1)
if v[0] == '"' and v[-1] == '"':
values[n] = v[1:-1]
else:
values[n] = v
return values
示例13: parse_list_header
def parse_list_header(value):
"""
Parse lists as described by RFC 2068 Section 2.
"""
result = []
for item in urllib2.parse_http_list(value):
if item[:1] == item[-1:] == '"':
item = unquote_header_value(item[1:-1])
result.append(item)
return result
示例14: parse_authorization_header
def parse_authorization_header(authorization_header):
"""Parse an OAuth authorization header into a list of 2-tuples"""
auth_scheme = 'OAuth '
if authorization_header.startswith(auth_scheme):
authorization_header = authorization_header.replace(auth_scheme, '', 1)
items = urllib2.parse_http_list(authorization_header)
try:
return urllib2.parse_keqv_list(items).items()
except ValueError:
raise ValueError('Malformed authorization header')
示例15: _parse
def _parse(self, authorization):
scheme, rest = authorization.split(None, 1)
args = urllib2.parse_keqv_list(urllib2.parse_http_list(rest))
challengeType = {
'basic': BasicChallenge,
'digest': DigestChallenge,
}.get(scheme.lower())
if challengeType is None:
return "", None
return scheme.lower(), challengeType(**args)