How to parse 127.0.0.1:1234

181 Views Asked by At

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?

1

There are 1 best solutions below

3
Khan Asfi Reza On

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='')