d33tah d33tah - 7 months ago 10
Python Question

Parse a git URL like 'ssh://git@gitlab.org.net:3333/org/repo.git'?

How could I easily extract hostname from a git URL like

ssh://git@gitlab.org.net:3333/org/repo.git


u = urlparse(s)


gives me

ParseResult(scheme='ssh', netloc='git@gitlab.org.net:3333', path='/org/repo.git', params='', query='', fragment='')


which means that netloc is closest to what I want and this leaves a disappointing amount of work to me.

Should I do

u.netloc.split('@')[1].split(':')[0]


or is there a library that handles it better?

Answer

The returned ParseResult has a hostname attribute:

>>> urlparse('ssh://git@gitlab.org.net:3333/org/repo.git').hostname
'gitlab.org.net'
Comments