Parse http GET and POST parameters from BaseHTTPHandler?

▼魔方 西西 提交于 2019-11-28 05:17:40

You could try the Werkzeug modules, the base Werkzeug library isn't too large and if needed you can simply extract this bit of code and you're done.

The url_decode method returns a MultiDict and has encoding support :)

As opposed to the urlparse.parse_qs method the Werkzeug version takes care of:

  • encoding
  • multiple values
  • sort order

If you have no need for these (or in the case of encoding, use Python 3) than feel free to use the built-in solutions.

zag

You may want to use urllib.parse:

>>> from urllib.parse import urlparse, parse_qs
>>> url = 'http://example.com/?foo=bar&one=1'
>>> parse_qs(urlparse(url).query)
{'foo': ['bar'], 'one': ['1']}

For Python 2, the module is named urlparse instead of url.parse.

Better solution to an old question:

def do_POST(self):
    length = int(self.headers.getheader('content-length'))
    field_data = self.rfile.read(length)
    fields = urlparse.parse_qs(field_data)

This will pull urlencoded POST data from the document content and parse it a dict with proper urldecoding

Have you investigated using libraries like CherryPy? They provide a much quicker path to handling these things than BaseHTTPServer.

Basic HTTP request parameters support is provided in the CGI module. The recommended mechanism to handle form data is the cgi.FieldStorage class.

To get at submitted form data, it’s best to use the FieldStorage class. The other classes defined in this module are provided mostly for backward compatibility. Instantiate it exactly once, without arguments. This reads the form contents from standard input or the environment (depending on the value of various environment variables set according to the CGI standard). Since it may consume standard input, it should be instantiated only once.

The FieldStorage instance can be indexed like a Python dictionary. It allows membership testing with the in operator, and also supports the standard dictionary method keys() and the built-in function len(). Form fields containing empty strings are ignored and do not appear in the dictionary; to keep such values, provide a true value for the optional keep_blank_values keyword parameter when creating the FieldStorage instance.

For instance, the following code (which assumes that the Content-Type header and blank line have already been printed) checks that the fields name and addr are both set to a non-empty string:

form = cgi.FieldStorage()
if "name" not in form or "addr" not in form:
    print "<H1>Error</H1>"
    print "Please fill in the name and addr fields."
    return
print "<p>name:", form["name"].value
print "<p>addr:", form["addr"].value
#...further form processing here...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!