Read remote file and if different Do something

Hi all,

I'm new to python and working with a raspberry pi. What I'd like to do is read in the text from a remote file, wait 30 seconds, then read it again. If it's different then I want it to print out that text in the command line.

For example:

https://dl.dropboxusercontent.com/u/13419372/pasberrypi.txt

I'd like that text read in. On the first run I want the text inside the file to go to the command prompt.
30 seconds later, (loop I guess) check the url again, if it's the same do nothing, else put the contents of the text file into the command prompt and press enter.

I'd need to be able to keep the loop going constanstantly (probably best to start the script at startup).

I really appreciate any help here with this.

Thanks in advance
LVL 2
oconnork00Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
gelonidaConnect With a Mentor Commented:
Well executing the command is not an issue
However normally executing unvalidated contents from a remote server as a command is considered a security issue. (depending on the context)
Image someone were able to change the file on the server to contain
'rm -rf /'

If this is no concern for you then try:

import time
import os
import urllib2

URL = 'https://dl.dropboxusercontent.com/u/13419372/pasberrypi.txt' 
DELAY = 30


def  main_loop():
    previous_data = None
    while True:
        u = urllib2.urlopen(URL)
        data = u.read()
        u.close()
        if data != previous_data:
            print data # just for debugging
            os.system(data) # call read in string as shell command
            previous_data = data
        time.sleep(DELAY)


main_loop()

Open in new window

0
 
gelonidaCommented:
import time
import urllib2

URL = 'https://dl.dropboxusercontent.com/u/13419372/pasberrypi.txt' 
DELAY = 30


def  main_loop():
    previous_data = None
    while True:
        u = urllib2.urlopen(URL)
        data = u.read()
        u.close()
        if data != previous_data:
            print data
            previous_data = data
        time.sleep(DELAY)


main_loop()

Open in new window

0
 
oconnork00Author Commented:
Thanks gelonida,

So, Do I create a new text document, call it test.py and then run that in the command line?
0
 
gelonidaCommented:
yes you create a file with whatever name you choose,

for example. check_rmt_doc_updates.py


You copy the code inside.
I assume you're running under windows and that you are running python 2.7

If you're running python 3.x some minor changes are required. I don't have python 3 installed at the machine I'm working on at the moment so can't test, but you had for example to change line 15 into
print(data) 

Open in new window


this should then work for python 2.7 and python 3.x


In order to start the script you can just double click on the script or type it's name in a command prompt
0
 
oconnork00Author Commented:
Thanks for that.

Im actually using Raspbian on a Raspberry PI

Will that code be ok for it?
0
 
gelonidaCommented:
yeah Apologies, I forgot you mentioned the platform in the first article.

So what you have to add to your file is following as first line.

#!/usr/bin/env python

Open in new window


You also have to be sure, that the file is executable. so if you're on the command prompt you can do (assuming you're in the directory where your python script is located)

chmod +x <python_script_name>

so for example
chmod +x check_rmt_doc_updates.py
./check_rmt_doc_updates.py

Open in new window

0
 
oconnork00Author Commented:
thanks for that.
Ok it works almost exactly how I need it to run.
The only thing is I need it to ru the content of that text file on a new command line.

So, when I typed in ./filename.ty is displays the contents inside the command line, however I needed it to run it as a new command line.

Is that possible?
You see the text file contains a command, ./speech.sh which I was hoping to simply add to the $ command line and run it

Thanks so much
0
 
oconnork00Author Commented:
Hmm, and now I wonder how will the loop work. I think this might be a too big a project for this question, would you think?
0
 
oconnork00Author Commented:
Exactly what I need, thank you.
0
 
oconnork00Author Commented:
Thanks for doing this for me, it works really well and exactly what I need.

Is there any way I can make this .py script run in the background, as soon as I boot up the raspberry PI?

Thanks for doing this, I really appreciate it.
0
 
gelonidaCommented:
don't know the exact distry of the raspberry py,

but you might have a file with the name /etc/rc.local
If this file doesn't exist, then check in the raspberry forums and use the
rc-file, that is called at startup.


you could add following line to this file

/absoloute_path_to_your_script > /dev/null 2> /dev/null &
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.