1

I am running a script at boot and continuously every second written in Python. It works fine for a limited amount of time (seems to vary ~5/10 mins) and then produced the following error bellow.

I have not tried changing the sleep time as this is a "test" script but I will be using scripts that must run every second so trying to find the right way to do it.

Google seems to yield little in the way of answers, perhaps I am using the wrong terms but I am not sure its the sleep time possibly a failure pinging the IP?

PYTHON

import time, urllib2 def internet_on(): try: response=urllib2.urlopen('http://64.233.160.94',timeout=1) return '<img class="right" src="networkon.png" width="32" height="32">' except urllib2.URLError as err: pass return '<img class="right" src="networkoff.png" width="32" height="32">' output = internet_on() f = open('/var/www/html/viv/wifiout.html', 'w') print >> f, output f.close() time.sleep(1) while True: internet_on() 

HTML

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vivarium Enviroment Control Centre</title> <link rel="stylesheet" href="style.css"> <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> <script type="text/javascript"> function updateTime() { var currentTime = new Date(); var hours = currentTime.getHours(); var minutes = currentTime.getMinutes(); var seconds = currentTime.getSeconds(); if (minutes < 10){ minutes = "0" + minutes; } if (seconds < 10){ seconds = "0" + seconds; } var v = hours + ":" + minutes + ":" + seconds + " "; if(hours > 11){ v+="PM"; } else { v+="AM" } setTimeout("updateTime()",1000); document.getElementById('time').innerHTML=v; } $("document").ready(function(){ updateTime(); setInterval(function(){ $("#wifi").load('wifiout.html'); },1000); }); function changeStatus() { var image = document.getElementById('lightStatus'); if (image.src.match("lightoff")) { image.src = "lighton.png"; } else { image.src = "lightoff.png"; } } </script> </head> <body> <div id="topbar"> <span id="time"></span> <span id="wifi"></span> <img id="lightStatus" class="right" onclick="changeStatus()" src="lightoff.png" width="32" height="32"> </div> </body> </html> 

ERROR THROWN AFTER RUNNING FOR A WHILE

pi@Vivarium:~ $ sudo python /home/pi/Desktop/wifi.py Traceback (most recent call last): File "/home/pi/Desktop/wifi.py", line 17, in <module> internet_on() File "/home/pi/Desktop/wifi.py", line 8, in internet_on urllib2.urlopen('http://64.233.160.94',timeout=1) File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 437, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 550, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 469, in error result = self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 656, in http_error_302 return self.parent.open(new, timeout=req.timeout) File "/usr/lib/python2.7/urllib2.py", line 437, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 550, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 469, in error result = self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 656, in http_error_302 return self.parent.open(new, timeout=req.timeout) File "/usr/lib/python2.7/urllib2.py", line 431, in open response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 449, in _open '_open', req) File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 1227, in http_open return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.7/urllib2.py", line 1200, in do_open r = h.getresponse(buffering=True) File "/usr/lib/python2.7/httplib.py", line 1073, in getresponse response.begin() File "/usr/lib/python2.7/httplib.py", line 415, in begin version, status, reason = self._read_status() File "/usr/lib/python2.7/httplib.py", line 371, in _read_status line = self.fp.readline(_MAXLINE + 1) File "/usr/lib/python2.7/socket.py", line 476, in readline data = self._sock.recv(self._rbufsize) socket.timeout: timed out 
10
  • Your problem seems to be related to lack of connection, which could be one of 2 issues: proxy or lack of responsiveness from the target server. The first issue is related to the fact that a script in python does not use the credentials on the machine, thus, if you have a proxy on your way, it will not authenticate and give the error. Commented Apr 4, 2016 at 16:47
  • Well no proxy setup or used so target server issue. What would be the cause? That is the IP for Google, so I used it assuming it wouldn't cause issues because of Google's size. Commented Apr 4, 2016 at 16:48
  • So, are you completely sure that the network you are using does not require proxy? Because this is a very common issue in corporate networks. Commented Apr 4, 2016 at 16:49
  • I'm at home. But I do get the very same issue and timings both at work and home. Commented Apr 4, 2016 at 16:51
  • do you realize that the python script you have provided overrides your html file with a single <img> tag that the function returns then continuously tries to retrieve the data until it takes more then a second to actually retrieve the data? You are almost certainly putting huge strain on the network connection it is very likely it takes more then a second because the server needs some time to flush some catches, how many times does internet_on() run in the while loop before it gives you the error? Commented Apr 4, 2016 at 16:55

1 Answer 1

1

Try this:

import time, urllib2 def internet_on(): returnValue = '<img class="right" src="networkon.png" width="32" height="32">' try: response=urllib2.urlopen('http://64.233.160.94',timeout=1) except: returnValue = '<img class="right" src="networkoff.png" width="32" height="32">' return returnValue while True: output = internet_on() with open('/var/www/html/viv/wifiout.html', 'w') as f: f.write(output) time.sleep(5) 
Sign up to request clarification or add additional context in comments.

6 Comments

time.sleep takes an argument in seconds, why are you waiting 1000 seconds?
Looks like JQuery 1000 eh? Ha OK let me try it out.
I juat fixed to only 5 seconds, which should be a good time for the network to breath a little between connections.
@Walter_Ritzel well the script is running! Unfortunately it will be a case of give it a while and then click the tick. Then leave it overnight and see if it's still running but like you say, 5 seconds should be enough. Thank you.
@Walter_Ritzel I have been testing some ways to lose/join the network and I've found it no longer seems to run the script correctly if there is no network when the script is ran by root on boot. Can I use my own script and just change the sleep time do you think?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.