gpt4 book ai didi

Perl HTTP 服务器

转载 作者:行者123 更新时间:2023-12-03 18:16:20 25 4
gpt4 key购买 nike

我是 Perl 的新手,我有一个关于 HTTP 服务器和客户端 API 的问题。

我想编写一个接受来自 HTTP 客户端的请求的 HTTP 服务器。问题是我不知道该怎么做,因为我是一名 Java 开发人员,这对我来说有点困难。请给我一些教程和示例 HTTP::Daemon Perl 的模块?

最佳答案

我花了很多时间尝试让许多用户同时创建一个“简单”可用的 Web 服务器。 HTTP::Daemon 的文档和其他在线资源对我没有帮助。

这是一个工作(Ubuntu 12.10,默认 Perl 包 v5.14.2)示例 预 fork 具有不同内容类型页面和错误页面的 Web 服务器:

#!/usr/bin/perl

use strict;
use warnings;

use CGI qw/ :standard /;
use Data::Dumper;
use HTTP::Daemon;
use HTTP::Response;
use HTTP::Status;
use POSIX qw/ WNOHANG /;

use constant HOSTNAME => qx{hostname};

my %O = (
'listen-host' => '127.0.0.1',
'listen-port' => 8080,
'listen-clients' => 30,
'listen-max-req-per-child' => 100,
);

my $d = HTTP::Daemon->new(
LocalAddr => $O{'listen-host'},
LocalPort => $O{'listen-port'},
Reuse => 1,
) or die "Can't start http listener at $O{'listen-host'}:$O{'listen-port'}";

print "Started HTTP listener at " . $d->url . "\n";

my %chld;

if ($O{'listen-clients'}) {
$SIG{CHLD} = sub {
# checkout finished children
while ((my $kid = waitpid(-1, WNOHANG)) > 0) {
delete $chld{$kid};
}
};
}

while (1) {
if ($O{'listen-clients'}) {
# prefork all at once
for (scalar(keys %chld) .. $O{'listen-clients'} - 1 ) {
my $pid = fork;

if (!defined $pid) { # error
die "Can't fork for http child $_: $!";
}
if ($pid) { # parent
$chld{$pid} = 1;
}
else { # child
$_ = 'DEFAULT' for @SIG{qw/ INT TERM CHLD /};
http_child($d);
exit;
}
}

sleep 1;
}
else {
http_child($d);
}

}

sub http_child {
my $d = shift;

my $i;
my $css = <<CSS;
form { display: inline; }
CSS

while (++$i < $O{'listen-max-req-per-child'}) {
my $c = $d->accept or last;
my $r = $c->get_request(1) or last;
$c->autoflush(1);

print sprintf("[%s] %s %s\n", $c->peerhost, $r->method, $r->uri->as_string);

my %FORM = $r->uri->query_form();

if ($r->uri->path eq '/') {
_http_response($c, { content_type => 'text/html' },
start_html(
-title => HOSTNAME,
-encoding => 'utf-8',
-style => { -code => $css },
),
p('Here are all input parameters:'),
pre(Data::Dumper->Dump([\%FORM],['FORM'])),
(map { p(a({ href => $_->[0] }, $_->[1])) }
['/', 'Home'],
['/ping', 'Ping the simple text/plain content'],
['/error', 'Sample error page'],
['/other', 'Sample not found page'],
),
end_html(),
)
}
elsif ($r->uri->path eq '/ping') {
_http_response($c, { content_type => 'text/plain' }, 1);
}
elsif ($r->uri->path eq '/error') {
my $error = 'AAAAAAAAA! My server error!';
_http_error($c, RC_INTERNAL_SERVER_ERROR, $error);
die $error;
}
else {
_http_error($c, RC_NOT_FOUND);
}

$c->close();
undef $c;
}
}

sub _http_error {
my ($c, $code, $msg) = @_;

$c->send_error($code, $msg);
}

sub _http_response {
my $c = shift;
my $options = shift;

$c->send_response(
HTTP::Response->new(
RC_OK,
undef,
[
'Content-Type' => $options->{content_type},
'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'no-cache',
'Expires' => 'Thu, 01 Dec 1994 16:00:00 GMT',
],
join("\n", @_),
)
);
}

关于Perl HTTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14425220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com