Python:请求有效的urllib3错误

发布于 2025-02-10 23:55:18 字数 1364 浏览 1 评论 0原文

我有以下代码

http = urllib3.PoolManager()
resp = http.request(
    "GET",
    "https://api.nhs.uk/medicines/aciclovir",
    headers={
        "subscription-key":"XXX"
    }
)
print(resp.data)

响应是:

You don't have permission to access "http://api.nhs.uk/medicines/aciclovir" on this server

为什么它转到http://地址?

但是,当我使用下面的请求时,

url = "https://api.nhs.uk/medicines/aciclovir"

payload={}
headers = {
  'subscription-key': 'XXX'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

有什么建议是成功的吗?

更新1

这是正在执行错误发生的确切代码,是的,它确实显示了http:// error

更新2

urllib3

Listening on 0.0.0.0 7777
Connection received on *** 50054
GET / HTTP/1.1
Host: ***:7777
Accept-Encoding: identity
subscription-key: xxx
User-Agent: python-urllib3/1.26.9

请求

Listening on 0.0.0.0 7777
Connection received on *** 50095
GET / HTTP/1.1
Host: ***:7777
User-Agent: python-requests/2.28.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
subscription-key: xxx

I have the following code

http = urllib3.PoolManager()
resp = http.request(
    "GET",
    "https://api.nhs.uk/medicines/aciclovir",
    headers={
        "subscription-key":"XXX"
    }
)
print(resp.data)

The response is:

You don't have permission to access "http://api.nhs.uk/medicines/aciclovir" on this server

Why has it gone to a http:// address?

However when I use requests as below, it's a success

url = "https://api.nhs.uk/medicines/aciclovir"

payload={}
headers = {
  'subscription-key': 'XXX'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

Any suggestions?

UPDATE 1

This is the exact code that is being executed for the error to happen which yes, it does show a http:// error

enter image description here

UPDATE 2

urllib3

Listening on 0.0.0.0 7777
Connection received on *** 50054
GET / HTTP/1.1
Host: ***:7777
Accept-Encoding: identity
subscription-key: xxx
User-Agent: python-urllib3/1.26.9

requests

Listening on 0.0.0.0 7777
Connection received on *** 50095
GET / HTTP/1.1
Host: ***:7777
User-Agent: python-requests/2.28.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
subscription-key: xxx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云巢 2025-02-17 23:55:19

感谢 https://stackoverflow.com/users/240443/amadan 问题是服务器不喜欢服务器Urllib3提供的默认用户代理字符串

更改它,甚至只是分解python-urllib字符串,例如pyxthon-urllib使它起作用,

我在他们的支持下进行了此操作

With thanks from https://stackoverflow.com/users/240443/amadan the problem was that the server does not like the default user agent string supplied by urllib3

Changing it or even just breaking up the python-urllib string such as pyXthon-urllib makes it work

I have took this up with their support

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文