当前位置: 首页 > news >正文

使用 HTTP::Server::Simple 实现轻量级 HTTP 服务器

在Perl中,HTTP::Server::Simple 模块提供了一种轻量级的方式来实现HTTP服务器。该模块简单易用,适合快速开发和测试HTTP服务。本文将详细介绍如何使用 HTTP::Server::Simple 模块创建和配置一个轻量级HTTP服务器。

安装 HTTP::Server::Simple

首先,需要确保安装了 HTTP::Server::Simple 模块。如果尚未安装,可以使用以下命令通过CPAN进行安装:

cpan HTTP::Server::Simple
​

或者,如果你使用的是 cpanm,可以通过以下命令安装:

cpanm HTTP::Server::Simple
​

创建简单的 HTTP 服务器

以下示例展示了如何创建一个最简单的HTTP服务器,该服务器在本地端口8080上运行,并返回一个简单的“Hello, World!”消息。

use strict;
use warnings;
use HTTP::Server::Simple::CGI;# 创建一个简单的服务器类,继承自HTTP::Server::Simple::CGI
{package MyWebServer;use base qw(HTTP::Server::Simple::CGI);sub handle_request {my ($self, $cgi) = @_;print "HTTP/1.0 200 OK\r\n";print $cgi->header,$cgi->start_html('Hello'),$cgi->h1('Hello, World!'),$cgi->end_html;}
}# 实例化并启动服务器
my $server = MyWebServer->new(8080);
print "Server is running on http://localhost:8080\n";
$server->run();
​

以上代码创建了一个继承自 HTTP::Server::Simple::CGI 的简单服务器类 MyWebServer,并重写了 handle_request 方法来处理请求。

扩展服务器功能

可以通过扩展 handle_request 方法来增加服务器的功能。例如,解析请求路径并返回不同的内容:

use strict;
use warnings;
use HTTP::Server::Simple::CGI;{package MyWebServer;use base qw(HTTP::Server::Simple::CGI);sub handle_request {my ($self, $cgi) = @_;my $path = $cgi->path_info;if ($path eq '/hello') {print "HTTP/1.0 200 OK\r\n";print $cgi->header,$cgi->start_html('Hello'),$cgi->h1('Hello, World!'),$cgi->end_html;} elsif ($path eq '/goodbye') {print "HTTP/1.0 200 OK\r\n";print $cgi->header,$cgi->start_html('Goodbye'),$cgi->h1('Goodbye, World!'),$cgi->end_html;} else {print "HTTP/1.0 404 Not Found\r\n";print $cgi->header,$cgi->start_html('Not Found'),$cgi->h1('404 - Not Found'),$cgi->end_html;}}
}my $server = MyWebServer->new(8080);
print "Server is running on http://localhost:8080\n";
$server->run();
​

在这个示例中,服务器根据请求路径返回不同的内容。对于 /hello路径,返回“Hello, World!”消息;对于 /goodbye路径,返回“Goodbye, World!”消息;对于其他路径,返回404错误。

添加日志记录

为了便于调试和监控,可以添加日志记录功能,记录每个请求的信息:

use strict;
use warnings;
use HTTP::Server::Simple::CGI;
use POSIX qw(strftime);{package MyWebServer;use base qw(HTTP::Server::Simple::CGI);sub handle_request {my ($self, $cgi) = @_;my $path = $cgi->path_info;# 记录请求信息my $log_entry = strftime("[%Y-%m-%d %H:%M:%S]", localtime) . " - $path\n";open my $log, '>>', 'server.log' or die "Cannot open log file: $!";print $log $log_entry;close $log;if ($path eq '/hello') {print "HTTP/1.0 200 OK\r\n";print $cgi->header,$cgi->start_html('Hello'),$cgi->h1('Hello, World!'),$cgi->end_html;} elsif ($path eq '/goodbye') {print "HTTP/1.0 200 OK\r\n";print $cgi->header,$cgi->start_html('Goodbye'),$cgi->h1('Goodbye, World!'),$cgi->end_html;} else {print "HTTP/1.0 404 Not Found\r\n";print $cgi->header,$cgi->start_html('Not Found'),$cgi->h1('404 - Not Found'),$cgi->end_html;}}
}my $server = MyWebServer->new(8080);
print "Server is running on http://localhost:8080\n";
$server->run();
​

此代码段通过将每个请求的信息记录到 server.log 文件中,帮助开发者了解服务器的运行情况和请求历史。

http://www.lryc.cn/news/530982.html

相关文章:

  • C++滑动窗口技术深度解析:核心原理、高效实现与高阶应用实践
  • 基于构件的软件开发方法
  • 网站快速收录:如何设置robots.txt文件?
  • OpenGL学习笔记(六):Transformations 变换(变换矩阵、坐标系统、GLM库应用)
  • 8.攻防世界Web_php_wrong_nginx_config
  • 【优先算法】专题——位运算
  • qt.qpa.plugin: Could not find the Qt platform plugin “dxcb“ in ““
  • 1-刷力扣问题记录
  • 物联网 STM32【源代码形式-使用以太网】连接OneNet IOT从云产品开发到底层MQTT实现,APP控制 【保姆级零基础搭建】
  • 【单层神经网络】基于MXNet的线性回归实现(底层实现)
  • unity中的动画混合树
  • 《基于deepseek R1开源大模型的电子数据取证技术发展研究》
  • Potplayer常用快捷键
  • C++ Primer 自定义数据结构
  • 35.Word:公积金管理中心文员小谢【37】
  • 北京钟鼓楼:立春“鞭春牛”,钟鼓迎春来
  • 股票入门知识
  • Java自定义IO密集型和CPU密集型线程池
  • Git的安装步骤详解(复杂的安装界面该如何勾选?)
  • 文本预处理
  • SQLAlchemy 2.0的简单使用教程
  • 基于RAG的知识库问答系统
  • SQL/Panda映射关系
  • 自定义数据集 使用paddlepaddle框架实现逻辑回归
  • Docker入门篇(Docker基础概念与Linux安装教程)
  • c/c++高级编程
  • 2024-我的学习成长之路
  • vscode软件操作界面UI布局@各个功能区域划分及其名称称呼
  • xmind使用教程
  • Day33【AI思考】-分层递进式结构 对数学数系的 终极系统分类