Ambari启用认证,发送restful请求报错[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

本文介绍了一个使用Python进行网络请求的私有方法实现,并详细解释了如何通过异常处理来确保请求过程的健壮性。该方法能够有效应对HTTP错误及无法连接到服务器等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人采用python代码实现:

解决方法:

#私有方法,封装的目的是为了捕获异常
    def __sendRequest(self, req, desc):
        try :
            context = ssl._create_unverified_context()
            res = urllib2.urlopen(req, context = context)  ##发送请求,接受反馈的信息
        #HTTPError是URLError的子类
        except urllib2.URLError, e:
            if hasattr(e, 'code'):
                # Only HTTPError has code attribute.
                print ('The server couldn\'t fulfill the request. %s failed, error code:%s, reason:%s' % (desc, e.code, e.reason))
            elif hasattr(e, 'reason'):
                # HTTPError and URLError all have reason attribute.
                print ('We failed to reach a server. %s failed, reason:%s' % (desc, e.reason))
            return False, None
        else:
            # everything is fine
            res = res.read()  # 读取反馈的内容
            print res
            return True, res

记住:

如果有HEADER头域的Accept字段值,需设置为:
"Accept":"*/*"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值