OS X OPENSSL urlopen()函数 ERROR:root:code for hash md5 was not found.

在学习《笨方法学python(第三版)》,ex41课程时,如下的代码:

import random
from urllib import urlopen
import sys

WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []

# --snip
#load up the words from the website
for word in urlopen(WORD_URL).readlines():
	WORDS.append(word.strip())

# --snip	

在终端中执行时,会返回错误:

ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
# -- 此处略去若干traceback
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha512

经上网查询,问题主要集中在:没找到openssl依赖库。
解决办法主要是重新安装openssl(好像说os x自带的openssl版本有问题)。
以下是重新安装openssl步骤

brew install openssl

反馈:

Warning: openssl 1.0.2q is already installed, it's just not linked
You can use `brew link openssl` to link this version.
Error: /usr/local/opt/openssl is not a valid keg

此处不知道这个keg是个什么概念。

brew link openssl --force

反馈,还是存在问题。

Warning: Refusing to link macOS-provided software: openssl
If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

安装反馈,编辑/etc/PATHS

sudo vim /etc/paths

将openssl的安装目录(我的安装目录是:/usr/local/Cellar/openssl/1.0.2q)添加到PATH,还是存在问题。
没办法,那就来“笨办法”!
把有问题的那个not a valid keg,也就是目录"usr/local/opt/openssl"删掉!
然后将/usr/local/Cellar中的openssl目录复制到/usr/local/opt中。
解决问题!
但,如果把代码中的http改为https,就会出现新的问题:

Traceback (most recent call last):
  File "ex41.py", line 29, in <module>
    for word in urlopen(WORD_URL).readlines():

# -- 此处略去若干traceback
IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)

此问题还未解决…
2018/12/27

猜你喜欢

转载自blog.csdn.net/steventian72/article/details/85299244