找不到错误原因500 - Perl

时间:2016-08-28 21:30:02

标签: perl cgi

我是Perl的新手并且在CGI中使用它。我已经有这个错误500几个小时仍然不知道错误在哪里。该脚本放在Apache服务器的相应/usr/lib/cgi-bin文件夹中。然后通过这个简单的HTML表单调用它:

<FORM action="http://localhost/cgi-bin/sensors.cgi" method="POST">
Sample period: <input type="text" name="sample_period">  <br>
<input type="submit" value="Submit">
</FORM>

据我所知,如果上传不当或脚本中有错误,则会出现错误500。但我已经测试过上传其他文件并且没有遇到任何问题。这就是为什么我认为代码中可能存在错误。这是Perl脚本:

#!/usr/bin/perl
use IO::Handle;

# Open the output file that contains the sensors' readings. It's open in write mode, and
# empties the content of the file on each opening.
open (my $readings, ">", "sensors_outputs.txt") || die "Couldn't open the output file.\n";


# Defines the physical magnitudes and sets each one a random value.
my $temp = rand 30;
my $hum = rand 100;
my $pres = 1000 + rand(1010 - 1000);
my $speed = rand 100;

for(;;) {
    # Writes in the file-handler's file the values of the physical magnitudes.
    print $readings "$temp\n$hum\n$pres\n$speed";
    # Flush the object so as not to open and close the file each time a new set of
    # values is generated.
    $readings->autoflush;
    # Move the file-handler to the beggining of the file.
    seek($readings, 0, SEEK_SET);
    # Generate new a new data set.
    $temp = rand 10;
    $hum = rand 100;
    $pres = 1000 + rand 10;
    $speed = rand 100;
    sleep 1;
}
close $readings || die "$readings: $!";

如果需要,请随时向我询问更多背景信息。提前致谢

1 个答案:

答案 0 :(得分:2)

显而易见的问题是脚本没有输出CGI规范要求的HTTP响应。

至少你需要这样的东西:

print "Status: 204 No Content", "\n\n";

但是更常见的是说

print "Status: 200 OK", "\n";
print "Content-type: text/plain", "\n\n";
print "Success!";

那就是说,500只是意味着存在错误。它可能与代码有关。它可能与服务器配置有关。以上是一个明显的问题,但可能还有其他问题。当您收到500错误时,您需要查看服务器的错误日志,并查看错误消息的实际内容。在你这样做之前尝试解决问题没有意义。