Urllib2 runs fine if i run the program independently but throws error when i add it to a cronjob

人走茶凉 提交于 2019-12-23 01:28:17

问题


url = "www.someurl.com"

request = urllib2.Request(url,header={"User-agent" : "Mozilla/5.0"})

contentString = urllib2.url(request).read()

contentFile = StringIO.StringIO(contentString)

for i in range(0,2):
    html = contentFile.readline()

print html

The above code runs fine from commandline but if i add it to a cron job it throws the following error:

  File "/usr/lib64/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.6/urllib2.py", line 1186, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib64/python2.6/urllib2.py", line 1161, in do_open
    raise URLError(err)
urllib2.URLError: 

I did look at some tips on the other forums and tried it but it has been of no use.

Any help will be much appreciated.


回答1:


The environment variables that were used by crontab and from the command line were different.

I fixed this by adding */15 * * * * . $HOME/.profile; /path/to/command.

This made the crontab to pick up enivronment variables that were specified for the system.



来源:https://stackoverflow.com/questions/14827296/urllib2-runs-fine-if-i-run-the-program-independently-but-throws-error-when-i-add

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