简体   繁体   English

使用perl HTTP :: Daemon的HTTP服务器 - 仅在运行服务器的同一台计算机上运行

[英]HTTP server using perl HTTP::Daemon - only works from the same computer the server is running on

I am going to create a simple HTTP server that I will also create some clients for it. 我将创建一个简单的HTTP服务器,我也将为它创建一些客户端。 Right now I am testing my simple server created in perl using HTTP::Daemon by a browser. 现在我正在通过浏览器使用HTTP :: Daemon测试我在perl中创建的简单服务器。

My server code is as follows: 我的服务器代码如下:

use HTTP::Daemon;
use HTTP::Status;

my $d = HTTP::Daemon->new(LocalPort => 80,
                      Listen => 20) || die;

print "Web Server started!\n";
print "Server Address: ", $d->sockhost(), "\n";
print "Server Port: ", $d->sockport(), "\n";


print "Please contact me at: <URL:", $d->url, ">\n";
while (my $c = $d->accept) {

    print "received a request...\n";

    while (my $r = $c->get_request) {

        print "URI: " . $r->uri->path . "\n";

        if ($r->method eq 'GET' and $r->uri->path eq "/") {

            # remember, this is *not* recommended practice :-)

            $c->send_file_response("http_daemon_test.pl");

        }

        else {

            print "method: " . $r->method;

            print " uri:" . $r->uri . "\n";

            $c->send_error(RC_FORBIDDEN, "please do not try again")
        }
    }

    $c->close;

    undef($c);
}

Currently, my problem is that this server works correctly when I test it on the same machine. 目前,我的问题是,当我在同一台机器上测试它时,此服务器正常工作。 When I try to access the server by another computer in the same network, the server does not receive a request at all. 当我尝试通过同一网络中的另一台计算机访问服务器时,服务器根本不会收到请求。 Am I missing a mandatory (maybe simple? I'm a newbie!) step to expose the server to the outside world? 我是否错过了将服务器暴露给外界的强制性(可能很简单?我是新手!)步骤?

I appreciate your help with this regard, 感谢你对这方面的帮助,


Update: bellow is an example of the output I receive when I run the server: 更新:bellow是我运行服务器时收到的输出示例:

Web Server started! Web Server启动了!

Server Address: 0.0.0.0 服务器地址:0.0.0.0

Server Port: 80 服务器端口:80

Please contact me at: 请联系我:

received a request... 收到了请求......

received a request... 收到了请求......

URI: / URI:/

received a request... 收到了请求......

URI: / URI:/

received a request... 收到了请求......

received a request... 收到了请求......

URI: / URI:/


Latest update: 最新更新:

I realized that the problem is different from what I described above. 我意识到问题与我上面描述的不同。 After all, I could access my server from another computer. 毕竟,我可以从另一台计算机访问我的服务器。 As you can see from the code, I'm not creating a separate thread to fulfill the request. 从代码中可以看出,我没有创建单独的线程来完成请求。 Strangely, when I access my server from a browser, all requests from other computers will time-out until I close the browser I initially sent the request from (and got the answer for the request). 奇怪的是,当我从浏览器访问我的服务器时,来自其他计算机的所有请求都将超时,直到我关闭我最初发送请求的浏览器(并获得请求的答案)。 Meanwhile, multiple requests from the same browser are fulfilled successfully. 同时,来自同一浏览器的多个请求成功完成。 This does not happen, though, when I sent requests from clients written in Perl using libwww-perl library. 但是,当我使用libwww-perl库向Perl编写的客户端发送请求时,情况不会发生。

How does this translate to the initial problem? 这如何转化为最初的问题? Every time I wanted to test my server, I did so first from the same machine using a browser. 每次我想测试我的服务器时,我首先使用浏览器从同一台机器上测试。 Then, I tried to access the server from another machine without closing the browser I initially tested the server with; 然后,我试图从另一台机器访问服务器而不关闭我最初测试服务器的浏览器; and, as expected, it didn't work. 而且,正如预期的那样,它没有用。

Thanks for your comments guys. 谢谢你们的评论。

Q: Does the PC (eg "myhttpserver") have a firewall? 问:PC(例如“myhttpserver”)是否有防火墙? Have you tried disabling it? 你试过禁用吗?

Q: Have you tried "telnet myhttpserver 80"? 问:你试过“telnet myhttpserver 80”吗? Can you "ping httpserver"? 你能“ping httpserver”吗?

This sounds like it's more of a networking/network configuration question than a programming question. 这听起来像是一个网络/网络配置问题,而不是编程问题。 Please make sure the hosts can see each other, and that the http port is open. 请确保主机可以看到对方,并且http端口已打开。

It seems that you didn't define LocalAddr in parameters for HTTP::Daemon constructor. 您似乎没有在HTTP :: Daemon构造函数的参数中定义LocalAddr。 It possibly defaults to localhost, in which case you will see the symptoms exactly as you provided. 它可能默认为localhost,在这种情况下,您将完全按照提供的方式查看症状。 Try to add 尝试添加

LocalAddr => '<your ip>'

to your constructor and see if it helps 到你的构造函数,看看它是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM