Python 请求给出 SSL 未知协议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32099208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:00:20  来源:igfitidea点击:

Python requests gives SSL unknown protocol

pythonsslpython-requests

提问by pyg

I am trying to send a request to an API I have set up on an AWS machine.

我正在尝试向我在 AWS 机器上设置的 API 发送请求。

The code I use is as follows:

我使用的代码如下:

import requests
import json

report_dict = {
        "client_name": "Wayne Enterprises",
        "client_id": 123,
        "report_type": "api_testing",
        "timestamp_generated": "2015-07-29T11:00:00Z",
        "report_data": {"revenue": 9000.00}
    }
report_json = json.dumps(report_dict)
resp = requests.post("https://my-url.com:8080/my-api/reports", data=report_json,verify=False)

Doing this, I get:

这样做,我得到:

Traceback (most recent call last):
  File "art2_java_test.py", line 124, in <module>
    main()
  File "art2_java_test.py", line 9, in main
    test_post_good_data()
  File "art2_java_test.py", line 29, in test_post_good_data
    resp = requests.post("https://my-url.com:8080/my-api/reports", data=report_json,verify=Fal
se)
  File "C:\Python27\lib\site-packages\requests-2.7.0-py2.7.egg\requests\api.py",
 line 109, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.7.0-py2.7.egg\requests\api.py",
 line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.7.0-py2.7.egg\requests\sessions
.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests-2.7.0-py2.7.egg\requests\sessions
.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.7.0-py2.7.egg\requests\adapters
.py", line 428, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:5
90)

But when I send the request as http instead of https, it (usually) works fine. I've found some evidence that this can have to do with proxy servers, but I am not using one. Are there any other potential reasons for this error? This is a website only available on my company's local network, if that's relevant.

但是当我将请求作为 http 而不是 https 发送时,它(通常)工作正常。我发现了一些证据表明这可能与代理服务器有关,但我没有使用代理服务器。此错误是否还有其他潜在原因?这是一个仅在我公司的本地网络上可用的网站(如果相关)。

采纳答案by Steffen Ullrich

.... https://my-url.com:8080/my-api/reports

...But when I send the request as http instead of https, it (usually) works fine.

.... https://my-url.com:8080/my-api/reports

...但是当我将请求作为 http 而不是 https 发送时,它(通常)工作正常。

My guess is that you are trying the same port 8080 for http and https. But, servers usually listen on a single port either for http or https and not both. This means that if your client is trying to start the TLS handshake needed for https against this port it will get a plain error message back. The client then tries to interpret this error message as TLS and returns some weird error messages, because the response is not TLS at all.

我的猜测是您正在为 http 和 https 尝试相同的端口 8080。但是,服务器通常在单个端口上侦听 http 或 https,而不是同时侦听两者。这意味着,如果您的客户端尝试针对此端口启动 https 所需的 TLS 握手,它将收到一条简单的错误消息。然后客户端尝试将此错误消息解释为 TLS 并返回一些奇怪的错误消息,因为响应根本不是 TLS。