您的位置:首页 > 其它

Perl LWP::Simple 提供的方法

2015-07-10 23:01 337 查看
<pre name="code" class="sql"><pre name="code" class="sql"> get($url)
The get() function will fetch the document identified by the given URL and return it.  It returns "undef" if it fails.  The $url argument can be either a simple string

or a reference to a URI object.

You will not be able to examine the response code or response headers (like ’Content-Type’) when you are accessing the web using this function.  If you need that

information you should use the full OO
interface (see LWP::UserAgent).

get() 函数会获取给定的URL 的内容, 它返回"undef"如果失败的话, $url 参数可以使一个简单的字符串 或者是一个URL 对象的引用

你不能校验 响应码 或者响应header(like ’Content-Type’),当你接收web使用这个函数的时候,如果你需要这些信息 你需要使用( LWP::UserAgent).

[root@dr-mysql01 test]# cat e1.pl
use LWP::Simple;
$a=get('http://zjcap.cn');
print "\$a is $a\n";

head($url)

Get document headers. Returns the following 5 values if successful: ($content_type, $document_length, $modified_time, $expires, $server)

Returns an empty list if it fails.  In scalar context returns TRUE if successful.

得到document 的headers. ,返回 5个值如果称为的话  ($content_type, $document_length, $modified_time, $expires, $server)

取网页返回header 响应header,响应头信息

如果失败返回一个空的列表
[root@dr-mysql01 test]# cat e2.pl
use LWP::Simple;
@content = head("http://www.zjcap.cn/");
die "Couldn't get it!" unless defined @content;
print "\@content is @content\n";
[root@dr-mysql01 test]# perl e2.pl
@content is text/html 27140 1436499350  nginx/1.7.7

getprint($url)

Get and print a document identified by a URL. The document is printed to the selected default filehandle for output (normally STDOUT) as data is received from the network.  If

the request fails, then the
status code and message are printed on STDERR.  The return value is the HTTP response code.

get和打印 URL 的document, dodument输出到标准输出, 如果请求失败, 会返回状态code到错误输出 返回值是 响应码

</body>
</html>@content is 200
[root@dr-mysql01 test]# cat e3.pl
use LWP::Simple;
@content = getprint("http://www.zjcap.cn/");
die "Couldn't get it!" unless defined @content;
print "\@content is @content\n";

[root@dr-mysql01 test]# perl e3.pl
500 Can't connect to www.zjcap1.cn:80 (connect: Connection timed out) <URL:http://www.zjcap1.cn/>
@content is 500
You have mail in /var/spool/mail/root

返回状态码:

[root@master test]# cat 1.pl
use LWP::Simple;
my $code = getstore('http://zjcap.cn','a.txt');
print "\$code is $code\n";

[root@master test]# perl 1.pl
$code is 200

根据返回码判断:

[root@master test]# cat 1.pl
use LWP::Simple;
my $code = getstore('http://zjcap.cn','a.txt');
print "\$code is $code\n";
if (is_success($code)){print "can open\n"}
else{print "error\n"};
[root@master test]# perl 1.pl
$code is 200
can open
[root@master test]# vi 1.pl
[root@master test]# cat 1.pl
use LWP::Simple;
my $code = getstore('http://zjcap.cn1','a.txt');
print "\$code is $code\n";
if (is_success($code)){print "can open\n"}
else{print "error\n"};
[root@master test]# perl 1.pl
$code is 500
error




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: