Page 1 of 1

python urllib2 error no address associated to host name

Posted: Wed Apr 23, 2014 3:38 pm
by pangren
Hi, I'm trying a simple python code here :

Code: Select all

import urllib2
response = urllib2.urlopen('http://pythonforbeginners.com/')
print response.info()
html = response.read()
response.close()
on my PC it returns this when i run it in idle :

Code: Select all

Server: nginx/1.4.1 (Ubuntu)
Date: Wed, 23 Apr 2014 15:00:27 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
But when I run it idle on my raspberry pi, it returns this:

Code: Select all

Traceback (most recent call last):
  File "python4beg.py", line 2, in <module>
    response = urllib2.urlopen('http://www.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>
The thing is, I'm very sure my internet connection is working. I put the url into midori and the page loads flawlessly, but I just don't understand what could be wrong here. Anyone has any idea? I've tried the same with other pages and it is the same result.

Re: python urllib2 error no address associated to host name

Posted: Thu Apr 24, 2014 2:00 am
by pangren
Help? Anyone? Anything I could do to troubleshoot my connection? Ports? I've been struggling on this problem for quite awhile.

Re: python urllib2 error no address associated to host name

Posted: Thu Apr 24, 2014 7:22 am
by paddyg
do you get the same error running from a terminal
$ python test_prog.py
what happens if you do the same thing with python3 ie.

Code: Select all

from urllib import request
response = request.urlopen('http://pythonforbeginners.com/')
print(response.info())
html = response.read()
response.close()
then
$ python3 test_prog.py
Did you re-start with a fresh SD card as you mentioned on the other thread?

Re: python urllib2 error no address associated to host name

Posted: Thu Apr 24, 2014 2:27 pm
by pangren
Hi paddyg,
thanks for the help, a very weird thing happened. I did quite a number of tests for the python and the python3 code. Both turned out the same way. Whether on the terminal or in IDLE, it worked but only , sometimes. I'd say out of 10 tries , it'll work once.

For Python and Python3, when it worked it took about 15 seconds to execute the code and return this:

Code: Select all

Server: nginx/1.4.1 (Ubuntu)
Date: Thu, 24 Apr 2014 14:10:47 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
But the same code when i run it again immediately in Python3, the error is as follows and it usually took about 3 seconds to error out:

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python3.2/urllib/request.py", line 1140, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/usr/lib/python3.2/http/client.py", line 970, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.2/http/client.py", line 1008, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.2/http/client.py", line 966, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.2/http/client.py", line 811, in _send_output
    self.send(msg)
  File "/usr/lib/python3.2/http/client.py", line 749, in send
    self.connect()
  File "/usr/lib/python3.2/http/client.py", line 727, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python3.2/socket.py", line 397, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "python4beg.py", line 14, in <module>
    response = request.urlopen('http://pythonforbeginners.com/')
  File "/usr/lib/python3.2/urllib/request.py", line 139, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.2/urllib/request.py", line 370, in open
    response = self._open(req, data)
  File "/usr/lib/python3.2/urllib/request.py", line 388, in _open
    '_open', req)
  File "/usr/lib/python3.2/urllib/request.py", line 348, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.2/urllib/request.py", line 1160, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/lib/python3.2/urllib/request.py", line 1143, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -5] No address associated with hostname>
As for Python2 the error is the same as my previous post. Yep I started with a new NOOBS installation but I configured a samba server with it. Thanks for your help

Re: python urllib2 error no address associated to host name

Posted: Sat May 10, 2014 5:21 pm
by edmundLi
Hey man,

I experienced a similar problem. Found out it had to do with sockets. This is the code I used:

======

import socket
import urllib

ipAdd = socket.gethostbyname('www.google.com') ## looks up google's ip address (i think)
ipAddFormatted = 'http://'+ipAdd ## formats the ip address for urlopen
f = urllib.urlopen(ipAddFormatted ).read() ##get html code
print f ## prints html codes


======

Check out more information here:

http://www.raspberrypi.org/forums/viewt ... 32&t=33167

Re: python urllib2 error no address associated to host name

Posted: Mon Jun 09, 2014 10:08 pm
by savaric
I'm having a similar problem that I've traced to getaddrinfo(). In my case I want to use pip to install the Flask framework.

Code: Select all

[email protected] / $ sudo pip install Flask
Downloading/unpacking Flask
  Cannot fetch index base URL http://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement Flask
No distributions at all found for Flask
Storing complete log in /root/.pip/pip.log
In looking at the log file I see the same error -5 No Address Associated...

Code: Select all

/usr/bin/pip run on Mon Jun  9 14:39:56 2014
Downloading/unpacking Flask

  Getting page http://pypi.python.org/simple/Flask
  Could not fetch URL http://pypi.python.org/simple/Flask: <urlopen error [Errno -5] No address associated with hostname>
  Will skip URL http://pypi.python.org/simple/Flask when looking for download links for Flask
  Getting page http://pypi.python.org/simple/
  Could not fetch URL http://pypi.python.org/simple/: <urlopen error [Errno -5] No address associated with hostname>
  Will skip URL http://pypi.python.org/simple/ when looking for download links for Flask
  Cannot fetch index base URL http://pypi.python.org/simple/

  URLs to search for versions for Flask:
  * http://pypi.python.org/simple/Flask/
  Getting page http://pypi.python.org/simple/Flask/
  Could not fetch URL http://pypi.python.org/simple/Flask/: <urlopen error [Errno -5] No address associated with hostname>
  Will skip URL http://pypi.python.org/simple/Flask/ when looking for download links for Flask
  Could not find any downloads that satisfy the requirement Flask

No distributions at all found for Flask

Exception information:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 104, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 245, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 978, in prepare_files
    url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 157, in find_requirement
    raise DistributionNotFound('No distributions at all found for %s' % req)
DistributionNotFound: No distributions at all found for Flask
I've traced into the pip code dependencies (Python 2.7):
pip
-->urllib2.urlopen()
-->httplib.HTTPConnection()
-->socket.create_connection()
-->_socket.getaddrinfo()
gaierror: [Errno -5] No address associated with hostname

I can reproduce this in Idle at any level of the call chain.
I've been banging my head against this for days. It seems to me that the getaddrinfo() implementation shouldn't be raising an exception, since socket.gethostbyname() works just fine.

Similarly wget cannot resolve pypi.python.org, but ping works just fine.

It's got to be something in my config, which is relatively fresh. Any suggestions?

I know other people have pip working on their systems. What should I be looking at?

Re: python urllib2 error no address associated to host name

Posted: Tue Jun 10, 2014 7:28 am
by paddyg
it looks to be https rather than http maybe that's it. I found that the move to pip 1.5 broke quite a lot of things in my installation scripts, mainly security, like having to explicitly flag a pre-release version number but also changing the name of pip-3.2 to pip3 etc.

I can't remember if I had issues with pip Flask but if I were you I would download https://pypi.python.org/simple/Flask/Fl ... 0.1.tar.gz extract it then (in relevant directory) $ sudo python setup.py install

Re: python urllib2 error no address associated to host name

Posted: Thu Jun 12, 2014 10:46 pm
by savaric
Thanks paddyg. I wound up hacking my socket.py file so that the create_connection() method used gethostbyname() first to resolve the hostname to an IP, then I used the IP in the original getaddrinfo() call, inspired by edmundLi's comments. This may not work under some circumstances, for instance I didn't test if I call urlopen() with a raw IP address instead of a hostname, but it fixed my problem with pip. I think there's maybe a bug in the libc6 code, maybe complicated by IPv6, but it's just intuition at this point based on a few days of online research. I'm not sure how to test that theory. Just a noob with a pi, and haven't been elbow deep in unix for about 10 years now.

Thanks for your suggestions.

Here's my hack in case someone else wants/needs to get past this sort of thing in a hurry.
/usr/lib/python2.7/socket.py (make backups!)

Code: Select all

def create_connection(.....)

original code:
host, port = address
err = None
for res in getaddrinfo(host, port, 0, SOCK_STREAM):

edited code:
hostip = gethostbyname(host)
for res in getaddrinfo(hostip, port, 0, SOCK_STREAM):
I'd be thrilled to hear from anyone with a more legitimate solution.

Thanks!

Re: python urllib2 error no address associated to host name

Posted: Fri Dec 19, 2014 5:44 am
by ilmaestro
Hope this thread hasn't gone too stale -- but have you tried running your code as root:

Code: Select all

sudo python test_prog.py
It seems libraries like urllib2 and gpio need superuser access.

Re: python urllib2 error no address associated to host name

Posted: Fri Dec 19, 2014 9:05 am
by elParaguayo
GPIO does need root access (unless you're running the pigpio demon). urllib2 does not.