[英]wordpress on apache2 running python as cgi
So here is what I want to do:所以这就是我想做的:
On my Raspi a python program is running.在我的 Raspi 上,一个 python 程序正在运行。 On a wordpress site the current state of the program should be displayed and some configurations should be changeable.
在 wordpress 站点上,应显示程序的当前 state,并且某些配置应该可以更改。
Here is the problem:这是问题所在:
Whenever I want to execute the python script, I get a 500 error code.每当我想执行 python 脚本时,我都会收到 500 错误代码。 It doesn't matter if I just want to display the value or change it.
如果我只想显示值或更改它并不重要。 I'm new to html, cgi and apache, tried a lot but now I have no clue how to continue.
我是 html、cgi 和 apache 的新手,尝试了很多但现在我不知道如何继续。 I'd appreciate it a lot if someone could point me in the right direction.
如果有人能指出我正确的方向,我将不胜感激。
Here are my configurations:这是我的配置:
Apache: Apache:
Edited the file /etc/apache2/apache2.conf:编辑文件 /etc/apache2/apache2.conf:
<Directory /var/www/>
Options +ExecCGI +Indexes +FollowSymLinks
AddHandler cgi-script .cgi .py
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
<Directory "/var/www/cgi-bin">
Options All
</Directory>
I also ran sudo a2enmod cgi
我也跑了
sudo a2enmod cgi
The webserver directory (/var/www/) looks like this: Web 服务器目录 (/var/www/) 如下所示:
.
├── cgi-bin
└── html
├── pma
│ └── ...
└── wordpress
└── ...
Wordpress: Wordpress:
On a wordpress site, I go into the "text" mode and have the following html code:在 wordpress 站点上,我 go 进入“文本”模式并具有以下 html 代码:
Curent Value: <form action="/cgi-bin/apfautostartval.py" method="get"></form>
<form action="/cgi-bin/apfcgi.py" method="post" target="_blank">
<input name="autoTest" type="radio" value="True" /> True (do automatic scan)
<input name="autoTest" type="radio" value="False" /> False (do manual scan)
<input type="submit" value="Submit" /></form>
Python files: Python 个文件:
The apfautostartval.py should just get the value from the config.ini and post it: apfautostartval.py 应该只从 config.ini 中获取值并将其发布:
#!/usr/bin/python3
import configparser
import os
import cgi, cgitb
cgitb.enable()
# Create config parser
config = configparser.ConfigParser()
configFilePath = os.path.join(os.path.sep,"home","pi",..., "config.ini")
config.read(configFilePath)
print("Content-type: text/html")
print()
print("<!DOCTYPE html>")
print("<html>")
print("<body>")
print(str(config['SETTINGS']["autoTest"]))
print("</body>")
print("</html>")
And finally the apfcgi.py should receive the submitted new value and write it to the config.ini:最后 apfcgi.py 应该接收提交的新值并将其写入 config.ini:
#!/usr/bin/python3
import configparser
import os
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
cgitb.enable()
# Create config parser
config = configparser.ConfigParser()
configFilePath = os.path.join(os.path.sep,"home","pi",..., "config.ini")
print("Content-type: text/html")
print()
print("<!DOCTYPE html>")
print("<html>")
print("<body>")
# Receive autotest command from web site
if form.getvalue("autoTest"):
config.read(configFilePath)
if form.getvalue("autoTest").lower() == "true":
config['SETTINGS']["autoTest"] = "True"
else:
config['SETTINGS']["autoTest"] = "False"
with open(configFilePath, 'w') as configfile:
config.write(configfile)
print("</body>")
print("</html>")
I had the same problem.我有同样的问题。 The problem was in the encoding.
问题出在编码上。 By default, ConfigParser.read() uses the encoding=none parameter.
默认情况下,ConfigParser.read() 使用encoding=none参数。 I specified utf-8 and it worked.
我指定了 utf-8,它起作用了。
config = configparser.ConfigParser()
config.read(configFilePath, encoding='utf-8')
Ok I have found the solution for one part of the problem: 好的,我已经找到了解决问题的一部分的方法:
When changing the value make sure, you granted permissions to the file for cgi scripts: 更改值时,请确保已为cgi脚本授予了该文件的权限:
in /etc/apache2/apache2.config add: 在/etc/apache2/apache2.config中添加:
<Directory "/home/pi/dirToTheConfigFile/">
Options +ExecCGI +Indexes +FollowSymLinks
AddHandler cgi-script .cgi .py
AllowOverride None
Require all granted
</Directory>
And make sure, the user www-data is allowed to modify the file. 并确保允许用户www-data修改文件。
In general add cgitb.enable()
to your python scripts, so for html 500 errors you will get a detailed error message in the /var/log/apache2/error.log
file 通常将
cgitb.enable()
添加到python脚本中,因此对于html 500错误,您将在/var/log/apache2/error.log
文件中获得详细的错误消息。
For receiving data the following Solution was found: The Python script was unfortunately owned by root and therefore cound not be executed. 为了接收数据,找到了以下解决方案:不幸的是,Python脚本是root拥有的,因此无法执行cound。 I changed the permissions.
我更改了权限。
The receiving html in the wordpress has been rewritten, too: WordPress中的接收html也已被重写:
<iframe src="/cgi-bin/apfautostartval.py" width="100" height="29.5" frameborder="0" marginwidth="8" marginheight="0" scrolling="no" align="bottom"></iframe>
It's now an iframe and displays whatever is returned. 现在是iframe,并显示返回的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.