Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47489
| From | Roy Smith <roy@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: [newbie] problem with if then |
| Date | 2013-06-09 16:23 -0400 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <roy-DD0DA2.16235509062013@news.panix.com> (permalink) |
| References | <cd1be83a-f560-4024-90b3-697a51bb1bb0@g7g2000vbv.googlegroups.com> |
In article
<cd1be83a-f560-4024-90b3-697a51bb1bb0@g7g2000vbv.googlegroups.com>,
Jean Dubois <jeandubois314@gmail.com> 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[newbie] problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 12:44 -0700
Re: [newbie] problem with if then Fábio Santos <fabiosantosart@gmail.com> - 2013-06-09 21:00 +0100
Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 13:15 -0700
Re: problem with if then Fábio Santos <fabiosantosart@gmail.com> - 2013-06-09 21:29 +0100
Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 13:51 -0700
Re: [newbie] problem with if then Roy Smith <roy@panix.com> - 2013-06-09 16:23 -0400
Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 14:00 -0700
Re: problem with if then Roy Smith <roy@panix.com> - 2013-06-09 17:35 -0400
Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-10 00:46 -0700
Re: [newbie] problem with if then Albert Dengg <albert@fsfe.org> - 2013-06-09 22:37 +0200
csiph-web