gpt4 book ai didi

python - 为什么 urlparse.urlenparse 工作不一致?

转载 作者:行者123 更新时间:2023-11-28 16:44:11 25 4
gpt4 key购买 nike

当netloc为空时urlparse.urlunparse不一致:

>>> urlparse.urlunparse(('http','','test_path', None, None, None))
'http:///test_path'
>>> urlparse.urlunparse(('ftp','','test_path', None, None, None))
'ftp:///test_path'
>>> urlparse.urlunparse(('ssh','','test_path', None, None, None))
'ssh:test_path'

这是错误还是功能?我希望 urlunparse 始终如第一个示例所示,即使无法识别方案也是如此。

最佳答案

您传递给 urlunparsedata 元组具有以下组件:

scheme, netloc, url, query, fragment = data

当没有netloc,并且scheme不在uses_netloc中时,url为

    url = scheme + ':' + url

这就是 urlunparse(调用 urlunsplit)的方式 is defined :

def urlunsplit(data):
...
scheme, netloc, url, query, fragment = data
if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'):
if url and url[:1] != '/': url = '/' + url
url = '//' + (netloc or '') + url
if scheme:
url = scheme + ':' + url

请注意 'ssh' 不在 uses_netloc 中:

uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet',
'imap', 'wais', 'file', 'mms', 'https', 'shttp',
'snews', 'prospero', 'rtsp', 'rtspu', 'rsync', '',
'svn', 'svn+ssh', 'sftp','nfs','git', 'git+ssh']

如果您提供 netloc,您会得到一个以 ssh:// 开头的 url:

In [140]: urlparse.urlunparse(('ssh','netloc','test_path', None, None, None))
Out[140]: 'ssh://netloc/test_path'

关于python - 为什么 urlparse.urlenparse 工作不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15514306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com