Главная страница


ru.cgi.perl

 
 - RU.CGI.PERL ------------------------------------------------------------------
 From : Paul Kulchenko                       2:5020/400     28 Aug 2000  22:35:12
 To : All
 Subject : LWP FAQ (1/1)
 -------------------------------------------------------------------------------- 
 
 Added 1.17. How to tell LWP do not do redirect?
 
 ====================================================================
 
 LWP FAQ by Paul Kulchenko (paulclinger@yahoo.com), updated 29/06/2000
 
 News:
   fido7.ru.cgi.perl
 
 Internet: 
   http://perl.ti.cz/LWP-FAQ.txt (Andrew Zhilenko, andrew@nextra.com)
 
 ====================================================================
 
 1.1.  How to get text file (http, ftp)?
 1.2.  How to get jpeg/gif/bmp file and return it?
 1.3.  How to access password protected file?
 1.4.  How to set up REFERER and other HTTP header parameters?
 1.5.  How to get specified part of file (first MAXSIZE bytes)?
 1.6.  How to get and set up cookies?
 1.7.  How to specify proxy servers?
 1.8.  How to check for redirect?
 1.9.  How to access URL with POST method? 
 1.10. How to upload file? 
 1.11. How to access secure site (https://)
 1.12. How to install SSL module (Crypt-SSLeay) on Win32?
 1.13. How to get information about remote file?
 1.14. How to do "redirect" that works with both GET and POST?
 1.15. What is the difference between redirect for GET and POST methods?
 1.16. How to get file only if modified since specfied time?
 1.17. How to tell LWP do not do redirect?
 
 ====================================================================
 
 1.1. How to get text file (http, ftp)?
 
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $URL = 'http://www.yahoo.com/';
 my $res = LWP::UserAgent->new->request(new HTTP::Request GET => $URL);
 
 print header, $res->is_success ? $res->content : $res->status_line;
 
 ====================================================================
 
 1.2. How to get jpeg/gif/bmp file and return it?
 
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 $URL =
 'http://a100.g.akamaitech.net/7/100/70/0001/www.fool.com/art/new/butts/go99.gif'
 ;
 my $res = LWP::UserAgent->new->request(new HTTP::Request GET => $URL);
 
 binmode(STDOUT);
 print $res->is_success ? (header('image/gif'), $res->content) 
                        : (header('text/html'), $res->status_line);
 
 ====================================================================
 
 1.3. How to access password protected file?
 
 BEGIN {
   package RequestAgent;
   use LWP::UserAgent;
   @ISA = qw(LWP::UserAgent);
 
   sub new { LWP::UserAgent::new(@_); }
   sub get_basic_credentials { return 'user', 'password' }
 }
 use CGI qw(header -no_debug);
 
 my $res = RequestAgent->new->request(new HTTP::Request GET => $URL);
 
 print header, $res->is_success ? $res->content : $res->status_line;
 
 ====================================================================
 
 1.4. How to set up REFERER and other HTTP header parameters?
 
 use LWP::UserAgent;
 use HTTP::Headers;
 use CGI qw(header -no_debug);
 
 my $URL = 'http://localhost/cgi-bin/hello.cgi';
 my $res = LWP::UserAgent->new->request(
   new HTTP::Request(
     GET => $URL, 
     new HTTP::Headers referer => 'http://www.yahoo.com'
   ),
 );
 
 print header, $res->is_success ? $res->content : $res->status_line;
 
 ====================================================================
 
 1.5. How to get specified part of file (first MAXSIZE bytes)?
      (Range added as suggested by Andrey Sapozhnikov and insisted by
 Artem Chuprina)
 
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $URL = 'http://www.yahoo.com/';
 my $MAXSIZE = 1024;
 my $ENDRANGE = $MAXSIZE-1;
 
 print header;
 my $res = LWP::UserAgent->new->request(
   new HTTP::Request(
     GET => $URL,
     new HTTP::Headers Range => "bytes 0-$ENDRANGE"
   ), \&callback, $MAXSIZE,
 );
 sub callback { my($data, $response, $protocol) = @_; print $data; die }
 
 ====================================================================
 
 1.6. How to get and set up cookies?
 
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 use HTTP::Cookies;
 
 my $URL = 'http://mail.yahoo.com/';
 
 my $ua = new LWP::UserAgent;
 my $res = $ua->request(new HTTP::Request GET => $URL);
 my $cookie_jar = new HTTP::Cookies;
 $cookie_jar->extract_cookies($res);
 
 print header;
 if ($res->is_success) {
   my $req = new HTTP::Request GET => $URL;
   $cookie_jar->add_cookie_header($req);
   $res = $ua->request($req); 
   print $res->is_success ? $res->as_string : $res->status_line;
 } else {
   print $res->status_line;
 }
 
 ====================================================================
 
 1.7. How to specify proxy servers?
 
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $URL = 'http://www.yahoo.com/';
 my $ua = new LWP::UserAgent;
 
 $ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');
 $ua->proxy('gopher', 'http://proxy.sn.no:8001/');
 
 my $res = $ua->request(new HTTP::Request GET => $URL);
 
 print header, $res->is_success ? $res->content : $res->status_line;
 
 ====================================================================
 
 1.8. How to check for redirect?
 
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $URL = 'http://www.yahoo.com/';
 my $res = LWP::UserAgent->new->request(new HTTP::Request GET => $URL);
 
 print header;
 print $res->request->url if $res->previous &&
 
 $res->previous->is_redirect;
 
 ====================================================================
 
 1.9. How to access URL with POST method? 
      (simplified by Andrey Babkin)
 
 use HTTP::Request::Common;
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $URL = 'http://mail.yahoo.com/';
 my $req = POST $URL => [ 
   # default Content-Type: application/x-www-form-urlencoded
   login    => 'mylogin',
   password => 'mypassword',
 ];
 
 my $res = LWP::UserAgent->new->request($req);
 
 print header, $res->is_success ? $res->content : $res->status_line;
 
 ====================================================================
 
 1.10. How to upload file? 
 
 use HTTP::Request::Common;
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $URL = 'http://localhost/cgi-bin/survey.cgi';
 my $req = POST $URL,
   Content_Type => 'form-data',
   Content      => [ 
     name       => 'Paul Kulchenko',
     email      => 'paulclinger@yahoo.com',
     surveyfile => ['./survey.dat'], # this file will be uploaded
   ];
 
 my $res = LWP::UserAgent->new->request($req);
 
 print header, $res->is_success ? $res->content : $res->status_line;
 
 ====================================================================
 
 1.11. How to access secure site (https://)
 
 The only difference from '1.9. How to access URL with POST method?' 
 is that URL is accessed via https protocol. In that case you'll need 
 to install Net::SSL module from Crypt-SSLeay package. Detailed 
 description how to do it for ActiveState's Perl is in 
 '1.12. How to install SSL module (Crypt-SSLeay) on Win32?'
 
 use HTTP::Request::Common;
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $URL = 'https://www.secureserver.com/abc/form.htm';
 my $req = POST $URL => [ 
   # default Content-Type: application/x-www-form-urlencoded
   login    => 'mylogin',
   password => 'mypassword',
 ];
 
 # if you need to login with Basic Authentication Scheme
 
 $req->authorization_basic('my_username','my_password');
 
 my $res = LWP::UserAgent->new->request($req);
 
 print header, $res->is_success ? $res->content : $res->status_line;
 
 ====================================================================
 
 1.12. How to install SSL module (Crypt-SSLeay) on Win32?
       (from Kras Gadjokov, kras_gadjokov@yahoo.com)
 
 1) Install ActivePerl for Win32 from www.activestate.com
 
 Make sure you install version that has ActiveState's
 package (ZIP-file) for the Crypt-SSLeay module.
 Note: The lates ActivePerl vestion 5.6.0 *doesn't*
 have this, use the previous version - 5.005.
 
 2) Install Crypt-SSLeay package - find Crypt-SSLeay.zip in
 http://www.ActiveState.com/ppmpackages/5.005/zips/
 
 Important note: For ActiveState's Perl for Win32, you
 do NOT need to get any other package or file in order
 to install Crypt-SSLeay.
 One can get confused from the documentation because it
 doesn't consider ActiveState's packages, which are
 "pre-packaged" with binaries (DLLs) for Win32. No need
 to compile or/and install OpenSSL or Net-SSLeay
 packages, binaries or other libraries.
 All you need is to install the Crypt-SSLeay module is
 one single file - Crypt-SSLeay.zip.
 
 ActivePerl has a package manager, PPM. Didn't work
 from inside of the interface (no time to figure out
 why), so I did the following:
 
 - downloded Crypt-SSLeay.zip
 - unzipped, changed to the directory where the PPD-file is
 - run command 
       ppm install Crypt-SSLeay.PPD
 
 3) Testing:
 *highly* recommend to use another test line *instead*
 of the one from the documnation on Crypt::SSLeay 
 The original test line
 
      lwp-request https://www.nodeworks.com
 
 gave me some message "... method not supported". 
 
 My testing line that worked:
 
      lwp-request -m GET https://www.nodeworks.com
 
 ====================================================================
 
 1.13. How to get information about remote file?
 
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $url = 'http://yahoo.com/';
 my $res = LWP::UserAgent->new->request(HTTP::Request->new(HEAD =>
 $url)); 
 
 print header;
 
 local $\ = "\n";
 if ($res->is_success) {
   $res->previous && 
   $res->previous->is_redirect and print 'redirected: ',
 
 $res->request->url;
 
   $res->last_modified         and print 'modified: ',
 scalar(localtime($res->last_modified)); 
   $res->content_length        and print 'size: ', $res->content_length;
 } else {
   print $res->status_line;
 }
 
 ====================================================================
 
 1.14. How to do "redirect" that works with both GET and POST?
       (Is it the same as "forward" does in Java?)
 
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $URL = 'http://www.yahoo.com/';
 my $MAXSIZE = 1024;
 
 local $| = 'non buffered';
 
 print header;
 my $res = LWP::UserAgent->new->request(
   new HTTP::Request(GET => $URL), # or POST depending on current method
   sub { my($data, $response, $protocol) = @_; print $data }, # <=
 callback
   $MAXSIZE,
 );
 
 ====================================================================
 
 1.15. What is the difference between redirect for GET and POST methods?
 
 If you send GET request and server responds with Redirect status (3??)
 LWP
 will make redirect for you automatically. You can chech whether you were
 redirected with following code:
 
 # -- code begin --
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $res = LWP::UserAgent->new->request(HTTP::Request->new(GET =>
 'http://yahoo.com/')); 
 
 print header, 
   $res->is_success && $res->previous && $res->previous->is_redirect 
     ? ("indirect link from ", $res->request->url) : ("direct link");
 # -- code end --
 
 This code will show you only the LATEST redirect, so if you want to get
 all 
 information follow the same scheme, get previous response, get
 is_redirect
 status etc.
 
 Situation with POST method is little bit different. According to RFC
 POST 
 requests should not be redirected automatically without user
 intervention.
 So, when you send POST request LWP won't redirect you, but return you
 response where is_redirect status is true. You should check it and if 
 it's true, send another request with the same data and new URL like:
 ====================================================================
 
 1.16. How to get file only if modified since specfied time?
 
 use LWP::UserAgent;
 use CGI qw(header -no_debug);
 
 my $URL = 'http://www.gutatelecom.ru/shop.php3';
 my $res = LWP::UserAgent->new->request(
   new HTTP::Request(
     GET => $URL,
     new HTTP::Headers 'If-Modified-Since' =>
 HTTP::Date::time2str(time-100),
   ),
 );
 
 # you should get status 304 if content was not modified since specified
 time
 # behavior for static and dynamic pages depends on webserver
 print header, $res->is_success ? $res->content : $res->status_line;
 
 ====================================================================
 
 1.17. How to tell LWP do not do redirect?
 
 Add BEGIN { local $^W; sub LWP::UserAgent::redirect_ok {0}; } somewhere
 after use LWP::UserAgent;
 
 ====================================================================
 
 Best wishes, Paul.
 --- ifmail v.2.15dev5
  * Origin: Trans World Airlines (TWA) (2:5020/400)
 
 

Вернуться к списку тем, сортированных по: возрастание даты  уменьшение даты  тема  автор 

 Тема:    Автор:    Дата:  
 LWP FAQ (1/1)   Paul Kulchenko   28 Aug 2000 22:35:12 
 Re: LWP FAQ (1/1)   Sergey Suhov   29 Aug 2000 01:52:23 
 Re: LWP FAQ (1/1)   Sergey Suhov   29 Aug 2000 10:48:46 
 Re: LWP FAQ (1/1)   Paul Kulchenko   30 Aug 2000 19:36:04 
 LWP FAQ (1/1)   Natalya Levit   29 Aug 2000 21:22:34 
 Re: LWP FAQ (1/1)   Paul Kulchenko   30 Aug 2000 19:49:41 
 Re: LWP FAQ (1/1)   Pavel Kurnosoff   30 Aug 2000 17:16:48 
Архивное /ru.cgi.perl/3555e6725455.html, оценка 3 из 5, голосов 10
Яндекс.Метрика
Valid HTML 4.01 Transitional