#!/usr/bin/perl#use strict;#use warnings;use LWP::UserAgent;use http::cookies;use Encode;use JsON;use utf8;binmode(STDIN, ':enCoding(utf8)');binmode(STDOUT, ':enCoding(utf8)');binmode(STDERR, ':enCoding(utf8)');my $new_task_url = 'http://www.alibench.com/new_task.PHP'; #创建探测人物的URLmy $get_task_re_url = 'http://www.alibench.com/query_task.PHP'; # 获取探测结果URLsub save_cookie { ####保存cookie 并获取form name(下边创建任务post时会用到) my $UA = LWP::UserAgent->new; my $cookie_jar = http::cookies->new( file => "./ali_cookies.dat", autosave => 1,ignore_discard => 1, ); $UA->cookie_jar($cookie_jar); ##保存cookie my $response = $UA->get('http://www.alibench.com/'); my ($traceroute_from,$traceroute_from_name); my $str = $response->content; foreach my $line (split(/input/,$str)){ if ($line =~ m!.*name="traceroute_from" value="([^"]*)" ID.*!) { $traceroute_from = ; #获取form } if ($line =~ m!.*name="traceroute_from_name" value="([^"]*)" ID.*!) { $traceroute_from_name = ; #获取form name } } return ($traceroute_from,$traceroute_from_name);}sub create_task { my ($traceroute_from,$traceroute_from_name) = @_; my $UA = LWP::UserAgent->new; $UA->agent('Mozilla/5.0 (windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 firefox/35.0'); $UA->cookie_jar({'file' => './ali_cookies.dat'}); $UA->default_header( #定义headers 'Host' => 'www.alibench.com','User-Agent' => 'Mozilla/5.0 (windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 firefox/35.0','Accept' => 'application/Json, text/JavaScript, */*; q=0.01','Accept-Language' => 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3','Accept-EnCoding' => 'gzip, deflate','Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8','X-Requested-With' => 'XMLhttpRequest','Referer' => 'http://www.alibench.com/','Connection' => 'keep-alive','Pragma' => 'no-cache','Cache-Contro' => 'no-cache',); my $response = $UA->post("$new_task_url",['task_from' => 'self', ##post数据 'traceroute_from' => "$traceroute_from",'traceroute_from_name' => "$traceroute_from_name",'target' => 'www.test.com', ##定义需要探测的URL 'target2' => '', 'is_pk' => 'false', ##对比开关 'ac' => 'http', ##测试类型为http 'http_assign_time' => '20', ##任务下发时间 'isps[]' => '1', ##需要探测的服务商 'isps[]' => '4','http_method' => 'get','http_gzip' => 'true','http_follow_302' => 'true','http_cookie' => '','http_ua' => '','http_host' => '','http_referer' => '','http_limit_rate' => '', ]); return $response->content; ##包含任务ID 以及创建任务的返回码 ,0为成功}sub get_task_re { ##获取探测结果 my ($task_ID) = @_; my $UA = LWP::UserAgent->new; $UA->agent('Mozilla/5.0 (windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 firefox/35.0'); $UA->cookie_jar({'file' => './ali_cookies.dat'}); $UA->default_header( 'Host' => 'www.alibench.com','Referer' => "http://www.alibench.com/rp/$task_ID",); my $response = $UA->post("$get_task_re_url",[ 'task_IDs' => "$task_ID", 'task_type'=> '1',]); return $response->content;}sub handle_Json { ###Json处理 my $Json_data = $_[0]; my $Json_obj; my $Json = JsON->new->utf8; $Json_obj = $Json->decode("$Json_data"); my ($data_array,$tmp,$complete_status); my @data_item = qw!http_total_time http_dns_time curl_connect_time http_download_speed clIEnt_ip node_name!; ###定义需要获取的数据类型,以下是可以得到的信息,在数组data_item添加即可# {# "node_ID": "10658334",# "locate_ID": 34147844,# "task_type": "1",# "target_ip": "123.134.186.225",# "http_response_code": "200",# "http_total_time": 296000,# "http_dns_time": 15000,# "curl_connect_time": 62000,# "curl_pretransfer_time": 62000,# "curl_starttransfer_time": 140000,# "http_download_size": "36590",# "http_download_speed": "123614",# "http_response_head": "http/1.1 200 OK\r\nContent-Type: text/HTML\r\nLast-ModifIEd: Wed, 04 Feb 2015 08:55:16 GMT\r\nvary: Accept-EnCoding\r\nExpires: Wed, 04 Feb 2015 09:08:23 GMT\r\nCache-Control: max-age=300\r\ncontent-encoding: gzip\r\nContent-Length: 36590\r\nAccept-Ranges: bytes\r\nDate: Wed, 04 Feb 2015 09:05:33 \r\nServer: wxcdn/1.1.1\r\n\r\n",# "ldns_ip": "58.242.96.242",# "http_download_time": 234000,# "http_connect_time": 47000,# "status": 2,# "clIEnt_ip": "58.243.174.211",# "node_name": "安徽 阜阳 联通",# "node_country": "中国",# "node_province": "安徽",# "node_city": "阜阳",# "isp": "联通",# "target_location": "山东省莱芜市联通"# } for my $item (@{$Json_obj->{'data'}}){ $data_array = $item->{'data'}; $complete_status = $item->{'complete'}; #true:1 false:0 } foreach my $one_zone_info (@{$data_array}) { if (defined($one_zone_info->{'http_total_time'})) { foreach my $da_it (@data_item) { $tmp .= $one_zone_info->{"$da_it"} . "_"; } push (@result,$tmp); #将探测完成的数据push到数组 $tmp = ''; }else{ print "$one_zone_info->{'node_name'} this zone is not ok \n"; next; } } return($complete_status,@result);}my ($traceroute_from,$traceroute_from_name) = save_cookie;my $task_info = create_task($traceroute_from,$traceroute_from_name);my $task_ID;if ($task_info =~ m!\{"code":0,"env":"online","data":"\/rp\/(.*)"\}!) { $task_ID = ;}else{ print "Create task Failed \n $task_info"; exit;}sleep 20;print "get \n";my ($complete_status,@result);($complete_status,@result) = handle_Json(get_task_re($task_ID));while ($complete_status == 0) { sleep 2; print "get result,waiting \n"; ($complete_status,@result) = handle_Json(get_task_re($task_ID));}foreach (@result) { print "$_ \n"; ##输出全部的数据}
下图是获取的部分结果。 注意下执行频率,过快的话 可能会导致测试的域名被拉黑名单。
总结以上是内存溢出为你收集整理的alibench交互 获取测试链接全国打开情况全部内容,希望文章能够帮你解决alibench交互 获取测试链接全国打开情况所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)