d33tah d33tah - 1 year ago 64
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


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


or is there a library that handles it better?

Answer Source

The returned ParseResult has a hostname attribute:

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