在 python 请求中传递 IP 地址而不是 url link

pass ip address in python requests instead of url link

我想在 python 请求中传递 ip address 而不是 url link

例如:而不是

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

使用:

requests.get(url="172.217.168.228")

所以基本上传递ip而不是url。

我该怎么做?

我想我也应该通过相关端口,但是如何?

我尝试了几件事,但出现了一堆错误,例如:

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

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

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

您必须告诉请求伪造主机 header,并将 URL 中的主机名替换为 IP 地址:

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

更多信息,你可以看这个link