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

embedded/2025/2/4 17:10:05/

在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.ppmy.cn/embedded/159524.html

相关文章

笔记本搭配显示器

笔记本:2022款拯救者Y9000P,显卡RTX3060,分辨率2560*1600,刷新率:165Hz,无DP1.4口 显示器:2024款R27Q,27存,分辨率2560*1600,刷新率:165Hz &…

android 圆形弹窗摄像头开发踩坑——源码————未来之窗跨平台操作

一、飘窗刷脸&#xff0c;拍照采用飘窗 刷脸认证安卓接口采用飘窗具有在不干扰用户主要操作的前提下以醒目方式引导用户完成认证&#xff0c;且能灵活定制样式以提升用户体验和认证效率的优点 二、踩坑只有一个扇形 <?xml version"1.0" encoding"utf-8&quo…

vector容器(详解)

本文最后是模拟实现全部讲解&#xff0c;文章穿插有彩色字体&#xff0c;是我总结的技巧和关键 1.vector的介绍及使用 1.1 vector的介绍 https://cplusplus.com/reference/vector/vector/&#xff08;vector的介绍&#xff09; 了解 1. vector是表示可变大小数组的序列容器。…

蓝桥杯嵌入式省赛lcd模版

LCD原理图 可以参考和使用官方的参考例程 配置完引脚后点击GENERATE CODE即可无需其他操作 将这两个头文件复制到 在给的案例路径下将lcd.c也复制到bsp路径下面 #include "my_main.h" uint8_t led_sta0x10; char text[30]; void LED_Disp(uint8_t dsLED) {HAL_GPIO…

49【服务器介绍】

服务器和你的电脑可以说是一模一样的&#xff0c;只不过用途不一样&#xff0c;叫法就不一样了 物理服务器和云服务器的区别 整台设备眼睛能够看得到的&#xff0c;我们一般称之为物理服务器。所以物理服务器是比较贵的&#xff0c;不是每一个开发者都能够消费得起的。 …

【Linux系统】—— make/makefile

【Linux系统】—— make/makefile 1 什么是 make/makefile2 第一版本makefile3 依赖关系和依赖方法4 清理4.1 清理的基本语法4.2 make 的默认执行4.3 为什么要加 『.PHONY:clean』4.3.1 『.PHONY:clean』的功能4.3.2 如何理解总是不被执行4.3.2 如何区分文件的新旧 5 第二版本m…

bat脚本实现自动化漏洞挖掘

bat脚本 BAT脚本是一种批处理文件&#xff0c;可以在Windows操作系统中自动执行一系列命令。它们可以简化许多日常任务&#xff0c;如文件操作、系统配置等。 bat脚本执行命令 echo off#下面写要执行的命令 httpx 自动存活探测 echo off httpx.exe -l url.txt -o 0.txt nuc…

mac连接linux服务器

1、mac连接linux服务器 # ssh -p 22 root192.168.1.152、mac指定密码连接linux服务器 (1) 先安装sshpass,下载后解压执行 ./configure && make && makeinstall https://sourceforge.net/projects/sshpass/ (2) 连接linux # sshpass -p \/\\\[\!\\wen12\$ s…