HTML CGI脚本与MySQL

62o28rlo  于 2022-12-09  发布在  Mysql
关注(0)|答案(1)|浏览(117)

我只是试图写简单的代码插入数据从形式到SQL DB使用Python通过CGI。我得到CGI部分的工作,它的工作正常,但一旦我添加导入语句的mysql它打破了它,我得到内部服务器错误代码。以下是我得到的:

#!/usr/bin/python3
#!/usr/bin/mysql

import cgi, cgitb
import mysql

print("Content-Type: text/html\n")
print("<html>")

form = cgi.FieldStorage()
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print("Hello %s %s" % (first_name, last_name))

print("</html>")

如果我删除了导入的mysql,那么表单又可以正常工作了。我遗漏了什么吗?这是在我构建的Apache/MySQL服务器上运行的。
以下是Apache错误日志:

ModuleNotFoundError: No module named 'MySQLdb'
[Thu Dec 08 12:08:32.460712 2022] [cgid:error] [pid 4890:tid 139695757391616] [client 172.16.19.73:60330] End of script output before headers: getData.py, referer: http://10.143.110.70/user_add.html
Traceback (most recent call last):
  File "/var/www/html/getData.py", line 3, in <module>
    import mysql
ModuleNotFoundError: No module named 'mysql'
[Thu Dec 08 12:13:57.193858 2022] [cgid:error] [pid 4890:tid 139695790962432] [client 172.16.19.73:60658] End of script output before headers: getData.py, referer: http://10.143.110.70/user_add.html
Traceback (most recent call last):
  File "/var/www/html/getData.py", line 5, in <module>
    import mysql
ModuleNotFoundError: No module named 'mysql'
[Thu Dec 08 12:29:46.442151 2022] [cgid:error] [pid 4890:tid 139695832925952] [client 172.16.19.73:61568] End of script output before headers: getData.py, referer: http://10.143.110.70/user_add.html
Traceback (most recent call last):
  File "/var/www/html/getData.py", line 6, in <module>
    import mysql
ModuleNotFoundError: No module named 'mysql'
[Thu Dec 08 12:32:06.582967 2022] [cgid:error] [pid 4890:tid 139695841318656] [client 172.16.19.73:61611] End of script output before headers: getData.py, referer: http://10.143.110.70/user_add.html
Traceback (most recent call last):
  File "/var/www/html/getData.py", line 6, in <module>
    import mysql
ModuleNotFoundError: No module named 'mysql'
[Thu Dec 08 12:56:43.385720 2022] [cgid:error] [pid 4890:tid 139695698642688] [client 172.16.19.73:62714] End of script output before headers: getData.py, referer: http://10.143.110.70/user_add.html
Traceback (most recent call last):
  File "/var/www/html/getData.py", line 5, in <module>
    import MySQLdb
ModuleNotFoundError: No module named 'MySQLdb'
[Thu Dec 08 13:37:12.045851 2022] [cgid:error] [pid 4662:tid 139695698642688] [client 172.16.19.73:64108] End of script output before headers: getData.py, referer: http://10.143.110.70/user_add.html
e3bfsja2

e3bfsja21#

在查看Apache错误后很好地找到了它。必须安装mysql连接器:

pip3 install mysql-connector-python

相关问题