2014-05-17 72 views
2

我刚刚买了synology NAS(DS213J),我试图在上面运行一个python脚本。Python urlopen错误

我的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

感谢

回答

1

的问题是在#!/opt/bin/python,为了运行which python要弄清楚什么是你的Python二进制文件完整路径。

正如你可以看到你的名单都ok:

>>> 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解释器。

+0

嗨,运行'哪个python'我有'/ opt/bin/python'。 –

+0

你的蟒蛇坏了。尝试从shell运行命令,通过$ python进入python并运行命令。 – 0x90

+0

是的,那是2.5 python的东西。我刚安装了2.7,urlopen工作正常。感谢您的帮助:) –

相关问题