Does urllib or urllib2 in Python 2.5 support https?

久未见 提交于 2019-12-11 03:56:14

问题


Thanks for the help in advance. I am puzzled that the same code works for python 2.6 but not 2.5. Here is the code

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())

And here is the exception I got

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>

Again, appreciate for help.


回答1:


Most likely you're 2.5 version as been compiled without ssl support.

Try this :

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

to check if ssl is present (ie, you don't catch an AttributeError)




回答2:


If you get

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

you have to install openssl, libssl-dev and rebuild python from sources



来源:https://stackoverflow.com/questions/2875402/does-urllib-or-urllib2-in-python-2-5-support-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!