Solved

Read remote file and if different Do something

Posted on 2013-10-29
11
225 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Author Comment

by:oconnork00
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Exactly what I need, thank you.
0
 
LVL 2

Author Comment

by:oconnork00
Comment Utility
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
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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 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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now