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

Internet problems

Thu Apr 24, 2014 4:34 pm

Hi, I don't really know how to classify my problem , but here it goes. Generally it goes like this : When I write python code to access a webpage on the pi , it returns an error :

Code: Select all

[Errno -5] No address associated with hostname
whether is it using libraries like :

Code: Select all

import urllib2  
import smtplib
from email.mime.text import MIMEText 
etc etc
This error is exculsive to the Pi and never gives the same error on my Win7 pc.
to get wget working , I sort of have to add inet4_only = on to /etc/wgetrc .
wget works, internet works with midori, but somehow simple codes like this errors out close to 90% of the time

Code: Select all

import urllib2
response = urllib2.urlopen('http://www.pythonforbeginners.com')
print response.info()
html = response.read()
response.close()
It is as if I cannot connect to the internet via python codes or there is a timelimit to the pi trying to access the internet and if it passes the limit, it'll give me the error.
Anyone has any idea what is happening? All help is appreciated

User avatar
DougieLawson
Posts: 35529
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Internet problems

Thu Apr 24, 2014 10:36 pm

What's in /etc/resolv.conf?
What do you get from a host google.co.uk command?
What do you get from a host www.pythonforbeginners.com command?
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Internet problems

Fri Apr 25, 2014 2:10 pm

Hi,
my etc/resolv.conf returned this:

Code: Select all

nameserver 192.168.1.254
and when i tried "host google.co.uk"

Code: Select all

-bash: host: command not found

User avatar
DougieLawson
Posts: 35529
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Internet problems

Fri Apr 25, 2014 2:44 pm

OK so you don't have the host package installed.

What do these commands
route -n
ping -c3 8.8.8.8
traceroute 8.8.8.8
get?
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Internet problems

Sun Apr 27, 2014 6:20 am

ok route -n gives this:

Code: Select all

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.254   0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
ping -c3 8.8.8.8 gives :

Code: Select all

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=44 time=5.48 ms
64 bytes from 8.8.8.8: icmp_req=2 ttl=44 time=4.69 ms
64 bytes from 8.8.8.8: icmp_req=3 ttl=44 time=5.03 ms

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 4.699/5.073/5.487/0.322 ms
traceroute 8.8.8.8 gives :

Code: Select all

traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  192.168.1.254 (192.168.1.254)  0.984 ms  1.106 ms  0.889 ms
 2  * * *
 3  202.166.123.50 (202.166.123.50)  5.354 ms  5.069 ms  4.851 ms
 4  ae3-10.tp-ar11.singnet.com.sg (202.166.123.49)  52.592 ms  3.684 ms  2.954 ms
 5  202.166.121.82 (202.166.121.82)  3.526 ms  8.163 ms  3.179 ms
 6  ae4-0.pepsi.singnet.com.sg (202.166.123.70)  13.353 ms  8.028 ms  7.866 ms
 7  203.208.191.201 (203.208.191.201)  20.010 ms 203.208.190.197 (203.208.190.197)  19.160 ms 203.208.191.197 (203.208.191.197)  3.761 ms
 8  72.14.210.137 (72.14.210.137)  4.126 ms  3.499 ms  3.917 ms
 9  66.249.95.122 (66.249.95.122)  4.134 ms 66.249.95.124 (66.249.95.124)  4.255 ms  4.031 ms
10  72.14.239.61 (72.14.239.61)  6.246 ms 209.85.244.25 (209.85.244.25)  4.780 ms 64.233.174.109 (64.233.174.109)  5.025 ms
11  * * *
12  google-public-dns-a.google.com (8.8.8.8)  4.258 ms  4.924 ms  4.090 ms

User avatar
DougieLawson
Posts: 35529
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Internet problems

Sun Apr 27, 2014 7:35 am

Your system is working and is configured correctly.

You can ping google, you can resolve it's name to an ip address and reverse resolve it's ip address to a name. The rest of the internet should work for you.

So I have no idea why you're getting the strange error from your python code. Try using python3

Try this sample

Code: Select all

#!/usr/bin/python3

from urllib.request import urlopen
ip = urlopen('http://ipaddr.me/').read()
ip = ip.decode('utf-8')
print (ip)

import socket
name, alias, addresslist = socket.gethostbyaddr(ip)
print (name)
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Internet problems

Sun Apr 27, 2014 8:49 am

Thank you for being so patient DougieLawson , I tried your code and it returned this.

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 "testpy.py", line 5, in <module>
    ip = urlopen('http://ipaddr.me/').read()
  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>
could it be something in my router? or could I be hacked or something?

User avatar
DougieLawson
Posts: 35529
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Internet problems

Sun Apr 27, 2014 10:20 am

Try this one:

Code: Select all

#!/usr/bin/python2

import fcntl, socket, struct

def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]

print getHwAddr('eth0')
print getHwAddr('wlan0')
And this one:

Code: Select all

#!/usr/bin/python3
import socket
import os
gw = os.popen("ip -4 route show default").read().split()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((gw[2], 0))
ipaddr = s.getsockname()[0]
gateway = gw[2]
host = socket.gethostname()
print ("IP:", ipaddr, " GW:", gateway, " Host:", host)
I'm still not sure why you can't get the resolver to work with python.

The chances of you having been hacked are ZERO.

One option may be to build a new SDCard with the latest Raspbian (or NOOBS/Raspbian) and try running your stuff on there. You'll need to copy current stuff out to somewhere safe (off the RPi).
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Internet problems

Sun Apr 27, 2014 1:15 pm

For the first script , it only worked when I commented out the last line, but the result was

Code: Select all

b8:27:eb:f1:15:e7
And the 2nd script's output is

Code: Select all

IP: 192.168.1.11  GW: 192.168.1.254  Host: raspberrypi
Well... if there is no solution, I'll have to re install NOOBS but thanks anyway. Probably I should try out these python scripts first before I configure my samba server.

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

Re: Internet problems

Mon Apr 28, 2014 3:32 pm

Hey Dougie, one last question. Is there a flag that I can input for the urllib2 that sort of adds a longer timeout ? like, eg: give me an error only after you've tried for "x" number of seconds?

Update: Installed a fresh NOOBS(I've checked SHA-1 to be sure) , didn't configure anything at all and still it didn't work. Now I'm looking if there might be router issues?

User avatar
DougieLawson
Posts: 35529
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Internet problems

Mon Apr 28, 2014 11:56 pm

Yes you can set the timeout. Read the fine web pages @ https://docs.python.org/2.7/library/urllib2.html & https://docs.python.org/3.0/library/urllib.request.html

sudo sh -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
Try forcing your RPi to use Google's DNS for resolving domain name to IP addresses
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Troubleshooting”