开发者

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.

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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜