[newbie] problem with if then

J

Jean Dubois

I'm writing some code to check whether an url is available or not,
therefore I make use of a wget-command in Linux and then check whether
this is successful (returning a 0) or not returning an 8
However the if then statement seems to give the same result in both
cases:
Here is my code:

#!/usr/bin/env python
import sys
import os
from datetime import datetime, timedelta
today=datetime.now()
yesterday= datetime.now() - timedelta(days=1)
daybeforeyesterday= datetime.now() - timedelta(days=2)
collection = [daybeforeyesterday,yesterday,today]
for thisday in collection:
checkavailablestring='wget -q -O -
http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_'+thisday.strftime("%y%m%d")+'_JO7
/dev/null ; echo $?'
if os.system(checkavailablestring)==0:
print thisday, 'stream is available'
else:
print thisday, 'stream is not available'

Can anyone here tell me what I'm doing wrong.
Thanks in advance
jean
 
F

Fábio Santos

I'm writing some code to check whether an url is available or not,
therefore I make use of a wget-command in Linux and then check whether
this is successful (returning a 0) or not returning an 8
However the if then statement seems to give the same result in both
cases:

Which result? Failure, or success? Have you tried printing the
checkavailablestring string and running that command exactly? There may be
something wrong with the command or URL in some way, and calling os.system
instead of using the shell directly will hide any errors from you.
 
J

Jean Dubois

Which result? Failure, or success?
In case of failure I expect it to answer: stream not available
In case of success I expect it to answer: stream is available
But I get a "stream is available" in both cases even though the
command os.system(checkavailablestring) does give different answers: 0
and 8
Have you tried printing the
checkavailablestring string and running that command exactly? yes I did, no problem there
There may be something wrong with the command or URL in some way, and calling os.system
I have added a line to the script and the results below so you can see
better what's going wrong:

Here's the script once again, this time with an extra line:

#!/usr/bin/env python
import sys
import os
from datetime import datetime, timedelta
today=datetime.now()
yesterday= datetime.now() - timedelta(days=1)
daybeforeyesterday= datetime.now() - timedelta(days=2)
collection = [daybeforeyesterday,yesterday,today]
for thisday in collection:
checkavailablestring='wget -q -O -
http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_'+thisday.strftime("%y%m%d")+'_JO7
/dev/null ; echo $?'
print checkavailablestring
if os.system(checkavailablestring)==0:
print thisday, 'stream is available'
else:
print thisday, 'stream is not available'

And here is the result:

jean@antec4:~$ ./try.py
wget -q -O - http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_130607_JO7
/dev/null ; echo $?
8
2013-06-07 22:07:00.016807 stream is available
wget -q -O - http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_130608_JO7
/dev/null ; echo $?
8
2013-06-08 22:07:00.016795 stream is available
wget -q -O - http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_130609_JO7
/dev/null ; echo $?
0
2013-06-09 22:07:00.016763 stream is available
 
R

Roy Smith

Jean Dubois said:
I'm writing some code to check whether an url is available or not,
therefore I make use of a wget-command in Linux and then check whether
this is successful

In general, "shelling out" to run a command-line utility should be the
last resort. It's slower, and more complicated, than doing it in pure
python. You would only call a shell command if there was no other way.

Fortunately, in Python, there is another way. Several, in fact.

The most straight-forward is to use the built-in urllib2 module
(http://docs.python.org/2/library/urllib2.html).

If you're going to be doing anything complicated (i.e. setting optional
headers, managing cookies, etc), you probably want to be looking at the
excellent third-party module, requests (http://python-requests.org).

In any case, given your code:
#!/usr/bin/env python
import sys
import os
from datetime import datetime, timedelta
today=datetime.now()
yesterday= datetime.now() - timedelta(days=1)
daybeforeyesterday= datetime.now() - timedelta(days=2)
collection = [daybeforeyesterday,yesterday,today]
for thisday in collection:
checkavailablestring='wget -q -O -
http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_'+thisd
ay.strftime("%y%m%d")+'_JO7
/dev/null ; echo $?'
if os.system(checkavailablestring)==0:
print thisday, 'stream is available'
else:
print thisday, 'stream is not available'

I would break the debugging down into several parts. First, are you
generating the command string properly? Try printing out
checkavailablestring before you call os.system() to make sure it's what
you think it is.

Next, once you're sure you've got the correct string, run it manually in
the shell and see what it does.

Next, are you sure you're using os.system() correctly? Try running:

os.system("/bin/true")

and

os.system("/bin/false")

and make sure you get the results you think you should. But, really,
once you've done all that (and it's worth doing as an exercise), rewrite
your code to use urllib2 or requests. It'll be a lot easier.
 
A

Albert Dengg

Jean Dubois said:
The problem schould be the echo:
Since os.system returns the exit code of the shell, when chaining commands with ; it returns the exit status of the last command,in your case the echo.
So,if you really want to go with wget here,
Either drop the echo or chain with &&

Yours
Albert
Hi,
While i agree that calling wget here is not optimal....
 
J

Jean Dubois

 Jean Dubois said:
I'm writing some code to check whether an url is available or not,
therefore I make use of a wget-command in Linux and then check whether
this is successful

In general, "shelling out" to run a command-line utility should be the
last resort.  It's slower, and more complicated, than doing it in pure
python.  You would only call a shell command if there was no other way.

Fortunately, in Python, there is another way.  Several, in fact.

The most straight-forward is to use the built-in urllib2 module
(http://docs.python.org/2/library/urllib2.html).

If you're going to be doing anything complicated (i.e. setting optional
headers, managing cookies, etc), you probably want to be looking at the
excellent third-party module, requests (http://python-requests.org).

In any case, given your code:








#!/usr/bin/env python
import sys
import os
from datetime import datetime, timedelta
today=datetime.now()
yesterday= datetime.now() - timedelta(days=1)
daybeforeyesterday= datetime.now() - timedelta(days=2)
collection = [daybeforeyesterday,yesterday,today]
for thisday in collection:
     checkavailablestring='wget -q -O -
http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/E...
ay.strftime("%y%m%d")+'_JO7
/dev/null ; echo $?'
     if os.system(checkavailablestring)==0:
          print thisday, 'stream is available'
     else:
          print thisday, 'stream is not available'

I would break the debugging down into several parts.  First, are you
generating the command string properly?  Try printing out
checkavailablestring before you call os.system() to make sure it's what
you think it is.

Next, once you're sure you've got the correct string, run it manually in
the shell and see what it does.

Next, are you sure you're using os.system() correctly?  Try running:

os.system("/bin/true")

and

os.system("/bin/false")

and make sure you get the results you think you should.  But, really,
once you've done all that (and it's worth doing as an exercise), rewrite
your code to use urllib2 or requests.  It'll be a lot easier.

Could you show me how to code the example in metacode below wuth the
use of urllib2?
#!/usr/bin/env python
import urllib2
if check whether url exists succeed:
print 'url exists'
else:
print 'url does not exist'

thanks in advance
jean
 
R

Roy Smith

Jean Dubois said:
But, really,

Could you show me how to code the example in metacode below wuth the
use of urllib2?
#!/usr/bin/env python
import urllib2
if check whether url exists succeed:
print 'url exists'
else:
print 'url does not exist'

There are two basic ways Python function return status information.
Either they return something which indicates failure (such as None), or
they raise an exception.

In this case, what you want is urlopen(), which is documented at
http://docs.python.org/2/library/urllib2.html. The key piece of
information is a couple of paragraphs down, where it says, "Raises
URLError on errors".

It's not obvious from reading that whether URLError is a built-in or
not, but that's easy enough to figure out:
Traceback (most recent call last):
<class 'urllib2.URLError'>

So, that means you want something like:

#!/usr/bin/env python

import urllib2

url = "http://whatever...."
try:
urllib2.urlopen(url)
print "url exists"
except urllib2.URLError:
print "url does not exist"
 
J

Jean Dubois

There are two basic ways Python function return status information.
Either they return something which indicates failure (such as None), or
they raise an exception.

In this case, what you want is urlopen(), which is documented athttp://docs.python.org/2/library/urllib2.html.  The key piece of
information is a couple of paragraphs down, where it says, "Raises
URLError on errors".

It's not obvious from reading that whether URLError is a built-in or
not, but that's easy enough to figure out:


Traceback (most recent call last):


<class 'urllib2.URLError'>

So, that means you want something like:

#!/usr/bin/env python

import urllib2

url = "http://whatever...."
try:
   urllib2.urlopen(url)
   print "url exists"
except urllib2.URLError:
   print "url does not exist"

thanks a lot, this works like a charm

jean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top