Solved

Read remote file and if different Do something

Posted on 2013-10-29
11
226 Views
Last Modified: 2016-08-13
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
0
Comment
Question by:oconnork00
  • 6
  • 5
11 Comments
 
LVL 16

Expert Comment

by:gelonida
ID: 39611185
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
 
LVL 2

Author Comment

by:oconnork00
ID: 39611197
Thanks gelonida,

So, Do I create a new text document, call it test.py and then run that in the command line?
0
 
LVL 16

Expert Comment

by:gelonida
ID: 39611205
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
 
LVL 2

Author Comment

by:oconnork00
ID: 39611208
Thanks for that.

Im actually using Raspbian on a Raspberry PI

Will that code be ok for it?
0
 
LVL 16

Expert Comment

by:gelonida
ID: 39611219
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
 
LVL 2

Author Comment

by:oconnork00
ID: 39611277
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
 
LVL 2

Author Comment

by:oconnork00
ID: 39611280
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
 
LVL 16

Accepted Solution

by:
gelonida earned 500 total points
ID: 39611359
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
 
LVL 2

Author Closing Comment

by:oconnork00
ID: 39611389
Exactly what I need, thank you.
0
 
LVL 2

Author Comment

by:oconnork00
ID: 39611392
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
 
LVL 16

Expert Comment

by:gelonida
ID: 39611627
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.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which Eclipse should I install now, Java, Python? 1 135
run a set of files in Python 2 53
Raspberry Pi 3 to send text message 9 116
How to use pyenv 4 39
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question