Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read remote file and if different Do something

Posted on 2013-10-29
11
Medium Priority
?
243 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 17

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 17

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 17

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 17

Accepted Solution

by:
gelonida earned 2000 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 17

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

877 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