【问题标题】:Python urlopen errorPython urlopen 错误
【发布时间】:2014-05-17 13:29:32
【问题描述】:

我刚刚购买了 Synology NAS (DS213J),我正在尝试在其上运行 python 脚本。

我的 python 脚本:

  1 #!/opt/bin/python
  2
  3 import urllib
  4 response = urllib.urlopen('http://google.com')
  5 html = response.read()
  6 print html

当我运行这个脚本时,我得到了这个输出

Traceback (most recent call last):
  File "/opt/bin/test.py", line 4, in <module>
    response = urllib.urlopen('http://google.com')
  File "/opt/lib/python2.5/urllib.py", line 82, in urlopen
    return opener.open(url)
  File "/opt/lib/python2.5/urllib.py", line 190, in open
    return getattr(self, name)(url)
  File "/opt/lib/python2.5/urllib.py", line 272, in open_http
    import httplib
  File "/opt/lib/python2.5/httplib.py", line 70, in <module>
    import mimetools
  File "/opt/lib/python2.5/mimetools.py", line 6, in <module>
    import tempfile
  File "/opt/lib/python2.5/tempfile.py", line 33, in <module>
    from random import Random as _Random
  File "/opt/lib/python2.5/random.py", line 58, in <module>
    SG_MAGICCONST = 1.0 + _log(4.5)
OverflowError: math range error

我也尝试使用 urllib2,但没有成功。

脚本:

  1 #!/opt/bin/python
  2
  3 import urllib2
  4 response = urllib2.urlopen('http://google.com')
  5 html = response.read()
  6 print html

控制台输出

Traceback (most recent call last):
  File "/opt/bin/test.py", line 3, in <module>
    import urllib2
  File "/opt/lib/python2.5/urllib2.py", line 92, in <module>
    import httplib
  File "/opt/lib/python2.5/httplib.py", line 70, in <module>
    import mimetools
  File "/opt/lib/python2.5/mimetools.py", line 6, in <module>
    import tempfile
  File "/opt/lib/python2.5/tempfile.py", line 33, in <module>
    from random import Random as _Random
  File "/opt/lib/python2.5/random.py", line 58, in <module>
    SG_MAGICCONST = 1.0 + _log(4.5)
OverflowError: math range error

我不知道这些错误是什么意思;我用谷歌搜索了一些没有成功。上面的脚本是下载电影字幕的更大脚本的一部分(我刚刚从更大的脚本中提取了错误部分并在此处发布)。

我写的是这个脚本在 Synology DS213j 上运行,因为我认为这可能与 python 安装有关。一般来说,我在为我的 synylogy 安装 ipkg 时遇到问题。我最终得到了this 教程。从教程安装引导程序后,我只运行ipkg install python 并成功安装包。我的python版本是Python 2.5.6

谢谢

【问题讨论】:

    标签: python python-2.5 urlopen


    【解决方案1】:

    问题出在#!/opt/bin/python,运行which python 以确定您的python 二进制完整路径是什么。

    如您所见,您的列表没问题:

    >>> import urllib
    >>> response = urllib.urlopen('http://google.com')
    >>> html = response.read()
    >>> print html
    <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="iw" dir="rtl"><head><meta content="/images/google_favicon_128.png" itemprop="image"><title>Google</title>[...]</body></html>
    

    我认为你应该使用 python 2.7follow

    sudo add-apt-repository ppa:fkrull/deadsnakes
    sudo apt-get update
    sudo apt-get install python2.7
    

    使用ipkg

    ipkg update
    ipkg install python27
    

    python2.7 将启动 python 解释器。

    【讨论】:

    • 嗨,运行 'which python' I've got '/opt/bin/python'。
    • 你的蟒蛇坏了。尝试从shell运行命令,通过$python进入python并运行命令。
    • 是的,那是 2.5 python 的东西。我刚刚安装了 2.7 并且 urlopen 工作正常。感谢您的帮助:)
    • @ArekWoźniak 我建议坚持使用一个包安装管理器,你应该选择最流行的。祝你好运
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 2013-10-30
    • 2019-09-26
    • 2018-05-15
    • 2015-01-06
    • 1970-01-01
    相关资源
    最近更新 更多