【问题标题】:How to get methods from HTTP::Daemon如何从 HTTP::Daemon 获取方法
【发布时间】:2019-06-12 06:01:38
【问题描述】:

如何找到HTTP::Daemon 模块中的$code$mess?在 cpan 中的用法是

$c->send_status_line( $code, $mess, $proto )

但我不知道从哪里/如何获得$code$mess

例如,send_error($code) 用作send_error(RC_FORBIDDEN),这是我从网上某人的代码中找到的,他从哪里得到RC_FORBIDDEN? 一直在玩下面的代码。很抱歉格式化,非常感谢@choroba 为我格式化。

    use warnings;
    use strict;
    use HTTP::Daemon;
    use HTTP::Status;
    use LWP;

    my $daemon = HTTP::Daemon->new or die;
    my $d = HTTP::Daemon->new(
    LocalAddr => '0.0.0.0',
    LocalPort => '5000',
    );
    printf ("\n\n   URL of webserver is %s, show this script with %stest\n", 
    $d->url, $d->url);

    while (my $client_connection = $d->accept)  
            {
                new_connection($client_connection);
            }
    sub new_connection 
    { 
    my $client_connection = shift;
    printf "new connection\n";
    while (my $request = $client_connection->get_request) 
    {
        if (my $pid = fork)
            {
                print "Child created : $pid\n";
            }
        elsif (!defined $pid)
            {
                die "Cannot fork $!\n";
            }
        else
            {
                my $address_of_client = $client_connection->peerhost();
                my $port_of_client = $client_connection->peerport();
    print "Connection from client $address_of_client on port 
    $port_of_client\n";
                print "  request\n";
                    if ($request->method eq 'GET' and $request->uri->path 
    eq "/test") 
                        {
                            $client_connection->send_file_response(RC_OK);
                            #$client_connection->send_status_line(200);
                            #print "OK ";
                            #$client_connection->send_file_response($0);
                        }
                    else 
                        {
                            $client_connection->send_error(RC_NOT_FOUND);
                        }
            }
    $client_connection->close;
    } 
   }

【问题讨论】:

  • 您要发送什么代码和消息?如果你不想发送任何代码和消息,为什么要调用该方法?

标签: perl


【解决方案1】:

文档还说明

如果省略$code,则假定为 200。如果省略$mess,则插入与$code 对应的消息。如果$proto 缺失,则使用$HTTP::Daemon::PROTO 变量的内容。

因此,您根本不必指定参数。否则,只需将任何可能的HTTP status codes 用于$code,或者不指定$mess 以获取代码的默认消息,或者使用您喜欢的任何消息。

RC_FORBIDEN 是从HTTP::Status 导出的。

【讨论】:

  • 再次感谢@choroba,我尝试了所有被注释掉的东西,但没有运气。读取 $0 显示已执行的文件名,而 $0 打印整个脚本。 '$client_connection->send_file_response($0);'
  • 我仍然无法理解这一点。有人请为我澄清这一点。我想要的只是将 send_status_line 发送回模块说可以做的客户端。但我没有上浏览器。我变得空白,我正在使用: if ($request->method eq 'GET' and $request->uri->path eq "/test") { $client_connection->send_status_line(); }
  • send_file_response,如文档所述,将文件名作为参数并发送文件内容。给它 $0,它会返回脚本的内容。
猜你喜欢
  • 1970-01-01
  • 2017-11-22
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
相关资源
最近更新 更多