使用python

时间:2019-03-19 18:53:55

标签: python-2.7 python-requests

我正在尝试发出一个主机名与URL不同的request.post(someURL)。

我要访问的主机位于私有群集中,但可以从公共URL访问。问题是当我发出普通的发帖请求时,我得到404响应,因为主机期望使用其内部名称(hostName)。

是否可以执行诸如response = request.post(someURL,hostname = hostName,data = data)之类的方法,因此请求将转到someURL,但将hostName用作主机名而不是someURL。

1 个答案:

答案 0 :(得分:0)

“主机”几乎总是在标头中传递。尝试这样的事情

#add/remove whatever other headers you need
headers={
    'Accept':'application/json, text/plain, */*',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'en-US,en;q=0.9',
    'Host':'hostName'
}

response = request.post(someURL, headers=headers, data = data)

请注意,您可能要检查请求,因为某些站点需要主机,来源,引荐来源网址等。