0
def statusCheck(URL,PORT):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex((URL,PORT))
    return result

Here URL = "http://127.0.0.1"

When I am calling statusCheck function with below parameter,

I got error.

BASE_URL = "http://127.0.0.1"
PORT = 7000


res = statusCheck(BASE_URL,PORT)
print (res)

Error:

Traceback (most recent call last):

File "test.py", line 15, in <module>

res = statusCheck(BASE_URL,PORT)

File "test.py", line 12, in statusCheck

result = sock.connect_ex((URL,PORT))

File "/usr/lib64/python2.7/socket.py", line 224, in meth

return getattr(self._sock,name)(*args)

socket.gaierror: [Errno -2] Name or service not known

When I use URL = "127.0.0.1". It work fine.

So my question is how I can get ipv4 address from a url which contains http://

0

1 Answer 1

1

Use str.split('/') to split the url based on the delimiter / and grab the 3rd element

>>> BASE_URL = "http://127.0.0.1"
>>> BASE_URL.split('/', 3)[2]
'127.0.0.1'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.