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
相关文章推荐
- 关于树的总结从二叉树->二叉搜索树->平衡二叉树->红黑树->B树与B+树
- 数字电子技术碎片
- chunkAnalyse:C++下读取指定文件下的指定文件或文件
- 实时股票数据接口
- Genymotion Unable to load VirtualBox engine Genymotion启动失败解决办法
- 排列
- iOS 开发怎么入门?
- 区间不同数个数
- iOS中的copy ,kvc,kvo
- Java操作Oracle数据库
- 为什么对基础设施的监控变得如此重要?
- 设计模式---组合模式
- IOS开发常见面试题
- mbed TLS
- web缓存详解
- 记 Ubuntu14.04 Monodevelop 安装的两个问题
- SpringMVC学习笔记——Spring MVC与Struts2的区别2
- jQuery源码之ready()事件
- ionic cordova plugin for ios
- 实现mysql按月统计的教程