Chinaunix首页 | 论坛 | 博客
  • 博客访问: 593111
  • 博文数量: 158
  • 博客积分: 2696
  • 博客等级: 少校
  • 技术积分: 1668
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-10 00:49
个人简介

life?is?short?,?play?more!

文章分类

全部博文(158)

文章存档

2021年(1)

2013年(10)

2012年(4)

2011年(11)

2010年(27)

2009年(28)

2008年(52)

2007年(25)

我的朋友

分类:

2010-03-16 13:46:47

#########################

###auto test ############

#########################

#use XML::Simple;

#use Data::Dumper;

#use IO::Socket;

use Socket;
#use Test::HTTP


#$autotestxml="d:/autotest.xml";


#if(open(AUTO,$autotestxml))


#print("open autotesting xml ok!\n");

##here parsing the xml files to test cases

#}

#else

#{

#die("open error!\n $!");

#exit(-1);

#}


#close(AUTO);


#@testcases=;


#my $newxml=new XML::Simple;


#$testcases = $newxml->XMLin($autotestxml);


#print Dumper($testcases);


#print $testcase;


################

#intialize the sock

sub open_TCP
{
  # get parameters

  my ($FS, $dest, $port) = @_;
 
  my $proto = getprotobyname('tcp');
  socket($FS, PF_INET, SOCK_STREAM, $proto);
  my $sin = sockaddr_in($port,inet_aton($dest));
  connect($FS,$sin) || return undef;
  
  my $old_fh = select($FS);
  $| = 1;          # don't buffer output

  select($old_fh);
  return 1;
}



sub get_url
{
my ($url) = @_;
if ($url =~ /^http:\/\/(.+?):([0-9]+)\//) {

    $web_server = $1;
    $port = $2;
}

elsif ($url =~ /^http:\/\/(.+?)\// ) {
  $web_server = $1;
  $port = 80;
}

else {
print "plz give the correct url\n";
exit(-1);
}

#print $web_server.":".$port."\n\n";


if (open_TCP(F, $web_server, $port) == undef) {
  print "Error connecting to server at $!\n";
  exit(-2);
}
# http1.1 must send the host header , if not will server return 400
print F "GET ".$url." HTTP/1.1\r\n";
print F "HOST: $web_server\r\n";
print F "Connection: Close\r\n\r\n";

# connetion: close will  close the session imediertly after returns.
#print F "Accept: */*\r\n\r\n";
#print F "Accept-Language: zh-cn\r\n\r\n";
#print F "Accept-Encoding: gzip,deflate\r\n";
#print F "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n\r\n";
#print F "Proxy-Connection: Keep-Alive\r\n\r\n";
#print F "Accept-Language: zh-cn,zh;q=0.5\r\n";
#print F "Accept-Encoding: gzip,deflate\r\n";

my @return_line=<F>;

#print @return_line;

close(F);
my @headers,@contents;
my $lines = 0;
my $found_gap=0;

        while ($lines <= @return_line) {
        #print $lines;
                if ($found_gap == 0) {

                push (@headers,@return_line[$lines]);
                }

                if($found_gap ==1){

                push (@contents,@return_line[$lines]);
                }

                if (@return_line[$lines] =~ /^\r$/ && !$found_gap) {

                #print "found the gap bettween the headers and content.\n";

                $found_gap=1;

                }

        $lines ++;

        }


print @headers;

print "##########\n";

print @contents;
return @contents;
}

####################post data#######################

#########return the body############################

####################################################

sub post_url

{

my ($url,$post_data,$urlencode_need) = @_;

if ($url =~ /^http:\/\/(.+?):([0-9]+)\//) {

    $web_server = $1;
    $port = $2;
    
}

elsif ($url =~ /^http:\/\/(.+?)\// ) {

  $web_server = $1;
  $port = 80;

}

else {

print "plz give the correct url\n";

exit(-1);

}


$length = length($post_data);

if ($urlencode_need) {

   #$post_data =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;

  $length = "57";
     #length($post_data);


   print "urlencoded data: $post_data\n";

}

#print $web_server.":".$port."\n\n";


if (open_TCP(F, $web_server, $port) == undef) {
  print "Error connecting to server at $!\n";
  exit(-2);
}

print F "POST ".$url." HTTP/1.1\r\n";
#print F "Accept: */*\r\n";
#print "Accept: */*\r\n";
#print F "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.18) Gecko/2010020220 Firefox/3.0.18\r\n";
print F "Content-Type: application/x-www-form-urlencoded\r\n";
print F "Content-Length: ".$length."\r\n";
#print "Content-Length: ".$length."\r\n";
print F "Host: $web_server\r\n";
#print F "Keep-Alive: 100\r\n";
#print F "Pragma: no-cache\r\n";
print F "Connection: Close\r\n\r\n";
#print "POST data: $post_data\r\n";

print F "$post_data\r\n";

my @return_line=<F>;
my @contents;
my @headers;
#print @return_line;


close(F);

my $lines = 0;
my $found_gap=0;

        while ($lines <= @return_line) {
        #print $lines;
                if ($found_gap == 0) {
                push (@headers,@return_line[$lines]);
                }

                if($found_gap ==1){

                push (@contents,@return_line[$lines]);
                }

                if (@return_line[$lines] =~ /^\r$/ && !$found_gap) {

                #print "found the gap bettween the headers and content.\n";

                $found_gap=1;
                }
        $lines ++;

        }

print "\n###############################################################\n";
print "Post Url: $url\n";
print "Post Data: $post_data\n";
print "#######################return headers##########################\n";
print @headers;
#print "#####body#####\n";

#print @contents;

return @contents;
}


阅读(1006) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~