本文整理汇总了Python中urlobject.URLObject.with_scheme方法的典型用法代码示例。如果您正苦于以下问题:Python URLObject.with_scheme方法的具体用法?Python URLObject.with_scheme怎么用?Python URLObject.with_scheme使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urlobject.URLObject
的用法示例。
在下文中一共展示了URLObject.with_scheme方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_url
# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_scheme [as 别名]
def process_url(url, tracking_id):
amazon_url = URLObject(url)
amazon_url = amazon_url.with_scheme('https')
amazon_url = amazon_url.set_query_param('tag', tracking_id)
return amazon_url
示例2: authorized
# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_scheme [as 别名]
def authorized(self):
if "next" in request.args:
next_url = request.args["next"]
elif self.redirect_url:
next_url = self.redirect_url
elif self.redirect_to:
next_url = url_for(self.redirect_to)
else:
next_url = "/"
# check for error in request args
error = request.args.get("error")
if error:
error_desc = request.args.get("error_description")
error_uri = request.args.get("error_uri")
log.warning(
"OAuth 2 authorization error: %s description: %s uri: %s",
error, error_desc, error_uri,
)
oauth_error.send(self,
error=error, error_description=error_desc, error_uri=error_uri,
)
return redirect(next_url)
state_key = "{bp.name}_oauth_state".format(bp=self)
self.session._state = flask.session[state_key]
del flask.session[state_key]
secure = request.is_secure or request.headers.get("X-Forwarded-Proto", "http") == "https"
self.session.redirect_uri = url_for(
".authorized", next=request.args.get('next'), _external=True,
_scheme="https" if secure else "http",
)
url = URLObject(request.url)
if request.headers.get("X-Forwarded-Proto", "http") == "https":
url = url.with_scheme("https")
try:
token = self.session.fetch_token(
self.token_url,
authorization_response=url,
client_secret=self.client_secret,
**self.token_url_params
)
except MissingCodeError as e:
e.args = (
e.args[0],
"The redirect request did not contain the expected parameters. Instead I got: {}".format(
json.dumps(request.args)
)
)
raise
results = oauth_authorized.send(self, token=token) or []
if not any(ret == False for func, ret in results):
self.token = token
return redirect(next_url)
示例3: authorized
# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_scheme [as 别名]
def authorized(self):
next_url = request.args.get('next') or self.redirect_url or url_for(self.redirect_to)
state_key = "{bp.name}_oauth_state".format(bp=self)
self.session._state = flask.session[state_key]
del flask.session[state_key]
url = URLObject(request.url)
if request.headers.get("X-Forwarded-Proto", "http") == "https":
url = url.with_scheme("https")
token = self.session.fetch_token(
self.token_url,
authorization_response=url,
client_secret=self.client_secret,
)
self.token = token
oauth_authorized.send(self, token=token)
return redirect(next_url)
示例4: URLObjectModificationTest
# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_scheme [as 别名]
class URLObjectModificationTest(unittest.TestCase):
def setUp(self):
self.url = URLObject('https://github.com/zacharyvoase/urlobject?spam=eggs#foo')
def test_with_scheme_replaces_scheme(self):
assert (self.url.with_scheme('http') ==
'http://github.com/zacharyvoase/urlobject?spam=eggs#foo')
def test_with_netloc_replaces_netloc(self):
assert (self.url.with_netloc('example.com') ==
'https://example.com/zacharyvoase/urlobject?spam=eggs#foo')
def test_with_hostname_replaces_hostname(self):
url = URLObject('https://user:[email protected]/')
assert (url.with_hostname('example.com') ==
'https://user:[email protected]/')
def test_with_username_adds_username(self):
url = URLObject('https://github.com/')
assert url.with_username('zack') == 'https://[email protected]/'
def test_with_username_replaces_username(self):
url = URLObject('https://[email protected]/')
assert url.with_username('alice') == 'https://[email protected]/'
def test_without_username_removes_username(self):
url = URLObject('https://[email protected]/')
assert url.without_username() == 'https://github.com/'
def test_with_password_adds_password(self):
url = URLObject('https://[email protected]/')
assert url.with_password('1234') == 'https://zack:[email protected]/'
def test_with_password_raises_ValueError_when_there_is_no_username(self):
url = URLObject('https://github.com/')
assert_raises(ValueError, lambda: url.with_password('1234'))
def test_with_password_replaces_password(self):
url = URLObject('https://zack:[email protected]/')
assert url.with_password('5678') == 'https://zack:[email protected]/'
def test_without_password_removes_password(self):
url = URLObject('https://zack:[email protected]/')
assert url.without_password() == 'https://[email protected]/'
def test_with_auth_with_one_arg_adds_username(self):
url = URLObject('https://github.com/')
assert url.with_auth('zack') == 'https://[email protected]/'
def test_with_auth_with_one_arg_replaces_whole_auth_string_with_username(self):
url = URLObject('https://alice:[email protected]/')
assert url.with_auth('zack') == 'https://[email protected]/'
def test_with_auth_with_two_args_adds_username_and_password(self):
url = URLObject('https://github.com/')
assert url.with_auth('zack', '1234') == 'https://zack:[email protected]/'
def test_with_auth_with_two_args_replaces_whole_auth_string_with_username_and_password(self):
# Replaces username-only auth string
url = URLObject('https://[email protected]/')
assert url.with_auth('zack', '1234') == 'https://zack:[email protected]/'
# Replaces username and password.
url = URLObject('https://alice:[email protected]/')
assert url.with_auth('zack', '1234') == 'https://zack:[email protected]/'
def test_without_auth_removes_entire_auth_string(self):
# No username or password => no-op.
url = URLObject('https://github.com/')
assert url.without_auth() == 'https://github.com/'
# Username-only.
url = URLObject('https://[email protected]/')
assert url.without_auth() == 'https://github.com/'
# Username and password.
url = URLObject('https://alice:[email protected]/')
assert url.without_auth() == 'https://github.com/'
def test_with_port_adds_port_number(self):
assert (self.url.with_port(24) ==
'https://github.com:24/zacharyvoase/urlobject?spam=eggs#foo')
def test_with_port_replaces_port_number(self):
url = URLObject('https://github.com:59/')
assert url.with_port(67) == 'https://github.com:67/'
def test_without_port_removes_port_number(self):
url = URLObject('https://github.com:59/')
assert url.without_port() == 'https://github.com/'
def test_with_path_replaces_path(self):
assert (self.url.with_path('/dvxhouse/intessa') ==
'https://github.com/dvxhouse/intessa?spam=eggs#foo')
def test_root_goes_to_root_path(self):
assert self.url.root == 'https://github.com/?spam=eggs#foo'
def test_parent_jumps_up_one_level(self):
url = URLObject('https://github.com/zacharyvoase/urlobject')
assert url.parent == 'https://github.com/zacharyvoase/'
#.........这里部分代码省略.........
示例5: SpurlURLBuilder
# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_scheme [as 别名]
class SpurlURLBuilder(object):
def __init__(self, args, context, tags, filters):
self.args = args
self.context = context
self.tags = tags
self.filters = filters
self.autoescape = self.context.autoescape
self.url = URLObject()
def build(self):
for argument, value in self.args:
self.handle_argument(argument, value)
self.set_sensible_defaults()
url = six.text_type(self.url)
if self.autoescape:
url = escape(url)
return url
def handle_argument(self, argument, value):
argument = smart_str(argument, 'ascii')
handler_name = 'handle_%s' % argument
handler = getattr(self, handler_name, None)
if handler is not None:
value = value.resolve(self.context)
handler(value)
def handle_base(self, value):
base = self.prepare_value(value)
self.url = URLObject(base)
def handle_auth(self, value):
auth = self.prepare_value(value)
self.url = self.url.with_auth(*auth.split(':', 1))
def handle_secure(self, value):
is_secure = convert_to_boolean(value)
scheme = 'https' if is_secure else 'http'
self.url = self.url.with_scheme(scheme)
def handle_query(self, value):
query = self.prepare_value(value)
if isinstance(query, dict):
query = QueryString().set_params(**query)
self.url = self.url.with_query(QueryString(query))
def handle_query_from(self, value):
url = URLObject(value)
self.url = self.url.with_query(url.query)
def handle_add_query(self, value):
query_to_add = self.prepare_value(value)
if isinstance(query_to_add, six.string_types):
query_to_add = QueryString(query_to_add).dict
self.url = self.url.add_query_params(**query_to_add)
def handle_add_query_from(self, value):
url = URLObject(value)
self.url = self.url.add_query_params(**url.query.dict)
def handle_set_query(self, value):
query_to_set = self.prepare_value(value)
if isinstance(query_to_set, six.string_types):
query_to_set = QueryString(query_to_set).dict
self.url = self.url.set_query_params(**query_to_set)
def handle_set_query_from(self, value):
url = URLObject(value)
self.url = self.url.set_query_params(**url.query.dict)
def handle_remove_query_param(self, value):
query_to_remove = self.prepare_value(value)
self.url = self.url.del_query_param(query_to_remove)
def handle_toggle_query(self, value):
query_to_toggle = self.prepare_value(value)
if isinstance(query_to_toggle, six.string_types):
query_to_toggle = QueryString(query_to_toggle).dict
current_query = self.url.query.dict
for key, value in list(query_to_toggle.items()):
if isinstance(value, six.string_types):
value = value.split(',')
first, second = value
if key in current_query and first in current_query[key]:
self.url = self.url.set_query_param(key, second)
else:
self.url = self.url.set_query_param(key, first)
def handle_scheme(self, value):
self.url = self.url.with_scheme(value)
def handle_scheme_from(self, value):
url = URLObject(value)
self.url = self.url.with_scheme(url.scheme)
#.........这里部分代码省略.........
示例6: render
# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_scheme [as 别名]
def render(self, context):
kwargs = MultiValueDict()
for key in self.kwargs:
key = smart_str(key, 'ascii')
values = [value.resolve(context) for value in self.kwargs.getlist(key)]
kwargs.setlist(key, values)
if 'base' in kwargs:
url = URLObject.parse(kwargs['base'])
else:
url = URLObject(scheme='http')
if 'secure' in kwargs:
if convert_to_boolean(kwargs['secure']):
url = url.with_scheme('https')
else:
url = url.with_scheme('http')
if 'query' in kwargs:
query = kwargs['query']
if isinstance(query, basestring):
query = render_template_from_string_without_autoescape(query, context)
url = url.with_query(query)
if 'add_query' in kwargs:
for query_to_add in kwargs.getlist('add_query'):
if isinstance(query_to_add, basestring):
query_to_add = render_template_from_string_without_autoescape(query_to_add, context)
query_to_add = dict(decode_query(query_to_add))
for key, value in query_to_add.items():
url = url.add_query_param(key, value)
if 'scheme' in kwargs:
url = url.with_scheme(kwargs['scheme'])
if 'host' in kwargs:
url = url.with_host(kwargs['host'])
if 'path' in kwargs:
url = url.with_path(kwargs['path'])
if 'add_path' in kwargs:
for path_to_add in kwargs.getlist('add_path'):
url = url.add_path_component(path_to_add)
if 'fragment' in kwargs:
url = url.with_fragment(kwargs['fragment'])
if 'port' in kwargs:
url = url.with_port(kwargs['port'])
# sensible default
if not url.host:
url = url.with_scheme('')
# Convert the URLObject to its unicode representation
url = unicode(url)
# Handle escaping. By default, use the value of
# context.autoescape. This can be overridden by
# passing an "autoescape" keyword to the tag.
if 'autoescape' in kwargs:
autoescape = convert_to_boolean(kwargs['autoescape'])
else:
autoescape = context.autoescape
if autoescape:
url = escape(url)
if self.asvar:
context[self.asvar] = url
return ''
return url
示例7: SpurlURLBuilder
# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_scheme [as 别名]
class SpurlURLBuilder(object):
def __init__(self, args, context, tags, filters):
self.args = args
self.context = context
self.tags = tags
self.filters = filters
self.autoescape = self.context.autoescape
self.url = URLObject()
def build(self):
for argument, value in self.args:
self.handle_argument(argument, value)
try:
self.set_sensible_defaults()
url = unicode(self.url)
if self.autoescape:
url = escape(url)
url = url.replace('%20', '+')
url = url.replace('%2C', ',')
url = url.replace('&', '&')
except Exception as e:
url = self.url
return url
def handle_argument(self, argument, value):
argument = smart_str(argument, 'ascii')
handler_name = 'handle_%s' % argument
handler = getattr(self, handler_name, None)
if handler is not None:
value = value.resolve(self.context)
handler(value)
def handle_base(self, value):
base = self.prepare_value(value)
self.url = URLObject(base)
def handle_secure(self, value):
is_secure = convert_to_boolean(value)
scheme = 'https' if is_secure else 'http'
self.url = self.url.with_scheme(scheme)
def handle_query(self, value):
query = self.prepare_value(value)
if isinstance(query, dict):
query = QueryString().set_params(**query)
self.url = self.url.with_query(QueryString(query))
def handle_query_from(self, value):
url = URLObject(value)
self.url = self.url.with_query(url.query)
def handle_add_query(self, value):
query_to_add = self.prepare_value(value)
if isinstance(query_to_add, basestring):
query_to_add = QueryString(query_to_add).dict
self.url = self.url.add_query_params(**query_to_add)
def handle_add_query_from(self, value):
url = URLObject(value)
self.url = self.url.add_query_params(**url.query.dict)
def handle_set_query(self, value):
query_to_set = self.prepare_value(value)
if isinstance(query_to_set, basestring):
query_to_set = QueryString(query_to_set).dict
self.url = self.url.set_query_params(**query_to_set)
def handle_active_query(self, value):
query_to_toggle = self.prepare_value(value)
if isinstance(query_to_toggle, basestring):
query_to_toggle = QueryString(query_to_toggle).dict
current_query = self.url.query.dict
for key, value in query_to_toggle.items():
if key in current_query and value in current_query[key]:
self.url = True
else:
self.url = False
def handle_set_query_from(self, value):
url = URLObject(value)
self.url = self.url.set_query_params(**url.query.dict)
def handle_remove_query_param(self, value):
self.url = self.url.del_query_param(value)
#.........这里部分代码省略.........