pangren
Posts: 42
Joined: Thu Dec 19, 2013 2:52 pm

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

Sun Apr 20, 2014 2:13 am

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?

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

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

Sun Apr 20, 2014 6:12 am

Try using www.pythonforbeginners.com
Browsers will usually add www to a url but urllib2 won't, the address must be accurate.

Dave.
Apple say... Monkey do !!

gkreidl
Posts: 6114
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

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

Sun Apr 20, 2014 6:33 am

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.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

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

Sun Apr 20, 2014 6:39 am

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.
Apple say... Monkey do !!

gkreidl
Posts: 6114
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

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

Sun Apr 20, 2014 9:47 am

opening it from the cache, I suppose.
If a browser would always add that, accessing other sub domains wouldn't be possible at all.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

pangren
Posts: 42
Joined: Thu Dec 19, 2013 2:52 pm

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

Mon Apr 21, 2014 2:53 pm

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.

Return to “Python”