本文整理匯總了Python中six.moves.urllib.parse.urlparse方法的典型用法代碼示例。如果您正苦於以下問題:Python parse.urlparse方法的具體用法?Python parse.urlparse怎麽用?Python parse.urlparse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類six.moves.urllib.parse
的用法示例。
在下文中一共展示了parse.urlparse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_error_view
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def test_error_view(self):
client = app.test_client()
auth_redirect = client.get('/')
parsed_auth_request = dict(parse_qsl(urlparse(auth_redirect.location).query))
# fake auth error response sent to redirect_uri
error_auth_response = {
'error': 'invalid_request',
'error_description': 'test error',
'state': parsed_auth_request['state']
}
error_page = client.get('/redirect_uri?{}'.format(urlencode(error_auth_response)), follow_redirects=True)
assert json.loads(error_page.data.decode('utf-8')) == {
'error': error_auth_response['error'],
'message': error_auth_response['error_description']
}
示例2: router_login
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def router_login(base_url):
"""
Prompt for router username and password and attempt login.
Returns the username if successful or None.
"""
config = PdtoolsConfig.load()
session = requests.Session()
url_parts = urlparse(base_url)
for username, password in LoginGatherer(url_parts.netloc):
# Try to get a token for later use. Prior to 1.10, paradrop-daemon
# does not not support tokens.
_, token = send_router_login(base_url, username, password, session)
if token is not None:
config.addAccessToken(url_parts.netloc, username, token)
config.save()
return username
return None
示例3: send_router_login
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def send_router_login(request_url, username, password, session):
url_parts = urlparse(request_url)
auth_url = "{}://{}/api/v1/auth/local".format(url_parts.scheme, url_parts.netloc)
data = {
'username': username,
'password': password
}
request = requests.Request('POST', auth_url, json=data)
prepped = session.prepare_request(request)
res = session.send(prepped)
print("Server responded: {} {}".format(res.status_code, res.reason))
if res.ok:
data = res.json()
token = data['token']
return (res.status_code, token)
else:
return (res.status_code, None)
示例4: get_base_url
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def get_base_url(target):
if target.startswith("http"):
parts = urlparse(target)
if parts.scheme != 'http':
print("Warning: when specifying the Paradrop device address, "
"using a scheme ({}) other than http may result in errors."
.format(parts.scheme))
if not parts.path:
path = "/api/v1"
else:
print("Warning: when specifying the Paradrop device address, "
"using a path ({}) other than /api/v1 may result in errors."
.format(parts.scheme))
path = parts.path
return "{}://{}{}".format(parts.scheme, parts.netloc, path)
else:
return "http://{}/api/v1".format(target)
示例5: get_token
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def get_token(self):
url_parts = urlparse(self.auth_url)
print("Attempting to log in to authentication domain {}".format(url_parts.netloc))
self.username = builtins.input("Username: ")
password = getpass.getpass("Password: ")
data = {
self.param_map['username']: self.username,
self.param_map['password']: password
}
res = requests.post(self.auth_url, json=data)
try:
data = res.json()
self.token = data['token']
return self.token
except:
return None
示例6: test_default
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def test_default(self):
target = TestTableauServerResultTarget(test_config.get_tmp_path('result.job'))
target.datasource = 'test-datasource'
url = urlparse(target.get_result_url())
params = parse_qs(url.query)
eq_(url.scheme, 'tableau')
eq_(url.hostname, 'tableau.example.com')
eq_(url.path, '/' + target.datasource)
eq_(url_unquote(url.username), TestTableauServerResultTarget.username)
eq_(url_unquote(url.password), TestTableauServerResultTarget.password)
eq_(params.get('ssl'), ['true'])
eq_(params.get('ssl_verify'), ['true'])
eq_(params.get('server_version'), None)
eq_(params.get('site'), None)
eq_(params.get('project'), None)
eq_(params.get('mode'), ['replace'])
示例7: scheme
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def scheme(self):
return parse.urlparse(self.api._api_endpoint).scheme
示例8: combine_url
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def combine_url(url, page, pagetitle, **kwargs):
""" Add the specified arguments in the provided kwargs dictionary to
the given URL.
"""
url_obj = urlparse(url)
url = url_obj.geturl().replace(url_obj.query, "").rstrip("?")
query = {}
for k, v in parse_qsl(url_obj.query):
if k in query:
if isinstance(query[k], list):
query[k].append(v)
else:
query[k] = [query[k], v]
else:
query[k] = v
query[pagetitle] = page
query.update(kwargs)
args = ""
for key in query:
if isinstance(query[key], list):
for val in query[key]:
args += "&%s=%s" % (key, val)
else:
args += "&%s=%s" % (key, query[key])
return url + "?" + args[1:]
示例9: get_filename
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def get_filename(self, task, default_ext):
"""Set the path where the image will be saved.
The default strategy is to use an increasing 6-digit number as
the filename. You can override this method if you want to set custom
naming rules. The file extension is kept if it can be obtained from
the url, otherwise ``default_ext`` is used as extension.
Args:
task (dict): The task dict got from ``task_queue``.
Output:
Filename with extension.
"""
url_path = urlparse(task['file_url'])[2]
extension = url_path.split('.')[-1] if '.' in url_path else default_ext
file_idx = self.fetched_num + self.file_idx_offset
return '{:06d}.{}'.format(file_idx, extension)
示例10: _decorate_request
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def _decorate_request(self, filters, method, url, headers=None, body=None,
auth_data=None):
if auth_data is None:
auth_data = self.auth_data
token, _ = auth_data
base_url = self.base_url(filters=filters, auth_data=auth_data)
# build authenticated request
# returns new request, it does not touch the original values
_headers = copy.deepcopy(headers) if headers is not None else {}
_headers['X-Auth-Token'] = str(token)
if url is None or url == "":
_url = base_url
else:
# Join base URL and url, and remove multiple contiguous slashes
_url = "/".join([base_url, url])
parts = [x for x in urlparse.urlparse(_url)]
parts[2] = re.sub("/{2,}", "/", parts[2])
_url = urlparse.urlunparse(parts)
# no change to method or body
return str(_url), _headers, body
示例11: censor_connect_string
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def censor_connect_string(connect_string):
"""
Take a SQLAlchemy connect string and return a sanitized version
that can be written to the log without disclosing the password.
The password is replaced with "xxxx".
In case any error occurs, return "<error when censoring connect string>"
"""
try:
parsed = urlparse(connect_string)
if parsed.password is not None:
# We need to censor the ``netloc`` attribute: user:pass@host
_, host = parsed.netloc.rsplit("@", 1)
new_netloc = u'{}:{}@{}'.format(parsed.username, 'xxxx', host)
# Convert the URL to six components. netloc is component #1.
splitted = list(parsed)
splitted[1] = new_netloc
return urlunparse(splitted)
return connect_string
except Exception:
return "<error when censoring connect string>"
示例12: test_get_relative_url_with_unicode
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def test_get_relative_url_with_unicode(self):
"""Tests if it properly converts multibyte characters."""
from six.moves.urllib import parse as urlparse
self.view.request = self.request_factory.get(
'/', data={'a': 1, 'b': 2}
)
expected_path = ('/elasticsearch/.kibana/search'
'/New-Saved-Search%E3%81%82')
expected_qs = {'a': ['1'], 'b': ['2']}
url = self.view.get_relative_url(
u'/elasticsearch/.kibana/search/New-Saved-Searchあ'
)
# order of query params may change
parsed_url = urlparse.urlparse(url)
actual_path = parsed_url.path
actual_qs = urlparse.parse_qs(parsed_url.query)
self.assertEqual(actual_path, expected_path)
self.assertEqual(actual_qs, expected_qs)
示例13: connect_device
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def connect_device(uri):
"""
Initialize device with uri, and set as current device.
:param uri: an URI where to connect to device, e.g. `android://adbhost:adbport/serialno?param=value¶m2=value2`
:return: device instance
:Example:
* ``android:///`` # local adb device using default params
* ``android://adbhost:adbport/1234566?cap_method=javacap&touch_method=adb`` # remote device using custom params
* ``windows:///`` # local Windows application
* ``ios:///`` # iOS device
"""
d = urlparse(uri)
platform = d.scheme
host = d.netloc
uuid = d.path.lstrip("/")
params = dict(parse_qsl(d.query))
if host:
params["host"] = host.split(":")
dev = init_device(platform, uuid, **params)
return dev
示例14: instance
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def instance(cls, broker_id, zk_host, zk_port, zk_chroot, replicas,
partitions, message_max_bytes=1000000):
if zk_chroot is None:
zk_chroot = "afkak_" + str(uuid.uuid4()).replace("-", "_")
if "KAFKA_URI" in os.environ: # pragma: no cover
parse = urlparse(os.environ["KAFKA_URI"])
(host, port) = (parse.hostname, parse.port)
fixture = ExternalService(host, port)
else:
(host, port) = ("127.0.0.1", get_open_port())
fixture = cls(
host=host, port=port, broker_id=broker_id, zk_host=zk_host,
zk_port=zk_port, zk_chroot=zk_chroot, replicas=replicas,
partitions=partitions, message_max_bytes=message_max_bytes,
)
fixture.open()
return fixture
示例15: __init__
# 需要導入模塊: from six.moves.urllib import parse [as 別名]
# 或者: from six.moves.urllib.parse import urlparse [as 別名]
def __init__(self, requirement_string):
try:
req = REQUIREMENT.parseString(requirement_string)
except ParseException as e:
raise InvalidRequirement(
"Invalid requirement, parse error at \"{0!r}\"".format(
requirement_string[e.loc:e.loc + 8]))
self.name = req.name
if req.url:
parsed_url = urlparse.urlparse(req.url)
if not (parsed_url.scheme and parsed_url.netloc) or (
not parsed_url.scheme and not parsed_url.netloc):
raise InvalidRequirement("Invalid URL given")
self.url = req.url
else:
self.url = None
self.extras = set(req.extras.asList() if req.extras else [])
self.specifier = SpecifierSet(req.specifier)
self.marker = req.marker if req.marker else None