def url_join(*parts, **kwargs):
"""
Normalize url parts and join them with a slash.
adapted from: http://codereview.stackexchange.com/q/13027
"""
def concat_paths(sequence):
result = []
for path in sequence:
result.append(path)
if path.startswith('/'):
break
return '/'.join(reversed(result))
schemes, netlocs, paths, queries, fragments = zip(*(urlsplit(part) for part in reversed(parts)))
scheme = next((x for x in schemes if x), kwargs.get('scheme', 'http'))
netloc = next((x for x in netlocs if x), '')
path = concat_paths(paths)
query = queries[0]
fragment = fragments[0]
return urlunsplit((scheme, netloc, path, query, fragment))
评论列表
文章目录