uri(rfc 3986)和iris(rfc 3987)的解析和验证

rfc3987的Python项目详细描述


此模块根据RFC 3986 “Uniform Resource Identifier (URI): Generic Syntax”RFC 3987 “Internationalized Resource Identifiers (IRIs)”提供正则表达式,并且 参考文献的合成和相对分辨率实用程序。

API

匹配(字符串,rule='iri_reference')

检查字符串是否与特定规则匹配的便利函数。

返回匹配对象或不返回:

>>> assert match('%C7X', 'pct_encoded') is None
>>> assert match('%C7', 'pct_encoded')
>>> assert match('%c7', 'pct_encoded')
parse(字符串,rule='iri_reference')

根据规则将字符串解析为子组件的dict。

如果规则为none,则解析iri_引用without validation

如果regex可用,则支持任何规则;使用re规则必须 “iri_reference”或其特殊情况(“iri”,“absolute_iri”, 'irelative_ref'、'irelative_part'、'uri_reference'、'uri'、'absolute_uri', “相对参考”,“相对部分”)。

>>> d = parse('http://tools.ietf.org/html/rfc3986#appendix-A',
...           rule='URI')
>>> assert all([ d['scheme'] == 'http',
...              d['authority'] == 'tools.ietf.org',
...              d['path'] == '/html/rfc3986',
...              d['query'] == None,
...              d['fragment'] == 'appendix-A' ])
compose(**部分)
从命名部分返回uricomposed
resolve(base,uriref,strict=true,return_parts=false)

Resolves相对于基的uri引用。

Test cases

>>> base = resolve.test_cases_base
>>> for relative, resolved in resolve.test_cases.items():
...     assert resolve(base, relative) == resolved

如果return_parts为true,则返回命名部分的dict,而不是 一根绳子。

示例:

>>> assert resolve('urn:rootless', '../../name') == 'urn:name'
>>> assert resolve('urn:root/less', '../../name') == 'urn:/name'
>>> assert resolve('http://a/b', 'http:g') == 'http:g'
>>> assert resolve('http://a/b', 'http:g', strict=False) == 'http://a/g'
模式
包含有用组名的正则表达式的dict。 可编译(仅限regex),无需任何特殊编译 标志。
[bmp][u]模式[(没有)名称]
模式的替代版本。 [u]不带re模块组名的nicode字符串。 BMP仅适用于窄版本。
get_compiled_模式(规则,标志=0)

返回规则名或模板字符串的已编译模式对象。

验证用法:

>>> uri = get_compiled_pattern('^%(URI)s$')
>>> assert uri.match('http://tools.ietf.org/html/rfc3986#appendix-A')
>>> assert not get_compiled_pattern('^%(relative_ref)s$').match('#f#g')
>>> from unicodedata import lookup
>>> smp = 'urn:' + lookup('OLD ITALIC LETTER A')  # U+00010300
>>> assert not uri.match(smp)
>>> m = get_compiled_pattern('^%(IRI)s$').match(smp)

在窄版本中,非BMP字符(不正确)被排除在外:

>>> assert NARROW_BUILD == (not m)

对于解析,只有在regex可用,否则请参见解析):

>>> match = uri.match('http://tools.ietf.org/html/rfc3986#appendix-A')
>>> d = match.groupdict()
>>> if REGEX:
...     assert all([ d['scheme'] == 'http',
...                  d['authority'] == 'tools.ietf.org',
...                  d['path'] == '/html/rfc3986',
...                  d['query'] == None,
...                  d['fragment'] == 'appendix-A' ])

>>> for r in patterns.keys():
...     assert get_compiled_pattern(r)
格式模式(**名称)

返回由 rule names for URIsrule names for IRIs

另请参见模式的模块级指令,以及获取编译的模式。

要在命名捕获组中包装规则,请将其作为关键字参数传递: rule_name='组名'。默认情况下,格式化模式不包含 命名组。

模式是包含ascii的str实例(在python 2.x或3.x中) 仅限字符。

注意事项:

  • with re, named capture groups cannot occur on multiple branches of an alternation
  • with re before python 3.3, ^{tt1}$ and ^{tt2}$ escapes must be preprocessed (see issue3665)
  • on narrow builds, character ranges beyond BMP are not supported

依赖关系

有些功能需要regex

这个包的docstring在python 2.6、2.7和3.2到3.6上进行了测试。 注意,在python<;=3.2中,基本多语言平面之外的字符是 窄版本不支持(请参见issue12729)。

发行说明

1.3.8版:

  • 修复了不推荐的转义序列

版本1.3.6:

  • 修复了IPv6模式中的错误:

    >>> assert match('::0:0:0:0:0.0.0.0', 'IPv6address')
    

版本1.3.4:

  • 允许使用小写百分比编码

1.3.3版:

  • 修复了resolve中的一个错误,该错误在某些路径的开头留下“../”号。

版本1.3.2:

  • 方便功能匹配
  • 仅限窄构建的BMP模式
  • 为Python3.3改编的doctests
  • 与Python2.6兼容(感谢Thijs Janssen)

1.3.1版:

  • 一些re兼容性:获取编译的模式,解析
  • 已从setup.py要求中删除regex

1.3.0版:

  • python 3.x兼容性
  • 格式化模式

版本1.2.1:

  • 撰写,解决

支架

这是免费软件。你可以用donation来表达你的感激之情。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
传递Java处理类类型参数的方式的参数   java安卓中间层admob在logcat中导致大量GC_FOR_ALLOC释放消息   java Vert。x Http请求未将参数分配为配置   java中多线程的输出不正确   如何减少java本地线程的冗余   java不完全分块结果   使用OpenCv时java中的Mat数据类型不受支持错误   Java中的正则表达式不工作,而同一正则表达式在shell中工作   java如何从数组中删除元素?   JDBCJava。sql。SQLException:[Microsoft][ODBC Microsoft Access驱动程序]操作必须使用可更新的查询   java如何以对角线打印字符串变量的字符?   SonarQube 5.2的java自定义插件生成NoClassDefFoundError   macos使用Java应用程序打开浏览器选项卡