2016-08-28 79 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