Page 1 of 1

Simple python code fetch URL, generates error only on the Pi

Posted: Sun Apr 20, 2014 2:13 am
by pangren
Hi, I've been trying to execute a simple code from the pi without much successes. The weird thing is, when i input the URL into midori, it works fine. When i input the same code into my pc it works fine too.
But I tried executing the same code in the pi's terminal and on idle, the result is an error.

Code: Select all

import urllib2
response = urllib2.urlopen('http://pythonforbeginners.com/')
print response.info()
html = response.read()
response.close()
Error here :

Code: Select all

Traceback (most recent call last):
  File "testing1.py", line 3, in <module>
    response = urllib2.urlopen('http://pythonforbeginners.com/')
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 401, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 419, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1211, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1181, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno -5] No address associated with hostname>
Any ideas?

Re: Simple python code fetch URL, generates error only on th

Posted: Sun Apr 20, 2014 6:12 am
by davef21370
Try using www.pythonforbeginners.com
Browsers will usually add www to a url but urllib2 won't, the address must be accurate.

Dave.

Re: Simple python code fetch URL, generates error only on th

Posted: Sun Apr 20, 2014 6:33 am
by gkreidl
davef21370 wrote:Try using http://www.pythonforbeginners.com
Browsers will usually add www to a url but urllib2 won't, the address must be accurate.

Dave.
Browser don't add 'www' to the url; usually servers redirect to the domain name with 'www' in front. The urlopen command does not handle that redirection.

Re: Simple python code fetch URL, generates error only on th

Posted: Sun Apr 20, 2014 6:39 am
by davef21370
gkreidl wrote: Browser don't add 'www' to the url; usually servers redirect to the domain name with 'www' in front. The urlopen command does not handle that redirection.
So why, when disconnected from the internet, does safari change google.com to www.google.com before it even attempts to go there?

Dave.

Re: Simple python code fetch URL, generates error only on th

Posted: Sun Apr 20, 2014 9:47 am
by gkreidl
opening it from the cache, I suppose.
If a browser would always add that, accessing other sub domains wouldn't be possible at all.

Re: Simple python code fetch URL, generates error only on th

Posted: Mon Apr 21, 2014 2:53 pm
by pangren
Cool I'll try it , but I got fed up and I'm reformatting my pi now. I've really tried my best to mess up my pi adding wayayayay too many programmes inside than it should. Probably a conflict somewhere inside with owncloud, bittorrentsync, noip , samba.

But i'll try again and let you guys know. Thank you.