python使用Heroku证书设置请求SSLError

时间:2018-07-18 18:26:39

标签: django heroku python-requests ssl-certificate pyopenssl

如果API要求您使用HTTPS创建SSL握手,那么在连接到API时,我在python中的请求是否需要做些特殊的事情?

我看过这里:

在这里:

但没有一个有效,我对Heroku的ACM证书似乎有效。我需要一些帮助,因为我在这里不断遇到此错误:

New Relic中出现当前错误:

// Error logs
Error message
requests.exceptions:SSLError: HTTPSConnectionPool(host='api.konnektive.com', port=443): Max retries exceeded with url: /landers/clicks/import?pageType=leadPage&ipAddress=73.160.94.162&campaignId=1&requestUri=https%3A%2F%2Fwww.workoutwarriors.com%2Fcontact%2F&loginId=**********&password=********** (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))


Sample stack trace
Traceback (most recent call last):
File "/app/.heroku/python/bin/gunicorn", line 11, in <module>
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 203, in run
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 72, in run
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 202, in run
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 544, in manage_workers
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 611, in spawn_workers
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 131, in init_process
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 124, in run
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 68, in run_for_one
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 30, in accept
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 135, in handle
File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/api/web_transaction.py", line 1280, in _nr_wsgi_application_wrapper_
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/api/web_transaction.py", line 807, in __init__
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/api/web_transaction.py", line 1169, in _nr_wsgi_application_wrapper_
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 177, in __call__
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/hooks/framework_django.py", line 423, in _nr_wrapper_BaseHandler_get_response_
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 123, in get_response
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/hooks/framework_django.py", line 220, in wrapper
File "/app/wow/middleware/tracking.py", line 35, in process_request
File "/app/wow/middleware/tracking.py", line 64, in set_url_parameters_in_session
File "/app/integrations/konnektive.py", line 1100, in konnektive_import_click
File "/app/integrations/konnektive.py", line 298, in import_click
File "/app/integrations/wrapper.py", line 17, in get
File "/app/.heroku/python/lib/python3.6/site-packages/requests/api.py", line 72, in get
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/api/external_trace.py", line 61, in dynamic_wrapper
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/api/coroutine_trace.py", line 171, in return_value
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/api/external_trace.py", line 61, in <lambda>
File "/app/.heroku/python/lib/python3.6/site-packages/requests/api.py", line 58, in request
File "/app/.heroku/python/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/api/external_trace.py", line 61, in dynamic_wrapper
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/api/coroutine_trace.py", line 171, in return_value
File "/app/.heroku/python/lib/python3.6/site-packages/newrelic/api/external_trace.py", line 61, in <lambda>
File "/app/.heroku/python/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
File "/app/.heroku/python/lib/python3.6/site-packages/requests/adapters.py", line 506, in send

在此错误之前,我得到了一个类似的错误,但区别是端口80,在pip安装pyopenssl之后,我开始获得端口443。

然后我读到一些说明我的库设置不正确的内容,a应该尝试:

pip install -m request[security]

但端口443出现相同错误。

我的证书不是无效的吗?如果您使用的是自定义域,则我使用的是Heroku给出的默认ACM证书。再次看到错误,但我认为很重要:

SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)")

这是我检查证书的方式:

openssl s_client -connect “workoutwarriors.com:443” -showcerts -servername “workoutwarriors.com”

这是我如何在django应用程序中使用python请求设置api请求的示例代码段:

这是在我的settings.py中找到的我的api URL的基础

//settings.py
KONNEKTIVE_BASE_URL = 'https://api.konnektive.com/'

这是api集成类“ NEWKonnektive”,用于从请求中调用GET请求

//konnektive.py
from wrapper import Request
import logging
logger = logging.getLogger(__name__)

class NEWKonnektive(Request):
    params = {}

    def __init__(self):

        self.auth_params = {
            'loginId': os.getenv("KONNEKTIVE_USERNAME"),
            'password': os.getenv("KONNEKTIVE_PASSWORD")
        }

    def import_click(self, *args, **kwargs):

        function_name = currentframe().f_back.f_code.co_name

        self.url = settings.KONNEKTIVE_BASE_URL + 'landers/clicks/import'
        self.params = {}

        page_type = self.set_param_or_none(kwargs, 'pageType', 'page_type', list_var=PAGE_TYPE_LIST)
        if page_type is None: return False
        ip_address = self.set_param_or_none(kwargs, 'ipAddress', 'ip_address')
        user_agent = self.set_param_or_none(kwargs, 'userAgent', 'user_agent')
        campaign_id = self.set_param_or_none(kwargs, 'campaignId', 'campaign_id')
        request_uri = self.set_param_or_none(kwargs, 'requestUri', 'request_uri')
        session_id = self.set_param_or_none(kwargs, 'sessionId', 'session_id')

        if not session_id and (not (ip_address and campaign_id and request_uri)):
        logger.error("Konnektive {}: session_id={}, ip_address={}, campaign_id={}, request_uri={}".format(function_name, session_id, ip_address, campaign_id, request_uri))
        return False
        response = self.get(params=self.params)
        logger.info("Konnektive Import Click Response: {}".format(response))
        return response

这里是api集成类NEWKonnektive,用于从请求中调用GET请求

// request wrapper.py
import json
import requests
import logging
logger = logging.getLogger(__name__)

HEADERS = {'Content-type': 'application/json'}  # Default headers


class Request:
    def get(self, params=None):
        if not self.auth_params or not self.url:
            return
        if not params:
            params = {}
        params.update(self.auth_params)
        response = requests.get(self.url, params=params, headers=HEADERS)
        logger.info('response url: {}'.format(self.url))
        logger.info('response: {}'.format(response))
        logger.info('response.ok: {}'.format(response.ok))
        logger.info('response.content: {}'.format(response.content))
        logger.info('response.header: {}'.format(response.headers))
        logger.info('response.url: {}'.format(response.url))
        return response.content

0 个答案:

没有答案