How to extract hostname from the given URL in Python?

后端 未结 2 1061
臣服心动
臣服心动 2021-01-27 10:43

How can I extract the hostname from: hostname:/file_name ? For example in ngs.pradhi.com:/upload, I want to extract ngs.pradhi.com from it

2条回答
  •  日久生厌
    2021-01-27 11:36

    The string in your example isn't a URL, so you won't be able to use the standard URL module (urlparse) to parse it. Here is how you can do it by hand:

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

    For SSH, take a look at paramiko.

提交回复
热议问题