簡體   English   中英

Microsoft Azure 應用程序網關阻止 python 的請求庫

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM