本文整理汇总了Python中urllib.parse.parse_qsl函数的典型用法代码示例。如果您正苦于以下问题:Python parse_qsl函数的具体用法?Python parse_qsl怎么用?Python parse_qsl使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_qsl函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_preserved_filters
def add_preserved_filters(context, url, popup=False):
opts = context.get('opts')
preserved_filters = context.get('preserved_filters')
parsed_url = list(urlparse(url))
parsed_qs = dict(parse_qsl(parsed_url[4]))
merged_qs = dict()
if opts and preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters))
try:
match = resolve(url)
except Resolver404:
pass
else:
current_url = '%s:%s' % (match.namespace, match.url_name)
changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
if changelist_url == current_url and '_changelist_filters' in preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))
merged_qs.update(preserved_filters)
if popup:
from django.contrib.admin.options import IS_POPUP_VAR
merged_qs[IS_POPUP_VAR] = 1
merged_qs.update(parsed_qs)
parsed_url[4] = urlencode(merged_qs)
return urlunparse(parsed_url)
示例2: twitter
def twitter():
request_token_url = 'https://api.twitter.com/oauth/request_token'
access_token_url = 'https://api.twitter.com/oauth/access_token'
authenticate_url = 'https://api.twitter.com/oauth/authenticate'
if request.args.get('oauth_token') and request.args.get('oauth_verifier'):
auth = OAuth1(app.config['TWITTER_CONSUMER_KEY'],
client_secret=app.config['TWITTER_CONSUMER_SECRET'],
resource_owner_key=request.args.get('oauth_token'),
verifier=request.args.get('oauth_verifier'))
r = requests.post(access_token_url, auth=auth)
profile = dict(parse_qsl(r.text))
user = User.query.filter_by(twitter=profile['user_id']).first()
if user:
token = create_jwt_token(user)
return jsonify(token=token)
u = User(twitter=profile['user_id'],
first_name=profile['screen_name'])
db.session.add(u)
db.session.commit()
token = create_jwt_token(u)
return jsonify(token=token)
else:
oauth = OAuth1(app.config['TWITTER_CONSUMER_KEY'],
client_secret=app.config['TWITTER_CONSUMER_SECRET'],
callback_uri=app.config['TWITTER_CALLBACK_URL'])
r = requests.post(request_token_url, auth=oauth)
oauth_token = dict(parse_qsl(r.text))
qs = urlencode(dict(oauth_token=oauth_token['oauth_token']))
return redirect(authenticate_url + '?' + qs)
示例3: _build_url
def _build_url(self):
"""Build resource url
Parsing ``self._url``, add ``self._params`` to query string if need
:return self._url: resource url
"""
scheme, netloc, path, params, query, fragment = urlparse(self._url)
# IDN domains support
netloc = to_unicode(netloc)
# removed idna encode as it was causing python3 urlunparse to error
# print(repr(netloc), repr(netloc.encode('idna')))
if not netloc:
raise ValueError("Invalid url")
elif not scheme:
scheme = "http"
tmp = []
if self._params is not None:
for param, value in self._params:
if isinstance(value, tuple):
for i in value:
tmp.append((param, i))
elif isinstance(value, str):
tmp.append((param, value))
if tmp:
tmp = parse_qsl(query, keep_blank_values=True) + tmp
else:
try:
tmp = parse_qsl(query, keep_blank_values=True, strict_parsing=True)
except ValueError:
tmp = query
if isinstance(tmp, str):
encode = quote_plus
noencode = lambda result: result
else:
encode = urlencode
noencode = urlnoencode
if self._encode_query:
query = encode(tmp)
else:
query = noencode(tmp)
del tmp
# print(repr([scheme, netloc, path, query, fragment]))
url_unparse_list = [
scheme.encode('utf8'),
netloc.encode('idna'),
path.encode('utf8'),
params.encode('utf8'),
query.encode('utf8'),
fragment.encode('utf8')]
self._url = urlunparse(url_unparse_list)
return self._url
示例4: append_qs
def append_qs(url, query_string):
"""Append query_string values to an existing URL and return it as a string.
query_string can be:
* an encoded string: 'test3=val1&test3=val2'
* a dict of strings: {'test3': 'val'}
* a dict of lists of strings: {'test3': ['val1', 'val2']}
* a list of tuples: [('test3', 'val1'), ('test3', 'val2')]
"""
parsed_url = urlparse.urlsplit(url)
parsed_qs = urlparse.parse_qsl(parsed_url.query, True)
if isstr(query_string):
parsed_qs += urlparse.parse_qsl(query_string)
elif isdict(query_string):
for item in query_string.items():
if islist(item[1]):
for val in item[1]:
parsed_qs.append((item[0], val))
else:
parsed_qs.append(item)
elif islist(query_string):
parsed_qs += query_string
else:
raise TypeError('Unexpected query_string type')
return urlparse.urlunsplit((
parsed_url.scheme,
parsed_url.netloc,
parsed_url.path,
urlencode_unicode(parsed_qs),
parsed_url.fragment,
))
示例5: build_url
def build_url(base, query_params={}, fragment={}):
"""Construct a URL based off of base containing all parameters in
the query portion of base plus any additional parameters.
Taken from https://github.com/NateFerrero/oauth2lib/blob/master/oauth2lib/utils.py and extended to allow
paramenters as fragment
:param base: Base URL
:type base: str
::param query_params: Additional query parameters to include.
:type query_params: dict
::param fragment: Additional parameters to include in the fragment section of the url
:type fragment: dict
:rtype: str
"""
url = urlparse.urlparse(base)
query_params.update(urlparse.parse_qsl(url.query, True))
query_params = {k: v for k, v in query_params.iteritems() if v is not None}
fragment.update(urlparse.parse_qsl(url.fragment, True))
fragment = {k: v for k, v in fragment.iteritems() if v is not None}
return urlparse.urlunparse((url.scheme,
url.netloc,
url.path,
url.params,
urllib.urlencode(query_params),
urllib.urlencode(fragment)))
示例6: add_preserved_filters
def add_preserved_filters(context, url, popup=False, to_field=None):
opts = context.get('opts')
preserved_filters = context.get('preserved_filters')
parsed_url = list(urlparse(url))
parsed_qs = dict(parse_qsl(parsed_url[4]))
merged_qs = {}
if opts and preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters))
match_url = '/%s' % url.partition(get_script_prefix())[2]
try:
match = resolve(match_url)
except Resolver404:
pass
else:
current_url = '%s:%s' % (match.app_name, match.url_name)
changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
if changelist_url == current_url and '_changelist_filters' in preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))
merged_qs.update(preserved_filters)
if popup:
from django.contrib.admin.options import IS_POPUP_VAR
merged_qs[IS_POPUP_VAR] = 1
if to_field:
from django.contrib.admin.options import TO_FIELD_VAR
merged_qs[TO_FIELD_VAR] = to_field
merged_qs.update(parsed_qs)
parsed_url[4] = urlencode(merged_qs)
return urlunparse(parsed_url)
示例7: respond_all
def respond_all(environ, start_response):
status = '200 OK' # HTTP Status
headers = [('Content-type', 'text/plain; charset=utf-8')] # HTTP Headers
start_response(status, headers)
my_dict = {'method_name': environ['REQUEST_METHOD'], 'path_info': environ['PATH_INFO']}
if len(environ['CONTENT_LENGTH'])>0:
my_dict['content_length'] = environ['CONTENT_LENGTH']
request_body = environ['wsgi.input'].read(int(environ['CONTENT_LENGTH'])).decode('utf-8')
request_body = request_body.strip('[] ')
body_dict = parse.parse_qsl(request_body)
my_dict.update(body_dict)
else:
pass
if len(environ['QUERY_STRING'])>0:
qs = environ['QUERY_STRING']
query_dict = parse.parse_qsl(qs)
my_dict.update(query_dict)
else:
pass
json_response = json.dumps(my_dict, sort_keys=True, indent=4, separators=(',',': '))
return [json_response.encode('utf-8')]
示例8: cleanup
def cleanup(url):
url = _follow(url)
# remove trackers params
try:
urlp = urlparse(url)
# cleanup query param
query = parse_qsl(urlp.query)
# only if query is non empty and we manage to parse fragment as
# key/value
if urlp.query and query:
for annoying in ANNOYING_PARAMS:
query = [(x, y) for x, y in query if not x.startswith(annoying)]
urlp = urlp._replace(
query=urlencode(query),
)
# cleanup fragment param
fragment = parse_qsl(urlp.fragment)
# only if fragments is non empty and we manage to parse fragment as
# key/value
if urlp.fragment and fragment:
for annoying in ANNOYING_PARAMS:
fragment = [(x, y) for x, y in fragment if not x.startswith(annoying)]
urlp = urlp._replace(
fragment=urlencode(fragment),
)
url = urlp.geturl()
except Exception:
app.logger.exception("Problem cleaning url %s", url)
app.logger.info("Final url %s", url)
return url
示例9: twitter
def twitter():
request_token_url = 'https://api.twitter.com/oauth/request_token'
access_token_url = 'https://api.twitter.com/oauth/access_token'
if request.json.get('oauth_token') and request.json.get('oauth_verifier'):
auth = OAuth1(app.config['OAUTH2_CLIENT_ID'],
client_secret=app.config['OAUTH2_CLIENT_SECRET'],
resource_owner_key=request.json.get('oauth_token'),
verifier=request.json.get('oauth_verifier'))
r = requests.post(access_token_url, auth=auth)
profile = dict(parse_qsl(r.text))
login = profile['screen_name']
if app.config['AUTH_REQUIRED'] and not db.is_user_valid(login=login):
return jsonify(status="error", message="User %s is not authorized" % login), 403
token = create_token(profile['user_id'], '@'+login, login, provider='twitter')
return jsonify(token=token)
else:
oauth = OAuth1(app.config['OAUTH2_CLIENT_ID'],
client_secret=app.config['OAUTH2_CLIENT_SECRET'],
callback_uri=app.config.get('TWITTER_CALLBACK_URL', request.headers.get('Referer', ''))
)
r = requests.post(request_token_url, auth=oauth)
oauth_token = dict(parse_qsl(r.text))
return jsonify(oauth_token)
示例10: get_redirect_to
def get_redirect_to(self):
assert self.is_redirect()
if hasattr(self.request, 'response_mode'):
response_mode = self.request.response_mode
else:
response_mode = self.request.get('response_mode')
if response_mode:
is_fragment = response_mode == 'fragment'
else:
response_types = set(self.request.response_type.split())
is_fragment = 'token' in response_types
parsed = urlparse(self.redirect_uri)
if is_fragment:
query = parsed.query
fragment = self.to_query_string()
else:
query = parse_qsl(parsed.query)
query.extend(parse_qsl(self.to_query_string()))
query = urlencode(query)
fragment = parsed.fragment
return urlunparse(parsed[:4] + (query, fragment))
示例11: url_concat
def url_concat(url, args):
"""Concatenate url and arguments regardless of whether
url has existing query parameters.
``args`` may be either a dictionary or a list of key-value pairs
(the latter allows for multiple values with the same key.
>>> url_concat("http://example.com/foo", dict(c="d"))
'http://example.com/foo?c=d'
>>> url_concat("http://example.com/foo?a=b", dict(c="d"))
'http://example.com/foo?a=b&c=d'
>>> url_concat("http://example.com/foo?a=b", [("c", "d"), ("c", "d2")])
'http://example.com/foo?a=b&c=d&c=d2'
"""
parsed_url = urlparse(url)
if isinstance(args, dict):
parsed_query = parse_qsl(parsed_url.query, keep_blank_values=True)
parsed_query.extend(args.items())
elif isinstance(args, list) or isinstance(args, tuple):
parsed_query = parse_qsl(parsed_url.query, keep_blank_values=True)
parsed_query.extend(args)
else:
err = "'args' parameter should be dict, list or tuple. Not {0}".format(
type(args))
raise TypeError(err)
final_query = urlencode(parsed_query)
url = urlunparse((
parsed_url[0],
parsed_url[1],
parsed_url[2],
parsed_url[3],
final_query,
parsed_url[5]))
return url
示例12: legacy_arguments
def legacy_arguments(self):
arguments = LegacyMultiDict()
if hasattr(self, 'uri'):
arguments.update(parse_qsl(self.parsed_uri.query))
body = getattr(self, 'body', None)
if body and getattr(self, 'content_type', None) == _FORM_CTYPE:
arguments.update(parse_qsl(self.body.decode('ascii')))
return arguments
示例13: form_arguments
def form_arguments(self):
arguments = {}
if hasattr(self, 'uri'):
arguments.update(parse_qsl(self.parsed_uri.query))
body = getattr(self, 'body', None)
if body and getattr(self, 'content_type', None) == FORM_CTYPE:
arguments.update(parse_qsl(self.body.decode('ascii')))
return arguments
示例14: assertURLEqual
def assertURLEqual(self, first, second, msg=None):
"""Check that two arguments are equivalent URLs. Ignores the order of
query arguments.
"""
first_parsed = urlparse(first)
second_parsed = urlparse(second)
self.assertEqual(first_parsed[:3], second_parsed[:3], msg)
first_qsl = sorted(parse_qsl(first_parsed.query))
second_qsl = sorted(parse_qsl(second_parsed.query))
self.assertEqual(first_qsl, second_qsl, msg)
示例15: _encode_url
def _encode_url(self, data):
query = self.query
if data:
data = native_str(data)
if isinstance(data, str):
data = parse_qsl(data)
else:
data = mapping_iterator(data)
query = parse_qsl(query)
query.extend(data)
query = urlencode(query)
self.query = query