• 【perl】simpleHTTP


    类似Python SimpleHTTPServer

    #!/usr/bin/perl
    # https://metacpan.org/pod/HTTP::Server::Simple
    # https://metacpan.org/pod/distribution/CGI/lib/CGI.pod
    package WebServer;
    
    use strict;
    use warnings FATAL => 'all';
    use HTTP::Server::Simple::CGI;
    use base qw(HTTP::Server::Simple::CGI);
    use JSON;
    
    sub handle_request {
      my $self = shift;
      my $cgi  = shift;
      my $handler = &resp_info;
    
      print "HTTP/1.0 200 OK
    ";
      print $cgi->header(
        -type    => 'application/json',
        -status  => '200',
      );
      $handler->($cgi);
    }
    
    sub resp_info {
      my $cgi  = shift;   # CGI.pm object
      return if !ref $cgi;
    
      # my $who = $cgi->param('name');
      my %rec_hash = (
        'path' => $cgi->path_info(),
        'method' => $cgi->request_method()
      );
      my $json = encode_json %rec_hash;
    
      print $cgi->param(
        -name  => 'data',
        -value => $json,
      );
    };
    
    # start the server on port 8080
    my $pid = WebServer->new(8080)->run();
    # my $pid = WebServer->new(8080)->background();
    # print "Use 'kill $pid' to stop server.
    ";
    
    
  • 相关阅读:
    Codeforces-859C Pie Rules(dp)
    Codeforces-550D Regular Bridge
    Codeforces-534D Handshakes
    抽象类
    内部类
    接口
    初始化
    this 和super
    数据库测试的测试点
    数据库测试的主要内容
  • 原文地址:https://www.cnblogs.com/jiangxu67/p/10755247.html
Copyright © 2020-2023  润新知