Parsing hostname and port from string or url

前端 未结 5 2088
迷失自我
迷失自我 2021-02-07 00:54

I can be given a string in any of these formats:

  • url: e.g http://www.acme.com:456

  • string: e.g www.acme.com:456, www.acme.com 456, or www.acme.co

5条回答
  •  广开言路
    2021-02-07 01:43

    I'm not that familiar with urlparse, but using regex you'd do something like:

    p = '(?:http.*://)?(?P[^:/ ]+).?(?P[0-9]*).*'
    
    m = re.search(p,'http://www.abc.com:123/test')
    m.group('host') # 'www.abc.com'
    m.group('port') # '123'
    

    Or, without port:

    m = re.search(p,'http://www.abc.com/test')
    m.group('host') # 'www.abc.com'
    m.group('port') # '' i.e. you'll have to treat this as '80'
    

    EDIT: fixed regex to also match 'www.abc.com 123'

提交回复
热议问题