[newbie] problem with if then

Discussion in 'Python' started by Jean Dubois, Jun 9, 2013.

  1. Jean Dubois

    Jean Dubois Guest

    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
     
    Jean Dubois, Jun 9, 2013
    #1
    1. Advertising

  2. On 9 Jun 2013 20:49, "Jean Dubois" <> 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.
     
    Fábio Santos, Jun 9, 2013
    #2
    1. Advertising

  3. Jean Dubois

    Jean Dubois Guest

    Re: problem with if then

    On 9 jun, 22:00, Fábio Santos <> wrote:
    > On 9 Jun 2013 20:49, "Jean Dubois" <> 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/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
     
    Jean Dubois, Jun 9, 2013
    #3
  4. Jean Dubois

    Roy Smith Guest

    In article
    <>,
    Jean Dubois <> 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/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.
     
    Roy Smith, Jun 9, 2013
    #4
  5. Jean Dubois

    Albert Dengg Guest

    Jean Dubois <> 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....
     
    Albert Dengg, Jun 9, 2013
    #5
  6. Jean Dubois

    Jean Dubois Guest

    Re: problem with if then

    On 9 jun, 22:29, Fábio Santos <> 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/vrtnieuws/videozone/programmas/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
     
    Jean Dubois, Jun 9, 2013
    #6
  7. Jean Dubois

    Jean Dubois Guest

    Re: problem with if then

    On 9 jun, 22:23, Roy Smith <> wrote:
    > In article
    > <>,
    >  Jean Dubois <> 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/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
     
    Jean Dubois, Jun 9, 2013
    #7
  8. Jean Dubois

    Roy Smith Guest

    Re: problem with if then

    In article
    <>,
    Jean Dubois <> 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"
     
    Roy Smith, Jun 9, 2013
    #8
  9. Jean Dubois

    Jean Dubois Guest

    Re: problem with if then

    On 9 jun, 23:35, Roy Smith <> wrote:
    > In article
    > <>,
    >  Jean Dubois <> 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
     
    Jean Dubois, Jun 10, 2013
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWlrZQ==?=

    greater then / less then

    =?Utf-8?B?TWlrZQ==?=, Nov 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,627
    Kevin Spencer
    Nov 4, 2004
  2. JosephByrns

    Slow, then quick then slow

    JosephByrns, Jul 10, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    2,517
    codezilla94
    Nov 13, 2007
  3. Mufasa
    Replies:
    7
    Views:
    435
    Alexey Smirnov
    Sep 4, 2007
  4. Replies:
    0
    Views:
    336
  5. Replies:
    4
    Views:
    561
    cwdjrxyz
    Jan 17, 2006
Loading...

Share This Page