Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [newbie] problem with if then

Reply
Thread Tools

[newbie] problem with if then

 
 
Jean Dubois
Guest
Posts: n/a
 
      06-09-2013
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
 
Reply With Quote
 
 
 
 
Fábio Santos
Guest
Posts: n/a
 
      06-09-2013
On 9 Jun 2013 20:49, "Jean Dubois" <(E-Mail Removed)> wrote:
>
> 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.

 
Reply With Quote
 
 
 
 
Jean Dubois
Guest
Posts: n/a
 
      06-09-2013
On 9 jun, 22:00, Fábio Santos <(E-Mail Removed)> wrote:
> On 9 Jun 2013 20:49, "Jean Dubois" <(E-Mail Removed)> wrote:
>
>
>
> > 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?

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/vrtnieuw.../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/vrtnieuw.../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/vrtnieuw.../EP_130609_JO7
>/dev/null ; echo $?

0
2013-06-09 22:07:00.016763 stream is available
















 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      06-09-2013
In article
<(E-Mail Removed)>,
Jean Dubois <(E-Mail Removed)> wrote:

> 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/vrtnieuw...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.
 
Reply With Quote
 
Albert Dengg
Guest
Posts: n/a
 
      06-09-2013
Jean Dubois <(E-Mail Removed)> wrote:
....
> checkavailablestring='wget -q -O -
>http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_'+thisday.strftime("%y%m%d")+'_JO7
>>/dev/null ; echo $?'

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....
 
Reply With Quote
 
Jean Dubois
Guest
Posts: n/a
 
      06-09-2013
On 9 jun, 22:29, Fábio Santos <(E-Mail Removed)> wrote:
> On 9 Jun 2013 21:24, "Jean Dubois"
> ...> And here is the result:
>
> > jean@antec4:~$ ./try.py
> > wget -q -O -

>
> http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/E...>>/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/E...>>/dev/null ; echo $?
> > 8
> > 2013-06-08 22:07:00.016795 stream is available
> > wget -q -O -

>
> http://www.deredactie.be/cm/vrtnieuw...mas/journaal/E...
>
> > >/dev/null ; echo $?

> > 0
> > 2013-06-09 22:07:00.016763 stream is available

>
> You don't need to echo the return code. os.system should return that as an
> int.
>
> The problem might be that os.system is returning the result from the echo
> command instead.


I removed the 'echo $?' and now the thing runs like expected, thanks a
lot

jean
 
Reply With Quote
 
Jean Dubois
Guest
Posts: n/a
 
      06-09-2013
On 9 jun, 22:23, Roy Smith <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
> *Jean Dubois <(E-Mail Removed)> wrote:
>
> > 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/vrtnieuw...mas/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
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      06-09-2013
In article
<(E-Mail Removed)>,
Jean Dubois <(E-Mail Removed)> wrote:

> 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'


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:

>>> URLError

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'URLError' is not defined
>>> import urllib2
>>> urllib2.URLError

<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"
 
Reply With Quote
 
Jean Dubois
Guest
Posts: n/a
 
      06-10-2013
On 9 jun, 23:35, Roy Smith <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
> *Jean Dubois <(E-Mail Removed)> wrote:
>
> > 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'

>
> 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:
>
> >>> URLError

>
> Traceback (most recent call last):
> * File "<stdin>", line 1, in <module>
> NameError: name 'URLError' is not defined>>> import urllib2
> >>> urllib2.URLError

>
> <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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with printing a bit pattern with printf and %x matt.jaffe@gmail.com C Programming 9 04-19-2013 12:14 PM
newbie: Problem with $ and \ in strings vivek_12315 Perl Misc 2 04-12-2013 08:30 PM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
greater then / less then =?Utf-8?B?TWlrZQ==?= ASP .Net 2 11-04-2004 06:05 PM



Advertisments