Kyle Burton on Thu, 20 Apr 2000 15:15:06 -0400 (EDT)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] scripting?


> > not sure if this is a scripting questoin or not. I've accumulated a bunch of
> > URLs over the course of my internet days and I've got them all in a text
> > file, listed one per line. There's easily over a hundred.
> > 
> > I'd like to be able to quickly test them to see if they still exist (ie.
> > they don't return a 4040 error). Is there a quick way I can do this without
> > writing a dedicated application?
> 
> Sure.  I know Python can, and I'm pretty sure Perl can, also.  Here's a
> way to do it in Python (you could just type this into an interpreter
> session):
> 
> ----- BEGIN CODE -----
> 
> import urllib, string
> for line in open('myfile', 'r').readlines():
>   line = string.strip(line)
>   try:
>     u = urllib.urlopen(line)
>   except IOError:
>     print 'Bad URL:', line
>   else:
>     u.close()
> 
> ----- END CODE -----
> 
> Note that if you type this directly into an interpreter session, you must
> enter a blank line (with no spaces) at the end for the blocks to get
> closed.  The file will be processed as soon as you enter that blank line.
> 
> If you want to make it a little program, just include a hash-bang in the
> first line with the path to your Python interpreter, i.e. "#!
> /usr/bin/python".


In perl (on a command line) it can look something like:

perl -MLWP::UserAgent -pe '$u=LWP::UserAgent->new(); $_ = (($u->request(new HTTP::Request("GET",$_)))->is_success()) ? ("Good: ".$_) : ("Bad:  ".$_);'

But I'd probably write that out into a file to be exexuted instead (one-liners
like that are a bit hard to grok):

#!/usr/bin/perl
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
while(<>) {
  print $ua->request(new HTTP::Request("GET",$_))->is_success()
    ? "Good: " : "Bad: ", $_;
}

The python seems a bit more object oriented though.  I'm still trying to
find time to learn and start using python...


k

------------------------------------------------------------------------------
In every hierarchy the cream rises until it sours. 
    -- Dr. Laurence J. Peter
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------



______________________________________________________________________
Philadelphia Linux Users Group       -       http://plug.nothinbut.net
Announcements - http://lists.nothinbut.net/mail/listinfo/plug-announce
General Discussion   -   http://lists.nothinbut.net/mail/listinfo/plug