• perl 定义post接口


    [root@wx03 mojo]# cat a1.pl 
    use Digest::MD5;
    my $md5 = Digest::MD5->new;
    $md5->add(1234567);
    print $md5->hexdigest."
    ";
    [root@wx03 mojo]# perl a1.pl 
    fcea920f7412b5da7be0cf42b8c93759
    
    
    [root@wx03 mojo]# cat a1.pl 
    use Digest::MD5;
    my $md5 = Digest::MD5->new;
    $md5->add(admin);
    print $md5->hexdigest."
    ";
    [root@wx03 mojo]# perl a1.pl 
    21232f297a57a5a743894a0e4a801fc3
    
    
    [root@wx03 mojo]# cat a1.pl 
    use Digest::MD5;
    my $md5 = Digest::MD5->new;
    $md5->add(admin);
    my $pass= $md5->hexdigest;
    print "$pass is $pass
    ";
    [root@wx03 mojo]# perl a1.pl 
    $pass is 21232f297a57a5a743894a0e4a801fc3
    
    
    
    
    mysql> select md5('admin') from user;
    +----------------------------------+
    | md5('admin')                     |
    +----------------------------------+
    | 21232f297a57a5a743894a0e4a801fc3 |
    | 21232f297a57a5a743894a0e4a801fc3 |
    
    
    
    mysql> select md5('1234567') from user;
    +----------------------------------+
    | md5('1234567')                   |
    +----------------------------------+
    | fcea920f7412b5da7be0cf42b8c93759 |
    | fcea920f7412b5da7be0cf42b8c93759 |
    
    
    /*************************************
    [root@wx03 mojo]# cat login.pl 
    use Mojolicious::Lite;
    use JSON qw/encode_json decode_json/;  
    use Encode;
    no strict;
    no warnings;
    use JSON; 
    use POSIX;
    use JSON::RPC::Client;
    use Data::Dumper;
    use Digest::MD5;
    my $SDATE = strftime("%Y-%m-%d",localtime());
    post '/api/login' => sub {
       my $c = shift;
       my $username = $c->param('username');
       my  $paswd = $c->param('passwd');
       my $md5 = Digest::MD5->new;
          $md5->add($passwd);
          $passwd= $md5->hexdigesta;
          print "$passwd is $passwd
    ";
          use DBI;
          my %hash=();
          my $dbUser='DEVOPS';
          my $user="root";
          my $pass='R00t,uHagt.0511';
           my $dbh  = DBI->connect("dbi:mysql:database=$dbUser;host=127.0.0.1;port=3306",$user,$pass) or die "can't connect to database ". DBI-errstr;
          my $hostSql = qq{select count(*)  from user where username='$username' and password='$passwd'};
          my $selStmt = $dbh->prepare($hostSql);
          print "$hostSql is $hostSql
    ";
          $selStmt->execute();
          my $count = $selStmt->fetchrow_array();
          print "$count is $count
    "; 
          if ( $count ==1 )
             { $hash{login}='success';$c->render(json =>  \%hash);}
             else
             { $hash{login}='failed';$c->render(json =>  \%hash);}
    };
      app->start;
             
            
       
    [root@wx03 mojo]# 
    [root@wx03 mojo]# morbo login.pl 
    Server available at http://127.0.0.1:3000
    
    
    
    模拟post 请求:
    
    Pattern 	Methods 	Name
    
    /api/login      POST            apilogin
    
    
    
    
    
    /**********get 登录:
    
    [root@wx03 mojo]# cat login.pl 
    use Mojolicious::Lite;
    use JSON qw/encode_json decode_json/;  
    use Encode;
    no strict;
    no warnings;
    use JSON; 
    use POSIX;
    use JSON::RPC::Client;
    use Data::Dumper;
    use Digest::MD5;
    my $SDATE = strftime("%Y-%m-%d",localtime());
    get '/api/login' => sub {
       my $c = shift;
       my $username = $c->param('username');
       my $passwd = $c->param('passwd');
       my $md5 = Digest::MD5->new;
          $md5->add($passwd);
          $passwd= $md5->hexdigest;
          print "$passwd is $passwd
    ";
          use DBI;
          my %hash=();
          my $dbUser='DEVOPS';
          my $user="root";
          my $pass='R00t,uHagt.0511';
           my $dbh  = DBI->connect("dbi:mysql:database=$dbUser;host=127.0.0.1;port=3306",$user,$pass) or die "can't connect to database ". DBI-errstr;
          my $hostSql = qq{select count(*)  from user where username='$username' and password='$passwd'};
          my $selStmt = $dbh->prepare($hostSql);
          print "$hostSql is $hostSql
    ";
          $selStmt->execute();
          my $count = $selStmt->fetchrow_array();
          print "$count is $count
    "; 
          if ( $count ==1 )
             { $hash{login}='success';$c->render(json =>  \%hash);}
             else
             { $hash{login}='failed';$c->render(json =>  \%hash);}
    };
      app->start;
             
    
    
    http://120.55.118.6:3000/api/login?username=admin&passwd=admin
    {"login":"success"}
    
    http://120.55.118.6:3000/api/login?username=admin&passwd=admin33
    {"login":"failed"}
    
    
    /*******post 登录
    
    [tomcat@wx03 ~]$ perl a3.pl 
    ---------------
    200 OK
    [tomcat@wx03 ~]$ vim a3.pl 
    [tomcat@wx03 ~]$ perl a3.pl 
    ---------------
    200 OK
    HTTP/1.1 200 OK
    Date: Fri, 21 Oct 2016 04:40:38 GMT
    Server: Mojolicious (Perl)
    Content-Length: 19
    Content-Type: application/json;charset=UTF-8
    Client-Date: Fri, 21 Oct 2016 04:40:38 GMT
    Client-Peer: 120.55.118.6:3000
    Client-Response-Num: 1
    
    {"login":"success"}
    [tomcat@wx03 ~]$ cat a3.pl 
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use File::Temp qw/tempfile/;
    use HTTP::Date qw(time2iso str2time time2iso time2isoz);
    my $CurrTime = time2iso(time());
    my $dis_mainpublish='中均资本';
    my $ua = LWP::UserAgent->new;
    $ua->timeout(10);
    $ua->env_proxy;
    my $now          = time();
    $ua->agent('Mozilla/5.0');
    my $cookie_jar = HTTP::Cookies->new(
    
        file           => 'lwp_cookies.txt',
        autosave       => 1,
        ignore_discard => 1
    );
    $ua->cookie_jar($cookie_jar);
    my $str=(rand(1));
    my $url='http://120.55.118.6:3000/api/login';
     my $res = $ua->post($url,{
    	                       'username'=>'admin',
                                   'passwd'=>'admin'
                                                    }
    			);
    	 print "---------------
    ";
    	print $res->status_line."
    ";
            print $res->as_string();
    [tomcat@wx03 ~]$ perl a3.pl 
    ---------------
    200 OK
    HTTP/1.1 200 OK
    Date: Fri, 21 Oct 2016 04:40:43 GMT
    Server: Mojolicious (Perl)
    Content-Length: 19
    Content-Type: application/json;charset=UTF-8
    Client-Date: Fri, 21 Oct 2016 04:40:43 GMT
    Client-Peer: 120.55.118.6:3000
    Client-Response-Num: 1
    
    {"login":"success"}
    
    [tomcat@wx03 ~]$ perl a3.pl 
    ---------------
    200 OK
    HTTP/1.1 200 OK
    Date: Fri, 21 Oct 2016 04:40:43 GMT
    Server: Mojolicious (Perl)
    Content-Length: 19
    Content-Type: application/json;charset=UTF-8
    Client-Date: Fri, 21 Oct 2016 04:40:43 GMT
    Client-Peer: 120.55.118.6:3000
    Client-Response-Num: 1
    
    {"login":"success"}
    [tomcat@wx03 ~]$ vim a3.pl 
    [tomcat@wx03 ~]$ perl a3.pl 
    ---------------
    200 OK
    HTTP/1.1 200 OK
    Date: Fri, 21 Oct 2016 04:41:19 GMT
    Server: Mojolicious (Perl)
    Content-Length: 18
    Content-Type: application/json;charset=UTF-8
    Client-Date: Fri, 21 Oct 2016 04:41:19 GMT
    Client-Peer: 120.55.118.6:3000
    Client-Response-Num: 1
    
    {"login":"failed"}
    
    
    

  • 相关阅读:
    吉哥系列故事――恨7不成妻
    K
    F
    树状数组
    34.在排序数组中查找元素的第一个和最后一个位置--二分查找
    CSS选择器及其权重
    CSS布局 圣杯和双飞翼
    983. 最低票价 -- 动态规划
    合并k个排序链表 二分
    面试题 16.03. 交点
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6199072.html
Copyright © 2020-2023  润新知