Search code examples
pythonpython-requestsrequestpython-requests-html

pass ip address in python requests instead of url link


i wanna pass ip address in python requests instead of url link.

e.g: instead of

requests.get(url="https://www.google.com")

use:

requests.get(url="172.217.168.228")

So basically pass ip instead of url.

How can i do this?

i guess i should pass relevant port either but how?

I tried couple of things but i get bunch of errors such as:

requests.exceptions.MissingSchema: Invalid URL '172.217.168.228': No schema supplied. Perhaps you meant http://172.217.168.228?

or

 File "/Users/ali/Desktop/cdn-detection/venv/lib/python3.9/site-packages/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='172.217.168.228', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1129)')))

or

File "/Users/ali/Desktop/cdn-detection/venv/lib/python3.9/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='172.217.168.228.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f9be196a0a0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))

Solution

  • You'd have to tell requests to fake the Host header, and replace the hostname in the URL with the IP address:

    requests.get("http://172.217.168.228", headers={"Host": "www.google.com"})
    

    for more info, you can see this link