表单提交后的空请求正文(Python服务器,HTML表单)

时间:2017-12-10 13:01:54

标签: python html web server request

我目前正在尝试为网站用户构建基于Web的身份验证系统。这是我的HTML登录表单:

<form action="/auth" method="post" class="pure-form pure-form-stacked">
    <fieldset>
        <legend>Login</legend>
        <label for="username">Username</label>
        <input id="username" type="username" placeholder="Username">

        <label for="password">Password</label>
        <input id="password" type="password" placeholder="Password">

        <button type="submit" class="pure-button pure-button-primary">
            Login
        </button>
    </fieldset>
</form>

通常我希望它向服务器发送请求,并在请求正文中包含两个凭据,因此我的/ auth路由可以检查它们。但是我使用的Post方法的解析器似乎并没有在请求体内找到任何东西。这是我的请求解析器(它检查后参数的部分:

class Request:
    """
    http request data.
    """

    def __init__(self):
        self.headers = {}
        self.method = None
        self.protocol = None
        self.resource = None
        self.path = None
        self.params = {}
        self.origin = None  # will be set from server

    def parse(self, conn):
        """Parses an http-Request and return a dictionary with process_request line values and headers."""
        self.headers = {}

        # read process_request line
        request_line = conn.readline().decode('utf-8').strip()
        log(1, "Request-Line: %s" % request_line)
        if not request_line:  # rfc says "server SHOULD ignore blank request lines"
            return None

        # parse process_request line
        try:
            self.method, self.resource, self.protocol = request_line.split(" ")
        except ValueError:
            raise StopProcessing(400, "Bad request-line: %s\n" % request_line)

        # parse resource to path and params
        # extract GET parameters
        from urllib.parse import urlparse, parse_qs # analyse urls and parse query strings
        requrl = urlparse(self.resource)
        self.path = requrl.path
        self.params.update(parse_qs(requrl.query))

        # read and parse Request-Headers
        while True:
            header_line = conn.readline().decode('utf-8').strip()
            if not header_line:
                break
            log(2, "Header-Line: " + header_line)
            (headerfield, headervalue) = header_line.split(":", 1)
            self.headers[headerfield.strip()] = headervalue.strip()

        # read cookies
        if 'Cookie' in self.headers:
            log(2, "Cookie ist: %s" % self.headers['Cookie'])
            self.cookies = Cookie.parse(self.headers['Cookie'])
        else:
            self.cookies = {}

        # parse POST parameters
        log(1,"Methode %s" % self.method)
        if self.method == 'POST' or self.method == 'post':
            postbody = conn.read(int(self.headers['Content-Length'])).decode('utf-8')
            self.params.update(parse_qs(postbody))

        # all parameter values are lists
        # replace lists by the only element if there is only one
        for key in self.params:
            if len(self.params[key])==1:
                self.params[key] = self.params[key][0]

        return self.headers

我在代码的关键段落中添加了一些跟踪输出,我发现我的解析器总是返回一个空字典{}。 我错过了什么? 此框架中没有使用任何附加组件。它是纯Python。

1 个答案:

答案 0 :(得分:1)

您需要在表单输入上设置name属性,id无法识别表单上的输入。