zoukankan      html  css  js  c++  java
  • Perl LWP::Simple 提供的方法

    <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
    ";
    
    
    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
    ";
    [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
    ";
    
    
    [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
    ";
    
    [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
    ";
    if (is_success($code)){print "can open
    "}
    else{print "error
    "};
    [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
    ";
    if (is_success($code)){print "can open
    "}
    else{print "error
    "};
    [root@master test]# perl 1.pl 
    $code is 500
    error
    


    
    
    
                                        
    
  • 相关阅读:
    怎么接音响
    怎样区分音箱与音响
    什么是卡盟
    小白晋级达人必备 电视接口使用介绍(4)
    液晶电视插有线电视信号线的是哪个接口 HDMI是什么接口
    Google 镜像站搜集
    屏幕检测
    网站引流
    夜神安卓模拟器
    html5模拟平抛运动
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351537.html
Copyright © 2011-2022 走看看