这是请求网址http://localhost:9009/?comd&user=kkc&mail=kkc@kkc.com
服务器perl脚本需要做哪些修改。
use IO::Socket;
use Net::hostent; # for OO version of gethostbyaddr
$PORT = 9009; # pick something not in use
$server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1);
die "can't setup server" unless $server;
print "[Server $0 accepting clients]\n";
while ($client = $server->accept())
{
$client->autoflush(1);
print $client "Welcome to $0; type help for command list.\n";
$hostinfo = gethostbyaddr($client->peeraddr);
printf "[Connect from %s]\n", $hostinfo ? $hostinfo->name : $client->peerhost;
print $client "Command? ";
while ( <$client>) {
next unless /\S/; # blank line
if (/comd/i ) { print $client `dir`; }
} continue {
print $client "Command? ";
}
close $client;
print "client closed";
}
答案 0 :(得分:0)
我认为您的脚本不是用于制作,而是用于家庭作业或测试。 Perl中有多个非常高效的Web服务器解决方案,如Apache,包含CGI或mod_perl,HTTP::Server::Simple和PSGI/Plack。
您通常也会使用像Dancer,Mojo或Catalyst这样的框架,它可以为您完成大部分无聊的标准内容:
use Dancer;
get '/' => sub {
return 'Hi there, you just visited host '.request->host.
' at port '.request->port.' asking for '.request->uri;
};
回到你的问题:你的脚本是一个交互式服务器,而HTTP有一个严格的请求和响应结构:
您需要删除交互式部分,然后等待客户端开始对话:
use IO::Socket;
use Net::hostent; # for OO version of gethostbyaddr
$PORT = 9009; # pick something not in use
$server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1);
die "can't setup server" unless $server;
print "[Server $0 accepting clients]\n";
while ($client = $server->accept())
{
$hostinfo = gethostbyaddr($client->peeraddr);
# Read request up to a empty line
my $request;
while ( <$client>) {
last unless /\S/;
$request .= $_;
}
# Do something with the request
# Send response
print $client "Status: 200 OK\r\nContent-type: text/plain\r\n\r\n".$request;
close $client;
print "client closed";
}
服务器从客户端读取完整请求,并返回最小化的HTTP标头和原始请求。