根据RFC 3986验证URI引用
项目描述
Python实现RFC 3986,包括验证和权威解析。
安装
使用pip安装rfc3986,如下所示
pip install rfc3986
许可协议
示例用法
以下为设想中的两个最常见使用场景rfc3986。
替换urlparse
解析URI并接收类似标准库的urllib.parse.urlparse
from rfc3986 import urlparse
ssh = urlparse('ssh://user@git.openstack.org:29418/openstack/glance.git')
print(ssh.scheme) # => ssh
print(ssh.userinfo) # => user
print(ssh.params) # => None
print(ssh.port) # => 29418
要创建一个带有新片段的副本,可以使用copy_with
new_ssh = ssh.copy_with(
scheme='https'
userinfo='',
port=443,
path='/openstack/glance'
)
print(new_ssh.scheme) # => https
print(new_ssh.userinfo) # => None
# etc.
严格解析URI并应用验证
要解析URI到一个方便的命名元组,可以简单地
from rfc3986 import uri_reference
example = uri_reference('http://example.com')
email = uri_reference('mailto:user@domain.com')
ssh = uri_reference('ssh://user@git.openstack.org:29418/openstack/keystone.git')
使用解析后的URI,您可以访问组件数据
print(example.scheme) # => http
print(email.path) # => user@domain.com
print(ssh.userinfo) # => user
print(ssh.host) # => git.openstack.org
print(ssh.port) # => 29418
它还可以解析包含unicode的URI
uni = uri_reference(b'http://httpbin.org/get?utf8=\xe2\x98\x83') # ☃
print(uni.query) # utf8=%E2%98%83
使用解析后的URI,您还可以验证它
if ssh.is_valid():
subprocess.call(['git', 'clone', ssh.unsplit()])
您还可以将解析后的URI规范化
mangled = uri_reference('hTTp://exAMPLe.COM')
print(mangled.scheme) # => hTTp
print(mangled.authority) # => exAMPLe.COM
normal = mangled.normalize()
print(normal.scheme) # => http
print(mangled.authority) # => example.com
但是这两个URI(功能上)是等效的
if normal == mangled:
webbrowser.open(normal.unsplit())
尽管如此,我们的路径、查询和片段是安全的
mangled = uri_reference('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth')
normal = mangled.normalize()
assert normal == 'hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth'
assert normal == 'http://example.com/Some/reallY/biZZare/pAth'
assert normal != 'http://example.com/some/really/bizzare/path'
如果您实际上不需要真实的引用对象,只想标准化URI
from rfc3986 import normalize_uri
assert (normalize_uri('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth') ==
'http://example.com/Some/reallY/biZZare/pAth')
您也可以非常简单地验证URI
from rfc3986 import is_valid_uri
assert is_valid_uri('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth')
所需组件
您可以验证特定字符串是否为有效的URI,并要求独立的组件
from rfc3986 import is_valid_uri
assert is_valid_uri('http://localhost:8774/v2/resource',
require_scheme=True,
require_authority=True,
require_path=True)
# Assert that a mailto URI is invalid if you require an authority
# component
assert is_valid_uri('mailto:user@example.com', require_authority=True) is False
如果您有一个 URIReference 的实例,您可以将相同的参数传递给 URIReference#is_valid,例如:
from rfc3986 import uri_reference
http = uri_reference('http://localhost:8774/v2/resource')
assert uri.is_valid(require_scheme=True,
require_authority=True,
require_path=True)
# Assert that a mailto URI is invalid if you require an authority
# component
mailto = uri_reference('mailto:user@example.com')
assert uri.is_valid(require_authority=True) is False
替代方案
贡献
本项目遵循并执行Python软件基金会行为准则。
如果您想贡献力量但没有考虑过错误或功能,请随时给Ian发邮件,了解您如何帮助。
本项目的git仓库由https://github.com/python-hyper/rfc3986维护。
项目详情
下载文件
下载适合您平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。
源分布
rfc3986-2.0.0.tar.gz (49.0 kB 查看哈希)
构建分布
rfc3986-2.0.0-py2.py3-none-any.whl (31.3 kB 查看哈希)