"'模块'对象没有属性' SSLContext'"使用flocker-api时出错

时间:2016-09-23 03:15:23

标签: python ssl

这是flocker api url

https://docs.clusterhq.com/en/latest/reference/api.html

我尝试使用httplib进行https连接,但我无法通过ssl验证[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:765)

顺便说一句:它显示错误:

  

AttributeError:' module'对象没有属性' SSLContext'

如果我使用python 2.7.5,我的python版本是2.7.6,

1 个答案:

答案 0 :(得分:-1)

我自己解决了我的问题,这是我的代码

import httplib
import ssl
import json
import socket
httpsConn = None
# KEY_FILE = "/etc/flocker/scio01.key"
# CERT_FILE = "/etc/flocker/scio01.crt"
# CA_FILE = "/etc/flocker/cluster.crt"

KEY_FILE = "/root/lichao_test/scio01.key"
CERT_FILE = "/root/lichao_test/scio01.crt"
CA_FILE = "/root/lichao_test/cluster.crt"

HOST = "192.168.9.14"
PORT = 4523
body = {
    "dataset_id": "a1234567-3fb9-4c1a-81ce-efeeb9f2c788",
    "primary": "c1234567-17b2-4812-beca-1434997d6c3f",
    "metadata": {
        "name": "demo",
        "owner": "lichao"
    },
    "maximum_size": 67108864
}

try:
    httpsConn = httplib.HTTPSConnection(HOST, PORT)
    sock = socket.create_connection((HOST, PORT))
    try:
        httpsConn.sock = ssl.wrap_socket(sock, ca_certs=CA_FILE, keyfile=KEY_FILE, certfile=CERT_FILE,
                                         cert_reqs=ssl.CERT_REQUIRED, ssl_version=ssl.PROTOCOL_SSLv3)
        json_body = json.dumps(body)
        # httpsConn.request(method="POST", url="https://192.168.9.14:4523/v1/configuration/datasets",
        #                   headers={"Content-type": "application/json", 'Connection': 'keep-alive'}, body=json_body)
        httpsConn.request(method="GET",url="/v1/configuration/datasets",headers={"Content-type": "application/json", 'Connection': 'keep-alive'})

        res = httpsConn.getresponse()
        body = res.read()
        status = res.status
        print "-" * 70
        print status, body
    except ssl.SSLError, e:
        print "Trying SSLv23.",e
except Exception, e:
    import traceback

    print traceback.format_exc()