 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
leke LXF regular

Joined: Mon Oct 22, 2007 6:45 pm Posts: 479 Location: Oulu, Finland
|
Posted: Mon Feb 15, 2010 11:44 am Post subject: Python WHILE problem |
|
|
Hi, I'm having some problems testing for false. If the code below doesn't connect to http://www.rssweather.com/wx/fi/pirkkala/rss.php , (for example, you are offline) it returns an error. I tested the error value with an IF conditional which seemed to return FALSE for fd.entries, so I tried to code a WHILE loop to keep trying until fd.entries returned TRUE. Here is a simplified version of my code...
| Code: | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import feedparser
df = feedparser.parse('http://www.rssweather.com/wx/fi/pirkkala/rss.php')
while (df.entries == False):
df = feedparser.parse('http://www.rssweather.com/wx/fi/pirkkala/rss.php')
else:
rss_feed_summary = str(df.entries[0].summary)
rss_feed_time = str(df.entries[0].updated)
sms = rss_feed_summary+"\n"+rss_feed_time
print sms |
If I go offline and run this, I get...
leke@leke ~/Desktop/saunasms_src $ python fpTest.py
Traceback (most recent call last):
File "fpTest.py", line 11, in <module>
rss_feed_summary = str(df.entries[0].summary)
IndexError: list index out of range
...If I go online, everything works as expected.
What am I doing wrong here? |
|
| Back to top |
|
 |
LeeNukes LXF regular

Joined: Sun Jun 21, 2009 9:11 pm Posts: 954 Location: At the bar
|
Posted: Mon Feb 15, 2010 2:22 pm Post subject: |
|
|
Am I right in thinking that you've set it so that whilst the link isn't available, go and get the link?
Won't that slow it down a bit for it to constantly check for the .rss to be available? _________________ Join GiffGaff and get £5 free credit |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Mon Feb 15, 2010 2:35 pm Post subject: |
|
|
A time.sleep call in the while loop would help, then put print df at the start of the else clause to see what see it df is actually a list. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
leke LXF regular

Joined: Mon Oct 22, 2007 6:45 pm Posts: 479 Location: Oulu, Finland
|
Posted: Mon Feb 15, 2010 3:01 pm Post subject: |
|
|
| LeeNukes wrote: | | Am I right in thinking that you've set it so that whilst the link isn't available, go and get the link? |
Yes, for some reason whilst online, the link isn't available sometimes. This is quite disastrous since the script is run on the server once a day.
| LeeNukes wrote: | | Won't that slow it down a bit for it to constantly check for the .rss to be available? |
I guess it would. Hey, how about a timer
| Code: | while (df.entries == False):
time.sleep(10)
print df
df = feedparser.parse('http://www.rssweather.com/wx/fi/pirkkala/rss.php')
else:
rss_feed_summary = str(df.entries[0].summary)
rss_feed_time = str(df.entries[0].updated) |
I added that timer and the print statement. But weirdly enough, I get the same error with no time delay and nothing printed. Does this mean python doesn't see df.entries as false? |
|
| Back to top |
|
 |
leke LXF regular

Joined: Mon Oct 22, 2007 6:45 pm Posts: 479 Location: Oulu, Finland
|
Posted: Mon Feb 15, 2010 3:04 pm Post subject: |
|
|
whoops, I see you said at the start of the else condition. This is what I get...
leke@leke ~/Desktop/saunasms_src $ python fpTest.py
{'feed': {}, 'encoding': 'utf-8', 'bozo': 1, 'version': None, 'entries': [], 'bozo_exception': URLError(gaierror(-2, 'Name or service not known'),)}
Traceback (most recent call last):
File "fpTest.py", line 15, in <module>
rss_feed_summary = str(df.entries[0].summary)
IndexError: list index out of range
If I ran df.entries i would get [] back in return. |
|
| Back to top |
|
 |
leke LXF regular

Joined: Mon Oct 22, 2007 6:45 pm Posts: 479 Location: Oulu, Finland
|
Posted: Mon Feb 15, 2010 3:43 pm Post subject: |
|
|
W00t, we cracked it guys. I messed up my False testing. Adding while (not df.entries): made it recognise that it was false and to run the true condition sequence.
Thanks I hope that works out a little better. If you are interested, here is the original script...
| Code: | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import urllib2, cookielib, re
import feedparser
df = feedparser.parse('http://www.rssweather.com/wx/fi/pirkkala/rss.php')
rss_feed_summary = str(df.entries[0].summary)
rss_feed_time = str(df.entries[0].updated)
sms = rss_feed_summary+"\n"+rss_feed_time # Message to send.
username = "" # Oma.Saunalahti account (your phonenumber in format 045...)
password = "" # Oma.Saunalahti account password
sender = "" # Your Saunalahti number (your phonenumber in format 045...)
recipients = "" # Receivers phone number (phonenumber in format 045...)
u = 'https://oma.saunalahti.fi/settings/smsSend'
d = r"username="+username+r"&login=Sisään&password="+password
c = cookielib.CookieJar()
o = urllib2.build_opener(urllib2.HTTPCookieProcessor(c))
o.addheaders = [('Referer', 'https://oma.saunalahti.fi/settings/'),
('Content-Type', 'application/x-www-form-urlencoded'),
('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')]
s = o.open(u, d)
p = s.read()
s.close()
d = "sender="+sender+"&recipients="+recipients+"&text="+sms+"&size="+str(len(sms))+"&send=Lähetä"
s = o.open(u,d)
d = s.read()
s.close()
|
|
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|