用Python接收http-post

时间:2016-11-28 18:42:24

标签: python html forms client-server

我想设置一个简单的HTML表单,并使用Pyton后端处理输入的数据。 我编写了HTML-frontend和Python服务器。

这是网站:

<html>
<head>
<style type="text/css">

  input:required:invalid, input:focus:invalid {
    background-image: url(invalid.png);
    background-position: right top;
    background-repeat: no-repeat;
  }
  input:required:valid {
    background-image: url(valid.png);
    background-position: right top;
    background-repeat: no-repeat;
  }
</head>
</style>
    <body>
        <form action="http://localhost:8080/" method="post" name="myForm" >
        <label>Name 1: </label> <input type="text" name="name1" value="Person 1" required/> <input type="number" size="6" name="exp1" min="0" max="9999" value="20" step="0.01" required/><br>
        <label>Name 2: </label> <input type="text" name="name2" value="Person 2" required/> <input type="number" size="6" name="exp2" min="0" max="9999" value="60" step="0.01" required/><br>
        <label>Name 3: </label> <input type="text" name="name3" value="Person 3" required/> <input type="number" size="6" name="exp3" min="0" max="9999" value="30" step="0.01" required/><br>
        <input type="submit" value="Submit"/> <br><br><br>
        </form>
    </body>
</html>

'服务器'部分看起来像这样:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import cgi

class Server(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self.wfile.write("<html><body><h1>GET!</h1></body></html>")

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        # No posted data received?!
        formData = cgi.FieldStorage()
        # this leads to an error: 
        #form = web.input(name="myForm")
        self.wfile.write("<html><body><h1>POST!</h1></body></html>")

def run(server_class=HTTPServer, handler_class=Server, port=8080):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print 'Starting httpd...'
    httpd.serve_forever()

if __name__ == "__main__":
    run()

当我单击提交按钮时,我到达do_POST方法(我在Eclipse-PyDev调试器中运行服务器),因此可以访问服务器。但是我没有从表单中获得任何数据,无论是纯文本还是json或xml。 我的错误在哪里?

0 个答案:

没有答案