Discussion:
How to use proxy with LWP?
(too old to reply)
Siegfried
2006-04-14 21:36:23 UTC
Permalink
I stole the following code fragment from an example program:

use vars '@ISA';
@ISA = 'LWP::UserAgent';
my $agent = __PACKAGE__->new;

Now I want to use a proxy and a search on CPAN to find
http://search.cpan.org/~qjzhou/LWP-UserAgent-ProxyAny-1.02/ProxyAny.pm with
some sample code (see below).

How do I modify the above fragment so I can conditionally use a proxy?

Thanks,
Sieg

use LWP::UserAgent::ProxyAny;

my $ua = LWP::UserAgent::ProxyAny->new;
$ua->env_proxy; # visit url with HTTP_PROXY or Win32 IE proxy settings

my $response = $ua->get('http://sourceforge.net/projects/bookbot');
if ($response->is_success) {
print $response->content; # or whatever
}
else {
die $response->status_line;
}

# Or set proxy by specified name

$ua->set_proxy_by_name("No"); # No Proxy
$ua->set_proxy_by_name("Default"); # $ua->env_proxy
$ua->set_proxy_by_name("127.0.0.1:8080"); # set proxy as
http://127.0.0.1:8080
Alan_C
2006-04-15 06:24:02 UTC
Permalink
Post by Siegfried
@ISA = 'LWP::UserAgent';
my $agent = __PACKAGE__->new;
Now I want to use a proxy and a search on CPAN to find
http://search.cpan.org/~qjzhou/LWP-UserAgent-ProxyAny-1.02/ProxyAny.pm with
some sample code (see below).
How do I modify the above fragment so I can conditionally use a proxy?
Well, $ua versus $agent and LWP::UserAgent versus LWP::UserAgent::ProxyAny
(the 2 differences + use LWP::UserAgent::ProxyAny; 3rd difference)

also, Did you try modify the/your below code, as in # No Proxy ?

I don't really know if that means "not use proxy feature" which is why I
asked.

I've not been around real long but thought I'd take a stab at it. I stand to
be corrected if need be.

#!/usr/bin/perl
use warnings;
use strict;

my $proxy_yes_no = 'no';

print "proxy is off. To use Proxy\nenter a y for yes: ";
chomp(my $stdin2proxy = <STDIN>);

# might use: eq y (instead of the =~ /regex/)
$proxy_yes_no = $stdin2proxy if $stdin2proxy =~ /\by\b/;

# print $proxy_yes_no, "\n";

if ($proxy_yes_no eq 'no') {
print "N \n";
# $ua->set_proxy_by_name("No");
}
else
{
print "Y \n";
# $ua->set_proxy_by_name("127.0.0.1:8080");  # set proxy as
}
# the remainder of proggie
Post by Siegfried
use LWP::UserAgent::ProxyAny;
my $ua = LWP::UserAgent::ProxyAny->new;
$ua->env_proxy; # visit url with HTTP_PROXY or Win32 IE proxy settings
my $response = $ua->get('http://sourceforge.net/projects/bookbot');
if ($response->is_success) {
print $response->content; # or whatever
}
else {
die $response->status_line;
}
# Or set proxy by specified name
$ua->set_proxy_by_name("No"); # No Proxy
$ua->set_proxy_by_name("Default"); # $ua->env_proxy
$ua->set_proxy_by_name("127.0.0.1:8080"); # set proxy as
http://127.0.0.1:8080
--
Alan.
Alan_C
2006-04-15 07:01:26 UTC
Permalink
[ . . ]
Post by Siegfried
How do I modify the above fragment so I can conditionally use a proxy?
http://www.google.com/search?q=perl+lwp+proxy&start=0&ie=utf-8&oe=utf-8&client=firefox-a&rls=org.mozilla:en-US:official

http://tomacorp.com/perl/lwp.html

http://search.cpan.org/~gaas/libwww-perl-5.805/lwpcook.pod#PROXIES

http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP/UserAgent.pm#Proxy_attributes

I forgot until now -- but LWP::UserAgent::ProxyAny I'm guessing is a class
underneath LWP::UserAgent

Thus, (how they are) as to some of the examples as/like at above url(s).

   print "N \n";
#  $ua->no_proxy(qw(no se fi)); # (I guess)
 }
else
 {
  print "Y \n";
# change ftp to http for http (I guess)
#  $ua->proxy(ftp => 'http://proxy.myorg.com');
 }
--
Alan.
Alan_C
2006-04-15 07:19:54 UTC
Permalink
Hi,

<http://learn.perl.org/first-response>

(just now found at bottom) which redirects to:

http://bfr.caseywest.com/

scroll down to heading entitled:

April 14, 2006
How to use proxy with LWP?

which has a dozen cross referenced searches going to lots of very relevant
places.

Please post your solution once you have it working. I'm curious now and
rather limited on time and inch by inch every once in a while I learn.

Thanks.
--
Alan.
Siegfried
2006-04-19 19:32:14 UTC
Permalink
Alan,
This worked for me!

Thanks,
Siegfried



#!c:/Perl/bin/Perl.exe
# Begin commands to execute this file using Perl with bash
# perl test-proxy.pl
# End commands to execute this file using Perl with bash
package test;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(PopulateSummaries); # Symbols to be exported by default
our @EXPORT_OK = qw(); # Symbols to exported by request
our $VERSION = 0.1;


use strict;
use warnings;
use LWP;
use HTTP::Cookies;
use HTML::Parser;
use HTTP::Request::Common;
use URI;
use Date::Manip;
use dbPersist;

use strict;
use LWP::UserAgent;
my $content;

my $ua = new LWP::UserAgent;

# Various enhancement possibilities:
# $ua->max_size(100000); # 100k byte limit
# $ua->timeout(3); # 3 sec timeout is default
$ua->proxy(['http'], 'http://proxy.mycorp.com/'); # set proxy
# $ua->env_proxy() # load proxy info from environment variables
# $ua->no_proxy('localhost', 'mycorp.com'); # No proxy for local machines

$ua->agent("Mozilla/6.0"); # Or something equally mysterious

my $req = new HTTP::Request GET => 'http://www.mycorp.com/';
my $res = $ua->request($req);

if ($res->is_success)
{
$content= $res->content;
print $content;
}
else
{
die "Could not get content";
}

-----Original Message-----
From: Alan_C [mailto:***@gmail.com]
Sent: Saturday, April 15, 2006 1:01 AM
To: ***@perl.org
Subject: Re: How to use proxy with LWP?
[ . . ]
Post by Siegfried
How do I modify the above fragment so I can conditionally use a proxy?
http://www.google.com/search?q=perl+lwp+proxy&start=0&ie=utf-8&oe=utf-8&clie
nt=firefox-a&rls=org.mozilla:en-US:official

http://tomacorp.com/perl/lwp.html

http://search.cpan.org/~gaas/libwww-perl-5.805/lwpcook.pod#PROXIES

http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP/UserAgent.pm#Proxy_at
tributes

I forgot until now -- but LWP::UserAgent::ProxyAny I'm guessing is a class
underneath LWP::UserAgent

Thus, (how they are) as to some of the examples as/like at above url(s).

   print "N \n";
#  $ua->no_proxy(qw(no se fi)); # (I guess)
 }
else
 {
  print "Y \n";
# change ftp to http for http (I guess)
#  $ua->proxy(ftp => 'http://proxy.myorg.com');
 }
--
Alan.
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Loading...