在Python中如何从给定的URL中提取主机名?

2024-09-30 12:29:59 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从:hostname:/file_name中提取主机名?例如在ngs.pradhi.com:/upload中,我想从中提取{},并通过ssh.connect连接. 在

我怎么能做到呢?在


Tags: namecomconnectngssshhostnamefile主机名
2条回答

您应该考虑使用^{}模块:

This module defines a standard interface to break Uniform Resource Locator (URL) strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a “relative URL” to an absolute URL given a “base URL.”

示例:

>>> import urlparse
>>> urlparse.urlparse('http://ngs.pradhi.com/upload')
ParseResult(scheme='http', netloc='ngs.pradhi.com', path='/upload', params='', query='', fragment='')

示例中的字符串不是URL,因此无法使用标准URL模块(urlparse)来解析它。以下是您手工操作的方法:

In [43]: path = 'ngs.pradhi.com:/upload'

In [44]: path.split(':')[0]
Out[44]: 'ngs.pradhi.com'

对于SSH,请看一下^{}。在

相关问题 更多 >

    热门问题