Python Forum

Full Version: Getting form data ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I get input data from a form via post without using any framework ?
using the example below I get the output :

ws_body = int(environ['CONTENT_LENGTH'])
ws_input = environ['wsgi.input'].read(ws_body)
ws_print = ws_input

status = '200 OK'
headers = [('Content-type', 'text/html; charset=utf-8')]
ws_output(status, headers)
return [str(ws_print).encode('utf-8')]

output-> b'' ?
As i mention before look at how other has done this.
As these task task can be hard to figure out at low WSGI level using the specification Doc in PEP 333 or PEP 3333

Werkzeug is a thin layer above WSGI,and stuff Flask is build on.
Form Data Parsing werkzeug
Quote:When does Werkzeug Parse?

Werkzeug parses the incoming data under the following situations:
  • you access either form, files, or stream and the request method was POST or PUT.
  • if you call parse_form_data().

In there example you see raw data that comes in from a form.
data = '--foo\r\nContent-Disposition: form-data; name="test"\r\n'\
'\r\nHello World!\r\n--foo--'
If i make own parser as a quick test,not looking how they have made the parser.
>>> import re 
>>> 
>>> r = re.search(r"name=\"\w+\"(.*?)--", data, re.DOTALL)
>>> r.group(1).strip()
'Hello World!'
Regex is common tool to use when parsing other Parsing In Python: Tools And Libraries.
PyParsing -- A Python Parsing Module.
(Nov-07-2019, 02:03 PM)snippsat Wrote: [ -> ]As i mention before look at at how other has done this. As these task task can be hard to figure out at low WSGI level using the specification Doc in PEP 333 or PEP 3333 Form Data Parsing werkzeug
Quote:When does Werkzeug Parse? Werkzeug parses the incoming data under the following situations:
  • you access either form, files, or stream and the request method was POST or PUT.
  • if you call parse_form_data().
In there example you see raw data that comes in from a form.
data = '--foo\r\nContent-Disposition: form-data; name="test"\r\n'\ '\r\nHello World!\r\n--foo--'
. If i make own parser as a quick test,not looking how they have made the parser.
>>> import re >>> >>> r = re.search(r"name=\"\w+\"(.*?)--", data, re.DOTALL) >>> r.group(1).strip() 'Hello World!'
Regex is common tool to use when parsing other Parsing In Python: Tools And Libraries. PyParsing -- A Python Parsing Module.

Too bad this is a puzzle it would be interesting for python language itself to provide a native function within wsgiref to handle this somewhat similar issue as the request_uri () function for accessing url data. Could you think of something like request_form () or request_input () to gain access to data sent by forms?