What is this Perl code using LWP::UserAgent doing?
I have this code:
use strict;
use LWP::UserAgent;
use warnings;
my $ua = new LWP::UserAgent(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5');
$ua->proxy([qw(http https)] => 'http://59.39.92.148:1080');
my $response = $ua->get("http://www.google.com");
print $response->c开发者_开发百科ode,' ', $response->message,"\n";
Is the meaning of the code "open www.google.com with sock proxy"? What is the explanation?
It's instantiating an LWP::UserAgent object.
The parameters specify the browser Mozilla Firefox
The address and port to connect to the proxy 'http://59.39.92.148:1080'
and the response is Google passed through the proxy:
my $response = $ua->get("http://www.google.com");`
Finally:
print $response->code,' ', $response->message,"\n";`
allows a response to be posed back to the user
I'm a C# developer, but that's how it looks to me :)
The code is setting up an LWP::UserAgent object to masquerade as a browser used by a human being to bypass Google's spider detection mechanism. In doing so, it violates Google's Terms of Service:
5.3 You agree not to access (or attempt to access) any of the Services by any means other than through the interface that is provided by Google, unless you have been specifically allowed to do so in a separate agreement with Google. You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers) and shall ensure that you comply with the instructions set out in any robots.txt file present on the Services.
59.39.92.148
is probably some compromised (or badly configured) open proxy in China. Setting $ua
to use it is an attempt to hide the origin of the TOS violation.
Incidentally, you should be aware that the server at 59.39.92.148
will be able to log and track all your requests and responses should you decide to go down this route.
The more important question is: What are you trying to do?
Looks like: Open "www.google.com" using an HTTP Proxy
精彩评论