How to parse 127.0.0.1:1234 [closed]

I am trying to parse 127.0.0.1:1234 with urlparse

urlparse('127.0.0.1:1234')

and it is giving me this result

ParseResult(scheme='127.0.0.1', netloc='', path='1234', params='', query='', fragment='')

I want 127.0.0.1 in netloc field. How to do that?


Solution 1:

You need to add a scheme before the URL, http or https

URL = 'http://127.0.0.1:1234'
# Here http is the scheme and 127.0.0.1 is the netloc
parsed_obj = urlparse('http://127.0.0.1:1234')
print(parsed_obj)
# ParseResult(scheme='http', netloc='127.0.0.1:1234', path='', params='', query='', fragment='')