简体   繁体   English

如何在 Mod_Python 中通过 POST 或 GET 发送数据?

[英]How to send data via POST or GET in Mod_Python?

With JS, i send a AJAX post request.使用 JS,我发送 AJAX 发布请求。

 $.ajax(
        {method:"POST",
        url:"https://my/website/send_data.py",
        data:JSON.stringify(data),
        contentType: 'application/json;charset=UTF-8'

On my Apache2 mod_Python server, I wish for my python file to access data .在我的 Apache2 mod_Python 服务器上,我希望我的 python 文件能够访问data How can i do this?我怎样才能做到这一点?

def index(req):
    # data = ??

PS: here is how to reproduce the problem. PS:这里是如何重现问题。 Create testjson.html :创建testjson.html

<script type="text/javascript">
xhr = new XMLHttpRequest();
xhr.open("POST", "testjson.py");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function(res) { console.log(xhr.responseText); };
xhr.send(JSON.stringify({'foo': '0', 'bar': '1'}));
</script>

and create testjson.py containing:并创建包含以下内容的testjson.py

from mod_python import apache 

def index(req):
    req.content_type = "application/json"
    req.write("hello")
    data = req.read()
    return apache.OK

Create a .htaccess containing:创建一个.htaccess包含:

AddHandler mod_python .py
PythonHandler mod_python.publisher

Here is the result:结果如下:

testjson.html:10 POST http://localhost/test_py/testjson.py 501 (Not Implemented) testjson.html:10 POST http://localhost/test_py/testjson.py 501(未实现)

在此处输入图片说明

As pointed by Grisha (mod_python's author) in a private communication, here is the reason why application/json is not supported and outputs a "HTTP 501 Not implemented" error:正如 Grisha(mod_python 的作者)在私人通信中指出的,这就是为什么不支持application/json并输出“HTTP 501 未实现”错误的原因:

https://github.com/grisha/mod_python/blob/master/lib/python/mod_python/util.py#L284 https://github.com/grisha/mod_python/blob/master/lib/python/mod_python/util.py#L284

The solution is either to modify this, or to use a regular application/x-www-form-urlencoded encoding, or to use something else than the mod_python.publisher handler.解决方案是修改它,或者使用常规的application/x-www-form-urlencoded编码,或者使用mod_python.publisher处理程序以外的其他东西。

Example with mod_python and PythonHandler mod_python.publisher : mod_pythonPythonHandler mod_python.publisher示例:

<script type="text/javascript">
var data = JSON.stringify([1, 2, 3, '&=test', "jkl", {'foo': 'bar'}]); // the data to send
xhr = new XMLHttpRequest();
xhr.open("POST", "testjson.py");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function(res) { console.log(xhr.responseText); };
xhr.send('data=' + encodeURIComponent(data));
</script>

Server-side:服务器端:

import json
from mod_python import apache 

def index(req):
    data = json.loads(req.form['data'])
    x = data[-1]['foo']
    req.write("value: " + x)

Output:输出:

value: bar价值:酒吧

Success!成功!

From Mod_python docs :来自Mod_python 文档

Client data, such as POST requests, can be read by using the request.read() function.可以使用request.read()函数读取客户端数据,例如 POST 请求。

From the Mod Python docs here .来自此处的 Mod Python 文档。

Mod Python 文档

Here you can do, for example to get the data在这里你可以做,例如获取数据

def index(req):
    data = req.read()

Additional Links : http://vandermerwe.co.nz/?p=9附加链接: http : //vandermerwe.co.nz/?p=9

As I said, from the new edition it looks like a configuration problem.正如我所说,从新版本来看,它看起来像是一个配置问题。

First, try to set PythonPath directive.首先,尝试设置PythonPath指令。

Secondly, the PythonHandler should be your file , ie:其次, PythonHandler 应该是你的file ,即:

PythonHandler testjson

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM