Python 脚本 Returns 403 但在 Postman 和浏览器中 returns 200
Python Script Returns 403 but returns 200 in Postman & Browser
当我在 python 脚本中 运行 以下代码时,我收到错误消息
但是当我通过邮递员发送获取请求或在浏览器中打开地址时,我没有收到错误消息,它显示的响应状态为 <200>
我已经复制了 Google Chrome 的网络控制台中显示的所有 header,看看问题是否与我没有导入所有正确的 headers,但即使在复制每个 header.
之后问题仍然存在
运行我的API
的代码
app = FastAPI()
@app.get("/test")
def pass_parameters(parameters: Optional[str] = Query(None)):
file_name = "hey.txt"
with open(file_name, 'w') as f:
api_token = "shdsajkhdjkasjdkasjh3242341jh"
parameters= {"phrase": parameters}
print(parameters)
response = requests.get(f"https://api.state.taxes", headers={ "Accept": "application/json", "Authorization": f"Bearer {api_token}"}, params=parameters)
f.write(json.dumps(response.json(), indent=4))
response = response.json()
return JSONResponse(content=response, media_type="text/json")
if __name__ == '__main__':
uvicorn.run(app, port=8080, host='127.0.0.1')
从我的API
获取东西的代码
import requests
url = "http://127.0.0.1:8080/test"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"Connection": "keep-alive",
"Cookie": "AMCV_1B3AA45570643167F000101%40AdobeOrg=-%7CMCIDTS%7C18944%7CMCMID%%7CMCOPTOUT-1636735667s%7CNONE%7CvVersion%7C5.1.1",
"Host": "127.0.0.1:8080",
"If-Modified-Since": "Sat, 19 Mar 2022 15:13:13 GMT",
"If-None-Match": "e6ead9f56bc933ab83465",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "Windows",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"
}
response = requests.get(url, headers=headers)
print(response) --> returns <Response [403]>
但是当我打开浏览器时,网页在开发人员工具的网络控制台以及 Postman 中正常打开并显示 200 响应。我将 Postman 调用导出到 Python 并得到相同的错误。我看到了几篇 Whosebug 帖子,他们是否能够通过应用正确的 header 并在 Postman 中禁用代理来解决问题,我已经完成了这两项操作,但问题仍然存在。我不确定我是否缺少 header?
调用 localhost 而不是我的本地 IP 地址似乎解决了这个问题。像这样:
http://localhost:8080
当我在 python 脚本中 运行 以下代码时,我收到错误消息
但是当我通过邮递员发送获取请求或在浏览器中打开地址时,我没有收到错误消息,它显示的响应状态为 <200>
我已经复制了 Google Chrome 的网络控制台中显示的所有 header,看看问题是否与我没有导入所有正确的 headers,但即使在复制每个 header.
之后问题仍然存在运行我的API
的代码app = FastAPI()
@app.get("/test")
def pass_parameters(parameters: Optional[str] = Query(None)):
file_name = "hey.txt"
with open(file_name, 'w') as f:
api_token = "shdsajkhdjkasjdkasjh3242341jh"
parameters= {"phrase": parameters}
print(parameters)
response = requests.get(f"https://api.state.taxes", headers={ "Accept": "application/json", "Authorization": f"Bearer {api_token}"}, params=parameters)
f.write(json.dumps(response.json(), indent=4))
response = response.json()
return JSONResponse(content=response, media_type="text/json")
if __name__ == '__main__':
uvicorn.run(app, port=8080, host='127.0.0.1')
从我的API
获取东西的代码import requests
url = "http://127.0.0.1:8080/test"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"Connection": "keep-alive",
"Cookie": "AMCV_1B3AA45570643167F000101%40AdobeOrg=-%7CMCIDTS%7C18944%7CMCMID%%7CMCOPTOUT-1636735667s%7CNONE%7CvVersion%7C5.1.1",
"Host": "127.0.0.1:8080",
"If-Modified-Since": "Sat, 19 Mar 2022 15:13:13 GMT",
"If-None-Match": "e6ead9f56bc933ab83465",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "Windows",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"
}
response = requests.get(url, headers=headers)
print(response) --> returns <Response [403]>
但是当我打开浏览器时,网页在开发人员工具的网络控制台以及 Postman 中正常打开并显示 200 响应。我将 Postman 调用导出到 Python 并得到相同的错误。我看到了几篇 Whosebug 帖子,他们是否能够通过应用正确的 header 并在 Postman 中禁用代理来解决问题,我已经完成了这两项操作,但问题仍然存在。我不确定我是否缺少 header?
调用 localhost 而不是我的本地 IP 地址似乎解决了这个问题。像这样:
http://localhost:8080