【问题标题】:python: Can't use urlopen!! from urllib, urllib2, clientcookie urlopen ssl error蟒蛇:不能使用urlopen!来自 urllib、urllib2、clientcookie urlopen ssl 错误
【发布时间】:2012-08-09 18:51:33
【问题描述】:

我为访问特定站点而不是用户编写了一些代码。 它与自动登录程序非常相似。 我的程序从用户那里接收用户名和密码,并尝试使用数据和登录访问 url,返回登录结果。

这是代码。

from urllib import urlencode
from urllib2 import Request
from ClientCookie import urlopen, install_opener, build_opener

httpheaders = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1;)'}
y_url1 = 'http://www.xxx.kr/papp.jsp'
y_url2 = 'https://im.xxx.kr/sso/auth'

def check_valid_user(user_id, user_pw):
    values = {'ssousername': user_id, 'password': user_pw}
    data = urlencode(values)
    req = Request(y_url1, data, httpheaders)
    response = urlopen(req)
    the_page = response.read()
    token = the_page.split('"site2pstoretoken"')[1].split('"')[1]
    values = {'ssousername': user_id, 'password': user_pw, 'site2pstoretoken' : token}
    data = urlencode(values)
    req = Request(y_url2, data, httpheaders)
    response = urlopen(req)
    the_page = response.read()
    install_opener(build_opener())
    if the_page.find('Cyber') == -1:
        return False
    else:
        return True

当我在 Windows 桌面上运行这个程序时,它运行良好。

但是当我在我的 ubuntu apache 服务器上运行这个程序时,它不工作。 (ubuntu 11.04, python 2.7.1)

我打开 django python shell 并尝试逐行调试 python manage.py shell

response = urlopen(req)

此时出现错误。

>>>response = urlopen(req)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 824, in urlopen
    return _opener.open(url, data)
  File "/usr/lib/python2.7/urllib2.py", line 397, in open
response = meth(req, response)
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 626, in http_response
"http", request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 429, in error
result = self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 154, in http_error_302
return self.parent.open(new)
  File "/usr/lib/python2.7/urllib2.py", line 391, in open
response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _open
'_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 724, in https_open
return self.do_open(httplib.HTTPSConnection, req)
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 694, in do_open
raise URLError(err)
URLError: <urlopen error [Errno 1] _ssl.c:499: error:14077417:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert illegal parameter>

有什么问题?请帮帮我.....

【问题讨论】:

    标签: python django urllib2 urllib urlopen


    【解决方案1】:

    查看此错误报告urllib bug

    看起来像下面这样的东西可能会解决它。

     import ssl
     https_sslv3_handler = urllib.request.HTTPSHandler(context=ssl.SSLContext(ssl.PROTOCOL_SSLv3))
     opener = urllib.request.build_opener(https_sslv3_handler)
     urllib.request.install_opener(opener)
    

    【讨论】:

    • 我制作了自定义 HTTPSHandler 并使用 install_opener。 SSL 错误消失。但是 urlopen 引发 AttributeError: 'NoneType' object has no attribute 'sendall'.......
    【解决方案2】:

    我会使用 requests 代替 urllib。语法更简洁:

    r = requests.get('https://your-url', auth=('user', 'pass'))
    

    您也可以添加标题:

    headers = {'content-type': 'application/json'}
    r = requests.get('https://your-url', auth=('user', 'pass'), headers=headers)
    

    【讨论】:

    • 我只是尝试使用 requests 模块,但它也会引发 SSL 错误...我认为关键是仅在 ubuntu 服务器中引发错误。在我的台式电脑上运行良好。
    【解决方案3】:

    您的服务器可能不支持 SSLv2,Python ssl 似乎默认使用它。

    看看我在这里发布的解决方案,看看它是否对你有帮助:https://stackoverflow.com/a/24175862/41957

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 2018-01-26
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      相关资源
      最近更新 更多