python request请求域名如何指定解析的ip地址?
网友回复
方法一:直接连接到 IP 地址并设置 Host 请求头
这是最直接、最常用的方式,适用于 HTTP 和 HTTPS(需谨慎处理 SSL 证书)。
将请求的 URL 替换为指定的 IP 圾址。
在请求头中设置 Host 字段为原始域名。
示例代码(HTTP):
import requests domain = "example.com" ip = "93.184.216.34" headers = { "Host": domain } response = requests.get(f"http://{ip}", headers=headers) print(response.text)
示例代码(HTTPS,需处理证书):
import requests domain = "example.com" ip = "93.184.216.34" headers = { "Host": domain } response = requests.get(f"https:/...
点击查看剩余70%