В настоящее время я работаю над сценарием CGI python, сейчас я пытаюсь протестировать все на виртуальной машине Linux CentOS 7. Когда я захожу на веб-страницу, я получаю 500 внутреннюю ошибку сервера ... мой код выглядит так:
Master3.py (192.168.234.2) (192.168.234.2) /cgi-bin/master3.py)
#!/usr/bin/python3
###importeren van modules om deze vervolgens te kunnen gebruiken in het script###
import cgi, cgitb
import matplotlib.pyplot as plt
import io
import sqlite3
import base64
#maak een instance van een field storage
cgitb.enable()
form = cgi.FieldStorage()
#het verkrijgen van data van de velden die vanuit de agent worden opgestuurd.
machine = form.getvalue("machine")
cpu = form.getvalue("cpu")
ram = form.getvalue("ram")
total_mem = form.getvalue("total")
used_mem = form.getvalue("used")
free_mem = form.getvalue("free")
#print ("Content-type:text/html\n\n")
def create_table():
try:
con = sqlite3.connect('toets_school.db')
cursor = con.cursor()
print("Database aangemaakt en succesvol verbonden met SQLite")
query_create_table = """CREATE TABLE IF NOT EXISTS data (
machine VARCHAR UNIQUE,
cpu REAL,
total_mem REAL,
used_mem REAL,
free_mem REAL); """
cursor.execute(query_create_table)
print ("SQLite table aangemaakt")
cursor.close()
except sqlite3.Error as error:
print ("Error while creating a sqlite table", error)
finally:
if (con):
con.close()
print("sqlite connection is closed")
def insert_data(machine, cpu, total_mem, used_mem, free_mem):
try:
con = sqlite3.connect('toets_school.db')
cursor = con.cursor()
print("Database aangemaakt en succesvol verbonden met SQLite")
insert_query = """INSERT INTO data (machine, cpu, total_mem, used_mem, free_mem)
VALUES (?, ?, ?, ?, ?);"""
verkregen_data = (machine, cpu, total_mem, used_mem, free_mem)
cursor.execute(insert_query, verkregen_data)
con.commit()
print ("Total", cursor.rowcount,
"Data is succesvol in database toets_school.db geschreven")
con.commit()
cursor.close()
except sqlite3.Error as error:
print ("Failed to insert data into table", error)
finally:
if (con):
con.close()
print("SQLite connection is closed")
def read_data():
try:
con = sqlite3.connect('toets_school.db')
cursor = con.cursor()
print("Database aangemaakt en succesvol verbonden met SQLite")
read_query = """SELECT * from data"""
cursor.execute(read_query)
records = cursor.fetchall()
print ("Totaal aantal rijen zijn: ", len(records))
print ("print elke rij")
data = []
for rij in records:
data.append(rij[0])
data.append(rij[1])
data.append(rij[2])
data.append(rij[3])
data.append(rij[4])
# print (data)
print ("Machine: ", rij[0])
print ("CPU: ", rij[1])
print ("Totaal geheugen: ", rij[2])
print ("Gebruikt geheugen: ", rij[3])
print ("Vrije geheugen: ", rij[4])
print("\n")
return data
cursor.close()
except sqlite3.Error as error:
print("Failed to read data from table", error)
finally:
if (con):
con.close()
print("SQLite connection is closed")
#gegevens klaarmaken om te gebruiken in de grafieken
cpulist = []
ramlist = []
used_memlist = []
free_memlist = []
def chart_cpu():
plt.plot(cpulist)
plt.title("CPU gebruik")
plt.xlabel('minuten')
plt.ylabel('percentage(%)')
buffer1 = io.BytesIO()
plt.grid(True)
plt.savefig(buffer1, format="png")
plt.show()
return buffer1
def chart_ram():
plt.plot(ramlist)
plt.title("Geheugen")
plt.xlabel('minuten')
plt.ylabel('percentage (%)')
buffer2 = io.BytesIO()
plt.grid(True)
plt.savefig(buffer2, format="png")
plt.show()
return buffer2
def chart_used_mem():
plt.plot(used_memlist)
plt.title("Gebruikt geheugen")
plt.xlabel('Geheugen')
plt.ylabel('Tijd')
buffer3 = io.BytesIO()
plt.grid(True)
plt.savefig(buffer3, format="png")
plt.show()
return buffer3
def chart_free_mem():
plt.plot(free_memlist)
plt.title("Geheugen")
plt.xlabel('Geheugen')
plt.ylabel('Tijd')
buffer4 = io.BytesIO()
plt.grid(True)
plt.savefig(buffer4, format="png")
plt.show()
return buffer4
create_table()
insert_data (machine, cpu, total_mem, used_mem, free_mem)
read_data()
chart_cpu()
chart_ram()
chart_used_mem()
chart_free_mem()
var1 = chart_cpu()
var2 = chart_ram()
var3 = chart_used_mem()
var4 = chart_used_mem()
print ("Content-type:text/html\n\n")
print ("<center>")
print ('De naam van deze grafieken is van :', machine)
print ('De totale geheugen van deze machine is: ', total_mem,'GB')
print ("<br>")
print ("<html><head><title>Website HAF sportschool</title></head><body>")
print("<img src='data:image/png;base64,"+str(base64.b64encode(var1.getvalue()).decode('ascii'))+"' />")
print("<img src='data:image/png;base64,"+str(base64.b64encode(var2.getvalue()).decode('ascii'))+"' />")
print("<img src='data:image/png;base64,"+str(base64.b64encode(var3.getvalue()).decode('ascii'))+"' />")
print("<img src='data:image/png;base64,"+str(base64.b64encode(var4.getvalue()).decode('ascii'))+"' />")
print ("</body></html>")
Любой, кто знает, почему я получаю ошибку: [cgi: error] [pid 1217] [client 192.168.234.1:55101] искаженный заголовок из скрипта 'master3.py': Неверный заголовок: База данных aangemaakt en succesv
httpd conf выглядит следующим образом:
AllowOverride All
Options +ExecCGI
AddHandler cgi-script .py .cgi
Require all granted
</Directory>