![](/img/trans.png)
[英]Getting 403 Forbidden - Microsoft-Azure-Application-Gateway/v2 when trying to upload particular image to the Azure storage account
[英]Microsoft Azure Application Gateway blocking python's requests library
我正在尝试使用 Python 登录到托管在 Azure 中的 API,requests 是我用于 http 请求的首选库。 试过这个:
import requests
url = f"https://{SERVER}/{PLATFORM}/Account/Logon"
payload = f'LicenseType=&Password={PASSWORD}&RedirectToMyselfInCaseOfError=false&RememberMe=false&UserName={USERNAME}'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
但得到:
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>Microsoft-Azure-Application-Gateway/v2</center>
</body>
</html>
如果我使用另一个 python 库,例如 http.client,它将正常工作:
import http.client
conn = http.client.HTTPSConnection(SERVER)
payload = f'LicenseType=&Password={PASSWORD}&RedirectToMyselfInCaseOfError=false&RememberMe=false&UserName={USERNAME}'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
}
conn.request("POST", f"/{PLATFORM}/Account/Logon", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
有什么办法可以避免 Azure 将我的“请求”库请求标记为恶意?
找到原因: Curl和Python请求(get)报不同的http状态码
只需在标题中添加一个 User-Agent 字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.