Urllib2 runs fine if i run the program independent

2019-06-06 07:27发布

问题:

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.