(Errno 101)使用crontab执行python脚本时无法访问网络

时间:2013-06-20 10:34:46

标签: python networking crontab

我想使用下面的脚本从webstie中检索一些信息。它使用命令行很好。但是,使用crontab计划时会遇到“错误101”问题。我可以找出根本原因,你能帮忙提一些建议吗?谢谢!

    req = urllib2.Request(tempURL)
    req.add_header('User-Agent', ua)
    try:
        response = urllib2.urlopen(req, timeout=10)
        data = response.read()
        print data
        response.close()
        logging.info("Successfully get information about city " + str(k))
    except urllib2.URLError, e:
        logging.info("Failed to crawl url " + tempURL)

我的python environement:

该机器是安装了python 2.6.6的redhat。为了使用python2.7.4。我从网上下载了python-2.7.4.tar并自己构建。还要将以下环境变量添加到/ etc / profile

PYHOME=$The unpacked path

在这之后,我获得了以下信息

[]$ python --version
 >>> Python 2.7.4

然后谷歌搜索,我创建一个shell脚本来设置ENV for python脚本。这个shell脚本将用于crontab计划

#!/bin/bash
export PYHOME=$Python274_location
python my.py

如果直接在命令行中执行'python my.py',则可以得到正确的结果。但是如果在crontab中安排,我将始终遇到错误。

ERROR:root:Failed to reach server. Reason: [Errno 101] Network is unreachable

在我看来,它应该与我的python ENV相关,但到目前为止还没有任何线索。

0 个答案:

没有答案