[英]How to make CGIHTTPRequestHandler execute my PHP scripts with POST data
I am currently trying to do simple web stuff with the http.server module in Python. When I try to POST a form to my script, it does not receive the POST data, $_POST
ist empty and file_get_contents('php://input')
as well.我目前正在尝试使用 Python 中的http.server模块执行简单的 web 操作。当我尝试将表单发布到我的脚本时,它没有收到 POST 数据,
$_POST
_POST 是空的并且file_get_contents('php://input')
也是如此。
This is my post_test.html
:这是我的
post_test.html
:
#!/usr/bin/php
<!DOCTYPE html>
<html>
<body>
<form method="post" action="post_test.html">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
echo "RAW POST: " . file_get_contents('php://input') . "<br>";
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
And this is my server script:这是我的服务器脚本:
import urllib.parse
from http.server import CGIHTTPRequestHandler, HTTPServer
hostName = "localhost"
serverPort = 8080
handler = CGIHTTPRequestHandler
handler.cgi_directories.append('/php-cgi')
class MyServer(handler):
def do_GET(self):
# get path without first '/' and without anything besides path and filename
file = self.path[1:].split("?")[0].split("#")[0]
# if the file is in the script list, execute from php-cgi, else load from webapp
php_handle = ["post_test.html"]
if file in php_handle:
self.path = "/php-cgi" + self.path
else:
self.path = "/webapp" + self.path
CGIHTTPRequestHandler.do_GET(self)
def do_POST(self):
# get path without first '/' and without anything besides path and filename
file = self.path[1:].split("?")[0].split("#")[0]
# if the file is in the script list, execute from php-cgi
php_handle = ["post_test.html"]
if file in php_handle:
length = int(self.headers['Content-Length'])
post_data = urllib.parse.parse_qs(self.rfile.read(length).decode('utf-8'))
for key, data in post_data.items():
self.log_message(key + ": " + str.join(", ", data))
self.path = "/php-cgi" + self.path
CGIHTTPRequestHandler.do_POST(self)
def do_HEAD(self):
CGIHTTPRequestHandler.do_HEAD(self)
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")
Okay, I was finally able to solve the problem: #!/usr/bin/php
is wrong, it should be #!/usr/bin/php-cgi
.好的,我终于能够解决问题了:
#!/usr/bin/php
是错误的,应该是#!/usr/bin/php-cgi
。
Reason : php
does NOT use the POST data, and there is no way giving it to php. php-cgi
is made for the webserver purpose and can handle it.原因:
php
不使用 POST 数据,无法将其提供给 php。php php-cgi
是为网络服务器目的而制作的,可以处理它。
Hwo to solve the next problem : To run php-cgi
successfully you have to create a php.ini in the current directory tho, with two settings. Hwo 解决下一个问题:要成功运行
php-cgi
,您必须在当前目录中创建一个 php.ini,其中包含两个设置。 First one to allow executing it directly, second one to set the directory where the scripts are.第一个允许直接执行它,第二个设置脚本所在的目录。 If you don't set it, you will be greeted with a
404 not found
.如果你不设置它,你会收到
404 not found
的问候。
php.ini : php.ini :
cgi.force_redirect = 0
doc_root = /home/username/server-directory
Where the server-directory
folder is the folder containing the php.ini, and the php-cgi
folder.其中
server-directory
文件夹是包含 php.ini 和php-cgi
文件夹的文件夹。 Also the name of the folder is not related to the php-cgi binary, it's just bad naming that I did.此外,该文件夹的名称与 php-cgi 二进制文件无关,这只是我所做的错误命名。
If you try to recreate this, it should work perfectly now.如果您尝试重新创建它,它现在应该可以完美运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.