所以我有一个CGI脚本(我只是把我的python脚本重命名为.cgi)基本上做了一些网页抓取并根据他所抓的内容写了几个文件。 但是每次我尝试运行它时,都会收到无法加载资源的情况:服务器响应状态为500(内部服务器错误)。
这是我的apache.conf:
ScriptAlias /cgi-bin/ /var/www/html/animals
<Directory /var/www/html/animals>
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>
这就是我调用脚本的方式:
<script>
jQuery.ajax({
type: "GET",
url: "../animals/GetAnimals.cgi",
success: function (msg) {
console.log("Sucess!");
}
});
</script>
代码:
#!/usr/bin/env python
from bs4 import BeautifulSoup
from urllib.request import urlopen
from urllib.request import Request
import json, codecs
from string import Template
import glob, os
import cgi, cgitb
cgitb.enable()
filelist = glob.glob('*.txt')
for file in filelist:
os.remove(file)
#open the website, save the html and close the connection
web_request = Request('super secret url', headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0
Safari/537.36'})
web_client = urlopen(web_request)
html_page = web_client.read().decode('UTF-8')
web_client.close()
page_soup = BeautifulSoup(html_page, 'html.parser')
#do a bunch of stuff with beautifulsoup and string manipulation
for animal in animals:
#write a file here
data = {}
data['Animals'] = final_json
with open('animals.json', 'w') as f:
json.dump(data, f, ensure_ascii=False, sort_keys=True, indent=4)
是的,我已经用sudo a2enmod cgi启用了cgi并在之后重新启动。