perl use HTTP::Server::Simple 轻量级 http server

cpan -i  HTTP::Server::Simple

返回:已是 up to date. 但是我在 D:\Strawberry\perl\site\lib\ 找不到 HTTP\Server

手工安装:下载 HTTP-Server-Simple-0.52.tar.gz

解压 tar zxvf HTTP-Server-Simple-0.52.tar.gz 

cd D:\perl\HTTP-Server-Simple-0.52

perl Makefile.PL

gmake install

D:\perl\HTTP-Server-Simple-0.52>gmake install
cp lib/HTTP/Server/Simple/CGI/Environment.pm blib\lib\HTTP\Server\Simple\CGI\Environment.pm
cp lib/HTTP/Server/Simple.pm blib\lib\HTTP\Server\Simple.pm
cp lib/HTTP/Server/Simple/CGI.pm blib\lib\HTTP\Server\Simple\CGI.pm
Installing D:\Strawberry\perl\site\lib\HTTP\Server\Simple.pm
Installing D:\Strawberry\perl\site\lib\HTTP\Server\Simple\CGI.pm
Installing D:\Strawberry\perl\site\lib\HTTP\Server\Simple\CGI\Environment.pm
Appending installation info to D:\Strawberry\perl\lib/perllocal.pod

官网样例:HTTP::Server::Simple - Lightweight HTTP server - metacpan.org

#!/usr/bin/perl
{
package MyWebServer;
  
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
  
my %dispatch = (
    '/hello' => \&resp_hello,
    # ...
);
  
sub handle_request {
    my $self = shift;
    my $cgi  = shift;
    
    my $path = $cgi->path_info();
    my $handler = $dispatch{$path};
  
    if (ref($handler) eq "CODE") {
        print "HTTP/1.0 200 OK\r\n";
        $handler->($cgi);
          
    } else {
        print "HTTP/1.0 404 Not found\r\n";
        print $cgi->header,
              $cgi->start_html('Not found'),
              $cgi->h1('Not found'),
              $cgi->end_html;
    }
}
  
sub resp_hello {
    my $cgi  = shift;   # CGI.pm object
    return if !ref $cgi;
      
    my $who = $cgi->param('name');
      
    print $cgi->header,
          $cgi->start_html("Hello"),
          $cgi->h1("Hello $who!"),
          $cgi->end_html;
}
  
} 
  
# start the server on port 8080
my $pid = MyWebServer->new(8080)->background();
print "Use 'kill $pid' to stop server.\n";

运行 perl http_server.pl

浏览器访问 http://localhost:8080/hello?name=Alien
鼠标右键,查看网页源代码。

最后我找到了 cpan 安装所在位置 D:\Strawberry\perl\vendor\lib\HTTP\Server\

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值