2016-08-28 55 views
0

我是新来的Perl及其在CGI中的使用。我一直有这个错误500几个小时,仍然不知道错误(s)在哪里。该脚本放置在Apache服务器的相应/usr/lib/cgi-bin文件夹中。它然后通过这个简单的HTML表单称为:找不到错误500的原因 - Perl

<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> 

由于FAS,因为我知道,一个错误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

这些天,Perl社区已经基本上从CGI转移到了[PSGI/Plack](http://plackperl.org/)。 – Quentin

+1

[严格使用,使用警告](http://perlmaven.com/always-use-strict-and-use-warnings) – Quentin

+0

@Quentin:“离开CGI”是一回事,但不是每个人都会转向正义一个框架... – stevieb

回答

2

存在的明显问题是脚本不输出HTTP响应,CGI规范要求。

你至少需要类似:

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

,但它会更平常说

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

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

+1

错误500通过添加您所述的内容来解决。这是我第一次使用Apache,之前从未使用过日志,但现在我知道如何。感谢您指出。 – tulians