Python 请求给出 SSL 未知协议 [英] Python requests gives SSL unknown protocol

查看:46
本文介绍了Python 请求给出 SSL 未知协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我使用的代码如下:

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)

这样做,我得到:

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)

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

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.

推荐答案

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

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

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

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

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.

这篇关于Python 请求给出 SSL 未知协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆