• perl LWP::UserAgent 设置超时


    def arrays(req):
        print req.get_full_path()
        a=[[12,23,34],[44234,4242,5353],['a','bbb','ccc']]
        print a
        return HttpResponse(json.dumps(a), content_type="application/json")
    
    
    url(r'^api/arrays/$', newview.arrays),
    
    
    [root@yyjk sbin]# curl http://1.1.1.1:9000/api/arrays/
    [[12, 23, 34], [44234, 4242, 5353], ["a", "bbb", "ccc"]][root@yyjk sbin]# 
    
    
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use JSON;
    use File::Temp qw/tempfile/;
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use JSON;
    use File::Temp qw/tempfile/;
    use HTML::TreeBuilder::XPath;
    use Encode;
    use HTML::TreeBuilder;
    use Data::Dumper;
    use HTML::TreeBuilder::XPath;
    use DBI;
    use Encode;
    my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, );;
    $ua->timeout(30);
    $ua->env_proxy;
    my $now          = time();
    $ua->agent("Mozilla/8.0");
    my $cookie_jar = HTTP::Cookies->new(
    
        file           => 'lwp_cookies.txt',
        autosave       => 1,
        ignore_discard => 1
    );
    $ua->cookie_jar($cookie_jar);
    my $response = $ua->get('http://1.1.1.1:9000/api/arrays/');
    
    if ($response->is_success) {
        $r = $response->content;
        print $r."
    ";
       }
     else {
        die $response->status_line;
    };
    
    
    1. 测试1,后台模拟超时
    
    def arrays(req):
        print req.get_full_path()
        a=[[12,23,34],[44234,4242,5353],['a','bbb','ccc']]
        print a
        time.sleep(31)
        return HttpResponse(json.dumps(a), content_type="application/json")
    
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use JSON;
    use File::Temp qw/tempfile/;
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use JSON;
    use File::Temp qw/tempfile/;
    use HTML::TreeBuilder::XPath;
    use Encode;
    use HTML::TreeBuilder;
    use Data::Dumper;
    use HTML::TreeBuilder::XPath;
    use DBI;
    use Encode;
    my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, );;
    $ua->timeout(30);
    $ua->env_proxy;
    my $now          = time();
    $ua->agent("Mozilla/8.0");
    my $cookie_jar = HTTP::Cookies->new(
    
        file           => 'lwp_cookies.txt',
        autosave       => 1,
        ignore_discard => 1
    );
    $ua->cookie_jar($cookie_jar);
    my $response = $ua->get('http://1.1.1.1:9000/api/arrays/');
    
    if ($response->is_success) {
        $r = $response->content;
        print $r."
    ";
       }
     else {
        die $response->status_line;
    };
    
    
    
    [root@yyjk sbin]# time perl testtimeout.pl 
    500 read timeout at testtimeout.pl line 41.
    
    real	0m30.277s
    user	0m0.212s
    sys	0m0.030s
    [root@yyjk sbin]# 
    
    
    后台看到了 错误的管道,因为前台超时断开了
    /api/arrays/
    [[12, 23, 34], [44234, 4242, 5353], [u'a', u'bbb', u'ccc']]
    [2018-10-13 06:12:35,987] - Broken pipe from ('1.1.1.1', 35487)
    
    
    测试2 如果是地址不通
    [root@yyjk sbin]# 
    [root@yyjk sbin]# cat testtimeout.pl 
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use JSON;
    use File::Temp qw/tempfile/;
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use JSON;
    use File::Temp qw/tempfile/;
    use HTML::TreeBuilder::XPath;
    use Encode;
    use HTML::TreeBuilder;
    use Data::Dumper;
    use HTML::TreeBuilder::XPath;
    use DBI;
    use Encode;
    my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, );;
    $ua->timeout(30);
    $ua->env_proxy;
    my $now          = time();
    $ua->agent("Mozilla/8.0");
    my $cookie_jar = HTTP::Cookies->new(
    
        file           => 'lwp_cookies.txt',
        autosave       => 1,
        ignore_discard => 1
    );
    $ua->cookie_jar($cookie_jar);
    my $response = $ua->get('http://10.5.100.99:5000/api/arrays/');
    
    if ($response->is_success) {
        $r = $response->content;
        print $r."
    ";
       }
     else {
        die $response->status_line;
    };
    [root@yyjk sbin]# user0m0.212s^C
    [root@yyjk sbin]# set -o vi
    [root@yyjk sbin]# time perl testtimeout.pl 
    500 Can't connect to 10.5.100.99:5000 at testtimeout.pl line 41.
    
    real	0m3.208s
    user	0m0.181s
    sys	0m0.020s
    [root@yyjk sbin]# 
    
    那么不能报无法连接
    
    $ua->timeout
        $ua->timeout( $secs )
            Get/set the timeout value in seconds. The default timeout() value is
            180 seconds, i.e. 3 minutes.
    
            The requests is aborted if no activity on the connection to the server
            is observed for "timeout" seconds. This means that the time it takes
            for the complete transaction and the request() method to actually
            return might be longer.
    		
    		设置 超时时间单位毫秒 默认是180秒 
    		
    请求时终止了如果没有活动连接到服务器是被观察  
    
    这以为这他花费完成实物和rquest()方法实际返回时间更长	
  • 相关阅读:
    java ssl https 连接详解 生成证书
    mysql实战优化之四:mysql索引优化
    mysql实战优化之五: 更新/插入优化 sql优化
    mysql实战优化之六:Order by优化 sql优化、索引优化
    mina2中IoHandler
    I/O通信模型(BIO,NIO,AIO)
    mina框架详解
    转:架构师于小波:魅族实时消息推送架构
    支付业务的数据库表的设计
    实战 TestNG 监听器
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349029.html
Copyright © 2020-2023  润新知