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

发布于 2024-10-06 09:01:51 字数 975 浏览 9 评论 0原文

我正在运行 mod_python 服务器,其中 index.py 旨在处理传入请求。

在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>

并从这样的表单中获取详细信息(注意上面的操作“func”)

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

它在浏览器中工作得很好。我可以上传一个文件,并且可以在服务器端检索其内容。

但是,我想通过curl的POST发送数据。在这种情况下,我想,

如果我可以从请求对象本身获取 POST 数据,则服务器上的元素不需要处理 POST。

假设我通过curl 的请求是这样的:

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

我的mod_python 请求处理程序应该如何设计,以便我获取我发布的数据。 我应该使用

吗?根本吗?

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

顺便说一句,请注意,浏览器根本不会访问 HTTP 服务器,客户端(curl、脚本)将发布数据并等待非 html 的响应。

I am running a mod_python server, where the index.py is designed to handle the incoming requests.

In the index.py, if I design something like this to handle form and get the details in the form:

<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)

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. 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.

Suppose my request via curl is like this:

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.
Should I use the <form> at all?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青柠芒果 2024-10-13 09:01:51

首先一些注意事项:

  • 看起来您正在使用 Publisher Handler mod_python。这在幕后做了很多工作,将 URL 映射到 Python 函数。
  • 服务器并不真正知道或关心它从哪里获取数据。在您的情况下, curl 命令只是模拟表单 POST 请求
  • 因此,您可以像处理表单请求一样处理curl 请求。
  • 您可能不想将函数命名为“index”,因为这可能会给发布者的路径->函数映射增加不必要的混乱,因为“index.py”是路径的隐含部分。没有错,只是令人困惑。

因此,对于你的curl命令,你应该能够从“index.py”模块中的这个函数中获得你想要的东西:

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

First a few notes:

  • It looks like you're using the Publisher Handler of mod_python. This does a lot under the covers to map URL's to Python functions.
  • 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.
  • Therefore you can handle the curl requests exactly the same as form requests.
  • 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. 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:

def path(req):
    request_data = req.form.getfirst('request')
    -
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文