本文整理汇总了Python中urllib2.parse_keqv_list函数的典型用法代码示例。如果您正苦于以下问题:Python parse_keqv_list函数的具体用法?Python parse_keqv_list怎么用?Python parse_keqv_list使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_keqv_list函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: 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
示例2: 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,)
示例3: 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
示例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: youtube_video
def youtube_video(url):
try:
conn = urllib2.urlopen(url)
encoding = conn.headers.getparam("charset")
content = conn.read().decode(encoding)
s = re.findall(r'"url_encoded_fmt_stream_map": ?"([^"]+)"', content)
if s:
import HTMLParser
s = s[0].split(",")
s = [a.replace("\\u0026", "&") for a in s]
s = [urllib2.parse_keqv_list(a.split("&")) for a in s]
n = re.findall(r"<title>(.+) - YouTube</title>", content)
s, n = (s or [], HTMLParser.HTMLParser().unescape(n[0]))
for z in s:
if z["itag"] == "18":
if "mp4" in z["type"]:
ext = ".mp4"
elif "flv" in z["type"]:
ext = ".flv"
try:
link = urllib.unquote(z["url"] + "&signature=%s" % z["sig"])
except:
link = urllib.unquote(z["url"])
return link
except:
return False
示例6: getYoutubeMovie
def getYoutubeMovie(url):
try:
conn = urllib2.urlopen(url)
encoding = conn.headers.getparam('charset')
content = conn.read().decode(encoding)
#get available streams
s = re.findall(r'"url_encoded_fmt_stream_map": ?"([^"]+)"', content)
print s
if s and len(s):
s = s[0].split(',')
values = {}
for stream in s:
stream = stream.replace('\\u0026', '&')
stream = urllib2.parse_keqv_list(stream.split('&'))
values[stream.get('itag') or "0"] = stream
itags = values.keys()
sorted(itags, reverse=True)
print itags
link = None
for itag in itags:
z = values[itag]
if itag == '84' or itag == '82' or itag == '38' or itag == '37' or itag == '22' or itag == '18':
try:
link = urllib.unquote(z['url'] + '&signature=%s' % z['sig'])
except:
link = urllib.unquote(z['url'])
return link
except Exception as e:
print e
return None
示例7: _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__'] = {}
示例8: 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')
示例9: _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)
示例10: retry_http_digest_auth
def retry_http_digest_auth(self, req, resp, host, auth):
_token, challenge = auth.split(" ", 1)
chal = urllib2.parse_keqv_list(urllib2.parse_http_list(challenge))
auth = self.get_authorization(req, chal)
if auth:
auth_val = "Digest %s" % auth
if req.headers.get(self.auth_header, None) == auth_val:
return None
req.add_unredirected_header(self.auth_header, auth_val)
return "request", req
return None
示例11: retry_http_digest_auth
def retry_http_digest_auth(self, req, auth):
token, challenge = auth.split(' ', 1)
chal = parse_keqv_list(parse_http_list(challenge))
auth = self.get_authorization(req, chal)
if auth:
auth_val = 'Digest %s' % auth
if req.headers.get(self.auth_header, None) == auth_val:
return None
newreq = copy.copy(req)
newreq.add_unredirected_header(self.auth_header, auth_val)
newreq.visit = False
return self.parent.open(newreq)
示例12: retry_http_digest_auth
def retry_http_digest_auth(self, req, auth):
token, challenge = auth.split(' ', 1)
chal = urllib2.parse_keqv_list(urllib2.parse_http_list(challenge))
auth = self.get_authorization(req, chal)
if auth:
auth_val = 'X-Digest %s' % auth
if req.headers.get(self.auth_header, None) == auth_val:
return None
req.add_unredirected_header(self.auth_header, auth_val)
resp = self.parent.open(req, timeout=req.timeout)
return resp
示例13: authorized
def authorized(self):
tcs = self.server.test_case_server
auth_header = self.headers.get(tcs.auth_header_recv, None)
if auth_header is None:
return False
scheme, auth = auth_header.split(None, 1)
if scheme.lower() == tcs.auth_scheme:
auth_dict = urllib2.parse_keqv_list(urllib2.parse_http_list(auth))
return tcs.digest_authorized(auth_dict, self.command)
return False
示例14: yt_get_all_url_maps_name
def yt_get_all_url_maps_name(url):
conn = urllib2.urlopen(url)
encoding = conn.headers.getparam('charset')
content = conn.read().decode(encoding)
s = re.findall(r'"url_encoded_fmt_stream_map": "([^"]+)"', content)
if s:
s = s[0].split(',')
s = [a.replace('\\u0026', '&') for a in s]
s = [urllib2.parse_keqv_list(a.split('&')) for a in s]
n = re.findall(r'<title>(.+) - YouTube</title>', content)
return (s or [],
HTMLParser.HTMLParser().unescape(n[0]))
示例15: createAuthObject
def createAuthObject(authHeader):
"""
Returns the authentication mechanism, or None if not implemented.
"""
authType, challenge = authHeader.split(' ', 1)
_authType_lower = authType.lower()
challenge = urllib2.parse_keqv_list(urllib2.parse_http_list(challenge))
assert _authType_lower in ('digest', 'ssl'), \
repr(_authType_lower)
# "basic" authentication is not supported
return DigestAuthentication(challenge) if _authType_lower == 'digest' \
else None