使 perl 从 index.html 文件中读取 [英] Making perl read from index.html file

查看:60
本文介绍了使 perl 从 index.html 文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 perl 作为特定地址的 Web 服务器:端口号,10.x.x.x:portNumber,同时还让它在默认情况下显示我的 index.html 文件中的内容.但是,当我在浏览器中运行 10.x.x.x:portNumber 时,perl 不会显示 index.html 的内容.如何让 perl 从文件中读取?

I'm trying to use perl as my web server for a specific address:port number, 10.x.x.x:portNumber, while also having it show what's in my index.html file by default. However, perl does not show the contents of index.html when I run 10.x.x.x:portNumber in my browser. How can I get perl to read from a file?

这是我正在处理这个问题的代码.

This is the code I'm working with for this problem.

#!/usr/bin/perl
{
package MyWebServer;
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
my %dispatch = (
    '/' => \&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 Perl"),
        $cgi->end_html;
}
}


my $pid = MyWebServer->new(XXXX)->background();
print "Use 'kill $pid' to stop server.\n";

谢谢.

推荐答案

当您访问服务器的根 URL 时,您的代码已明确配置运行 resp_hello().

Your code is explicitly configured run resp_hello() when you access your server's root URL.

my %dispatch = (
    '/' => \&resp_hello,
);

我不知道您希望实现什么 URL 结构,但是如果您想区分 //index.html 那么您可以这样做像这样:

I don't know exactly what URL structure you are hoping to implement, but if you're trying to differentiate / and /index.html then you could do something like this:

my %dispatch = (
    '/' => \&resp_hello,
    '/index.html' => \&resp_index,
);

然后编写一个 resp_index() 子程序,它打开 index.html 文件并将内容返回给浏览器.

and then write a resp_index() subroutine which opens the index.html file and returns the contents to the browser.

我想你可以扩展它并直接提供文件系统上存在的任何文件.

You could extend this I suppose and serve any files that exist on the filesystem directly.

但我想知道为什么你要自己做这一切,而不只是寻求使用 PSGI 和 Plack 的解决方案

But I have to wonder why you're doing this all yourself and not just reaching for a solution that uses PSGI and Plack. Where did you get the idea to use HTTP::Server::Simple?

我很震惊地看到文档鼓励使用来自 CGI.pm 的 HTML 生成函数.很长一段时间以来,我们都知道他们的想法是多么糟糕,它们现在已被弃用.

And I'm appalled to see documentation that encourages the use of the HTML-generation functions from CGI.pm. We've all known what a terrible idea they are for some considerable time, and they are now deprecated.

在您花太长时间沿​​着这条路走之前,我建议您研究更多标准的 Perl Web 工具.

Before you spend too long going down this path, I'd urge you to investigate more standard Perl web tools.

这篇关于使 perl 从 index.html 文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆