如何使用 WSGIREF 捕获 POST

发布于 2024-07-18 07:32:43 字数 740 浏览 4 评论 0原文

我正在尝试从简单的表单中捕获 POST 数据。

这是我第一次使用 WSGIREF,我似乎找不到正确的方法来做到这一点。

This is the form:
<form action="test" method="POST">
<input type="text" name="name">
<input type="submit"></form>

并且该函数显然缺少正确的信息来捕获帖子:

def app(environ, start_response):
    """starts the response for the webserver"""
    path = environ[ 'PATH_INFO']
    method = environ['REQUEST_METHOD']
    if method == 'POST':
        if path.startswith('/test'):
            start_response('200 OK',[('Content-type', 'text/html')])
            return "POST info would go here %s" % post_info
    else:
        start_response('200 OK', [('Content-type', 'text/html')])
        return form()

I am trying to catch POST data from a simple form.

This is the first time I am playing around with WSGIREF and I can't seem to find the correct way to do this.

This is the form:
<form action="test" method="POST">
<input type="text" name="name">
<input type="submit"></form>

And the function that is obviously missing the right information to catch post:

def app(environ, start_response):
    """starts the response for the webserver"""
    path = environ[ 'PATH_INFO']
    method = environ['REQUEST_METHOD']
    if method == 'POST':
        if path.startswith('/test'):
            start_response('200 OK',[('Content-type', 'text/html')])
            return "POST info would go here %s" % post_info
    else:
        start_response('200 OK', [('Content-type', 'text/html')])
        return form()

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

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

发布评论

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

评论(1

樱&纷飞 2024-07-25 07:32:43

您应该读取来自服务器的响应。

来自 nosklo 对类似问题的回答:“PEP 333

测试代码(改编自此答案) :

    警告:此代码仅用于演示目的。

    警告:尽量避免硬编码路径或文件名。

def app(environ, start_response):
    path    = environ['PATH_INFO']
    method  = environ['REQUEST_METHOD']
    if method == 'POST':
        if path.startswith('/test'):
            try:
                request_body_size = int(environ['CONTENT_LENGTH'])
                request_body = environ['wsgi.input'].read(request_body_size)
            except (TypeError, ValueError):
                request_body = "0"
            try:
                response_body = str(request_body)
            except:
                response_body = "error"
            status = '200 OK'
            headers = [('Content-type', 'text/plain')]
            start_response(status, headers)
            return [response_body]
    else:
        response_body = open('test.html').read()
        status = '200 OK'
        headers = [('Content-type', 'text/html'),
                    ('Content-Length', str(len(response_body)))]
        start_response(status, headers)
        return [response_body]

You should be reading responses from the server.

From nosklo's answer to a similar problem: "PEP 333 says you must read environ['wsgi.input']."

Tested code (adapted from this answer):

    Caveat: This code is for demonstrative purposes only.

    Warning: Try to avoid hard-coding paths or filenames.

def app(environ, start_response):
    path    = environ['PATH_INFO']
    method  = environ['REQUEST_METHOD']
    if method == 'POST':
        if path.startswith('/test'):
            try:
                request_body_size = int(environ['CONTENT_LENGTH'])
                request_body = environ['wsgi.input'].read(request_body_size)
            except (TypeError, ValueError):
                request_body = "0"
            try:
                response_body = str(request_body)
            except:
                response_body = "error"
            status = '200 OK'
            headers = [('Content-type', 'text/plain')]
            start_response(status, headers)
            return [response_body]
    else:
        response_body = open('test.html').read()
        status = '200 OK'
        headers = [('Content-type', 'text/html'),
                    ('Content-Length', str(len(response_body)))]
        start_response(status, headers)
        return [response_body]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文