Python 2.5中的urllib或urllib2是否支持https?

时间:2010-05-20 15:43:35

标签: python https urllib2 urllib python-2.5

提前感谢您的帮助。我很困惑,相同的代码适用于python 2.6但不适用于2.5。这是代码

import cgi, urllib, urlparse, urllib2
url='https://graph.facebook.com'

req=urllib2.Request(url=url)
p=urllib2.urlopen(req)
response = cgi.parse_qs(p.read())

这是我得到的例外

Traceback (most recent call last):
  File "t2.py", line 6, in <module>
    p=urllib2.urlopen(req)
  File "/home/userx/lib/python2.5/urllib2.py", line 124, in urlopen
    return _opener.open(url, data)
  File "/home/userx/lib/python2.5/urllib2.py", line 381, in open
    response = self._open(req, data)
  File "/home/userx/lib/python2.5/urllib2.py", line 404, in _open
    'unknown_open', req)
  File "/home/userx/lib/python2.5/urllib2.py", line 360, in _call_chain
    result = func(*args)
  File "/home/userx/lib/python2.5/urllib2.py", line 1140, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib2.URLError: <urlopen error unknown url type: https>

再次,感谢您的帮助。

2 个答案:

答案 0 :(得分:8)

很可能你是没有ssl支持编译的2.5版本。

试试这个:

>>> import socket
>>> socket.ssl
<function ssl at 0xb7ce602c>

检查ssl是否存在(即,你没有捕获到AttributeError)

答案 1 :(得分:6)

如果你得到

urllib2.URLError: <urlopen error unknown url type: https>

你必须从源代码

安装openssl,libssl-dev和rebuild python