我正在尝试访问受保护的文件.服务器正在使用摘要式身份验证 - 我可以从打印出的响应中看到.以下是示例代码:
use LWP;
use strict;
my $url = 'http://somesite.com/aa/bb/cc.html';
my $username = 'scott';
my $password = 'tiger';
my $browser = LWP::UserAgent->new('Mozilla');
$browser->credentials("http://somesite.com:80","realm-name",$username=>$password);
my $response=$browser->get($url);
print $response->content;
Run Code Online (Sandbox Code Playgroud)
当我尝试从浏览器访问该资源时,我从弹出窗口获取它的领域的名称.相同的用户名和密码在浏览器中工作得非常好,我能够看到内容,但是当我运行上面的脚本时,它总是说401 Authorization required.
LWP如何运作?
我是否需要要求LWP发送用户名和密码的MD5哈希(摘要),或者它是否在内部检查要使用的身份验证并发送相应的(基本/摘要)发送凭据的方式.我的问题是
任何快速帮助都非常感谢!
我试着运行这个perl5程序:
#!/usr/bin/env perl
use strict;
use warnings;
use LWP;
my $ua = LWP::UserAgent->new('Mozilla');
$ua->credentials("test.server.com:39272", "realm-name", 'user_name', 'some_pass');
my $res = $ua->get('http://test.server.com:39272/');
print $res->content;
Run Code Online (Sandbox Code Playgroud)
另一方面,我有HTTP ::守护进程:
#!/usr/bin/env perl
use strict;
use warnings;
use HTTP::Daemon;
my $hd = HTTP::Daemon->new or die;
print "Contact URL: ", $hd->url, "\n";
while (my $hc = $hd->accept) {
while (my $hr = $hc->get_request) {
if ($hr->method eq 'GET') {
print $hr->as_string, "\n";
}
}
$hc->close;
undef($hc);
}
Run Code Online (Sandbox Code Playgroud)
它只是打印:
Contact URL: http://test.server.com:39272/
GET / HTTP/1.1
Connection: TE, close …Run Code Online (Sandbox Code Playgroud) 我正在使用LWP :: UserAgent和:content_file选项将大文件直接下载到Perl文件中.
这是我的代码的简化示例:
require LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(3600);
$ua->env_proxy;
my $response = $ua->get(
'http://example.com/largefile.xml',
:content_file => 'path/to/file/largefile.xml'
);
if ($response->is_success) {
print "File downloaded\n";
}
else {
die $response->status_line;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法显示下载状态的百分比?(或类似wget输出的东西)
10% [===> ] 65.120.154 527K/s
Run Code Online (Sandbox Code Playgroud) 我有一个Perl脚本,它使用LWP :: UserAgent下载一个网页,然后使用正则表达式进行处理.问题是网页的常规HTML部分没有返回给LWP :: UserAgent,因为该网站认识到浏览器没有安装Flash,而是返回HTML,提示我们下载Flash而不是相应的HTML我们需要解析.
如何使LWP :: UserAgent似乎将闪存安装到我们要求该页面的Web服务器上?我正在使用以下代码初始化LWP :: UserAgent:
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(cookie_jar => { },requests_redirectable => [ ]);
$ua->agent('Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:9.9.9.9) Gecko/20079999 Firefox/2.0.0.1');
$ua->timeout(10);
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助!
我有一个简单的网址,它有302临时性.转到另一页.
如果URL返回代码200(对于OK)以检索它并且如果返回除200之外的其他内容,则尝试进入.
我的代码:
my $ua = LWP::UserAgent->new( env_proxy => 1,keep_alive => 1, timeout => 30, agent => "Mozilla/4.76 [en] (Win98; U)");
my $response = $ua->get( $currenturl);
print $response->code;
Run Code Online (Sandbox Code Playgroud)
ALWAYS上面的代码返回200,即使它是302.我在Firefox中使用FireBug测试了头部响应.该URL在FireBug的Net模块中返回"302 Moved Temporarily".但是perl上面的代码返回200.为什么?
我正在将应用程序从PHP/cURL移植到Perl和LWP :: UserAgent.我需要向Web服务器发出POST请求并提供客户端证书和密钥文件.我试图复制的PHP代码是这样的:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLCERT, "/path/to/certificate.pem");
curl_setopt($ch, CURLOPT_SSLKEY, "/path/to/private.key");
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, "secretpassword");
Run Code Online (Sandbox Code Playgroud)
这是我的Perl代码:
my $ua = LWP::UserAgent->new();
$ua->ssl_opts(
SSL_verify_mode => 0,
SSL_cert_file => '/path/to/certificate.pem',
SSL_key_file => "/path/to/private.key",
SSL_passwd_cb => sub { return "secretpassword"; }
);
Run Code Online (Sandbox Code Playgroud)
PHP代码成功连接到服务器,但Perl代码失败:
SSL读取错误错误:14094410:SSL例程:SSL3_READ_BYTES:sslv3警报握手失败
我无法弄清楚我错过了什么.
以下脚本可以在运行libwww-perl-5.836但不在主机上的主机上返回综合标头libwww-perl-6.30.0.在这种情况下,脚本显示以下内容:
500 Can't connect to backend.mutegroup.org:443 (certificate verify failed)
Content-Type: text/plain
Client-Date: Mon, 28 Jul 2014 21:09:28 GMT
Client-Warning: Internal response
Can't connect to backend.mutegroup.org:443 (certificate verify failed)
LWP::Protocol::https::Socket: SSL connect attempt failed with unknown error error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed at /usr/lib64/perl5/vendor_perl/5.16.3/LWP/Protocol/http.pm line 51.
Run Code Online (Sandbox Code Playgroud)
这是脚本:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0, } );
my $url = 'https://backend.mutegroup.org/api/getLastId';
my $request = POST $url;
print $ua->request($request)->as_string …Run Code Online (Sandbox Code Playgroud) 我正在使用LWP从网页下载内容,我想限制等待页面的时间.这是在lwp中完成的,如下所示:
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->get($url);
Run Code Online (Sandbox Code Playgroud)
这样可以正常工作,除非超时达到极限,它就会死掉,我无法继续使用脚本!我真的很想妥善处理这个超时,这样我就可以记录url超时然后转到下一个.有谁知道如何做到这一点?谢谢!
如果我用来wget从geonames.org服务器检索某些内容,它会报告两个IP地址,第一个会失败但是从第二个获取它:
Resolving ws.geonames.org (ws.geonames.org)... 5.9.41.208, 176.9.107.169
Connecting to ws.geonames.org (ws.geonames.org)|5.9.41.208|:80... failed: Connection refused.
Connecting to ws.geonames.org (ws.geonames.org)|176.9.107.169|:80... connected.
HTTP request sent, awaiting response... 200 OK
Run Code Online (Sandbox Code Playgroud)
但不幸的是,我必须使用LWP :: UserAgent和HTTP :: Request通过perl访问它.如果第一个IP失败,如何让他们尝试第二个IP?
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(
GET =>
"http://ws.geonames.org/countrySubdivision?lat=$lat&lng=$long&radius=$radius&username=xyzzy");
my $res = $ua->request($req);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Perl LWP通过Way2sms发送短信.登录部分成功,之后我将cookie保存到本地文件.登录后的欢迎页面显示发送短信链接,点击哪一个被重定向到另一个页面,其中有两个用于移动号码和短信文本的输入以及一个用于提交和发送短信的按钮.Firebug显示页面结构,如图所示.从Iframe网址和表单的action属性,我构造了表单操作的绝对URL并相应地提交表单,并将cookie存储在文件中.但是,短信不会被发送.我在这做错了什么?代码如下.(name两个文本输入的属性是正确的,通过观察Firebug中的源代码来获取,尽管图像中未包含该代码)
use LWP::UserAgent;
open f, "> way2sms.txt";
use HTTP::Cookies;
my $cookie_jar = HTTP::Cookies->new(
file => "cookies.txt",
autosave => 1,
);
my $ua = LWP::UserAgent->new(
agent =>
'Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
cookie_jar => $cookie_jar,
);
my $response = $ua->post(
'http://site2.way2sms.com/contentt/bar/Login1.action',
{
username => $user,
password => $pass,
}
);
if ( $response->is_redirect ) {
$response = $ua->get( $response->header('Location') );
print 5 if $response->decoded_content =~ /Kaustav Mukherjee/i; #prints it, showing that the …Run Code Online (Sandbox Code Playgroud)