1

I want to set up a simplistic HTML-form and process the entered data with a Pyton backend. I have composed the HTML- frontend and the Python server.

This is the website:

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

The 'server'-part looks like this:

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

When I click the submit-button, I reach the do_POST-method (I'm running the server inside a Eclipse-PyDev debugger), so the server is reachable. But I don't get any data from the form, neither in plaintext nor json or xml. Where is my mistake?

normator
  • 11
  • 3
  • change your form method to `post` in this line `
    `
    – Amin Etesamian Nov 28 '16 at 18:44
  • just corrected that, it says 'post' in my actual file. – normator Nov 28 '16 at 18:49
  • For what it is worth, when I run your example, I get **POST!** in large bold letters after clicking the submit button. – Robᵩ Nov 28 '16 at 19:00
  • I do too, but I don't seem to receive any data on the server-side! I want to process the user input – normator Nov 28 '16 at 19:07
  • set a breakpoint to figure out what `formData` is after submitting your post – Amin Etesamian Nov 28 '16 at 19:18
  • by the way, use `form_data` instead of `formData`. your naming convention is not correct for python. check this out: http://stackoverflow.com/questions/159720/what-is-the-naming-convention-in-python-for-variable-and-function-names – Amin Etesamian Nov 28 '16 at 19:20
  • "*Where is my mistake?*" -- Your mistake is in using `cgi.FieldStorage()`. That function is only useful in CGI scripts, which your program isn't. – Robᵩ Nov 28 '16 at 19:23
  • @AminEtesamian formData contains empty dicts/lists, but no data from the user input – normator Nov 28 '16 at 19:58
  • @Robᵩ The answer you linked to this post also uses cgi.* methods - which in fact is working for me – normator Nov 28 '16 at 20:06
  • @normator - Right. I don't mean that all `cgi.*` functions are wrong for you, only that `cgi.FieldStorage()` is. That example *isn't* using `cgi.FieldStorage()`. `cgi.FieldStorage()` isn't useful in this context, only in CGI scripts. On the other hand, `cgi.parse_multipart()` and `cgi.parse_qs()` *are* useful outside of CGI scripts, including in server code, as in your program. – Robᵩ Nov 28 '16 at 20:12

0 Answers0