为了账号安全,请及时绑定邮箱和手机立即绑定

python urlopen 怎么同时使用url,参数,header和context?

python urlopen 怎么同时使用url,参数,header和context?

尚方宝剑之说 2019-02-18 18:42:36
问题描述 python urlopen 怎么同时使用url,参数,头信息和context?原来的代码是这样的: req = urllib2.Request(url, obj, headers) opener = urllib2.urlopen(req) 现在出现[SSL: CERTIFICATE_VERIFY_FAILED]的报错 所以我需要使用下面的代码处理报错: import ssl import urllib2 context = ssl._create_unverified_context() print urllib2.urlopen("https://aa.com/", context=context).read() 请问怎么把这个context加入进去? 尝试过 print urllib2.urlopen(url=req, context=context).read() 报错: HTTP Error 503: Service Unavailable 我使用下面的代码,也是一样报错HTTP Error 503: Service Unavailable import sslssl._create_default_https_context = ssl._create_unverified_context 调试信息: Internal Server Error: /deploy/key_list_import/ Traceback (most recent call last): File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response response = self.process_exception_by_middleware(e, request) File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view return view_func(request, *args, **kwargs) File "/project/soms/deploy/views.py", line 398, in salt_key_import minions,minions_pre = sapi.list_all_key() File "/project/soms/deploy/saltapi.py", line 54, in list_all_key self.token_id() File "/project/soms/deploy/saltapi.py", line 30, in token_id content = self.postRequest(obj,prefix='/login') File "/project/soms/deploy/saltapi.py", line 42, in postRequest opener = urllib2.urlopen(req, context=ssl._create_unverified_context()) File "/usr/lib64/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout) File "/usr/lib64/python2.7/urllib2.py", line 437, in open response = meth(req, response) File "/usr/lib64/python2.7/urllib2.py", line 550, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib64/python2.7/urllib2.py", line 475, in error return self._call_chain(*args) File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/usr/lib64/python2.7/urllib2.py", line 558, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 503: Service Unavailable
查看完整描述

1 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

503是request错误导致的,检查了request有问题

查看完整回答
反对 回复 2019-03-01
  • 1 回答
  • 0 关注
  • 694 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信