简体   繁体   English

HTTP POST,curl和mod_python - 如何在没有HTML FORM元素的情况下处理POST请求?

[英]HTTP POST, curl and mod_python - How to handle POST request without HTML FORM element?

I am running a mod_python server, where the index.py is designed to handle the incoming requests. 我正在运行一个mod_python服务器,其中index.py用于处理传入的请求。

In the index.py, if I design something like this to handle form and get the details in the form: 在index.py中,如果我设计这样的东西来处理表单并获取表单中的详细信息:

<form enctype="multipart/form-data" action="func" method="post">
<p>Input file:<input type="file" name="request"></p>
<p><input type="submit" name="press" value="submit"></p>

And get the details from the form like this (note the action "func" above) 并从这样的表单中获取详细信息(请注意上面的操作“func”)

def func(req):
    message = []
    f = req.form.getfirst('request')

It works perfectly fine from the browser. 它在浏览器中完美运行。 I can upload a file and its contents can retrieved at the server end. 我可以上传文件,其内容可以在服务器端检索。

However, I want to send data via curl's POST. 但是,我想通过curl的POST发送数据。 In that case, I thought, the <form> element at the server is not required to handle POST, if I can get the POST data from the request object itself. 在那种情况下,我想,如果我可以从请求对象本身获取POST数据,则服务器上的<form>元素不需要处理POST。

Suppose my request via curl is like this: 假设我通过curl的请求是这样的:

curl --data "request=data_i_am_posting" http://mymodpythonsite.com/path/

How should my mod_python request handler be designed so that I get the data I am posting. 我应该如何设计我的mod_python请求处理程序,以便获取我发布的数据。 Should I use the <form> at all? 我应该使用<form>吗?

def index(req):
    # What should I do here to get data_i_am_posting

BTW, please note that I HTTP server wont be accessed by browser at all, the clients (curl, scripts) will post data and wait for the response which will be non-html. 顺便说一句,请注意我的HTTP服务器根本不会被浏览器访问,客户端(curl,脚本)将发布数据并等待非html的响应。

First a few notes: 先说几点:

  • It looks like you're using the Publisher Handler of mod_python . 看起来你正在使用mod_pythonPublisher Handler This does a lot under the covers to map URL's to Python functions. 这有很多内容可以将URL映射到Python函数。
  • The server doesn't really know or care where its getting its data. 服务器并不真正了解或关心其获取数据的位置。 In your case the curl command is simply simulating a form POST request . 在您的情况下, curl命令只是模拟表单POST请求
  • Therefore you can handle the curl requests exactly the same as form requests. 因此,您可以处理与表单请求完全相同的curl请求。
  • You probably don't want to name your function "index" because that might add unnecessary confusion to the publisher's path->function mapping since "index.py" is an implied part of the path. 您可能不希望将函数命名为“index”,因为这可能会给发布者的路径 - >函数映射添加不必要的混淆,因为“index.py”是路径的隐含部分。 Not wrong, just confusing. 没错,只是令人困惑。

So for your curl command, you should be able to get what you want from this function in the "index.py" module: 因此,对于curl命令,您应该能够在“index.py”模块中从此函数中获得所需内容:

def path(req):
    request_data = req.form.getfirst('request')
    - -

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

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