cgi.FieldStorage()获取网页间提交的数据《Head First Python》第七章
2017-01-13 22:27
651 查看
To get at submitted form data, it’s best to use the
For instance, the following code (which assumes that the Content-Type header and blank line have already been printed) checks that the fields
Here the fields, accessed through
The value attribute of the instance yields the string value of the field. The
如下图所示为第七章中,Generate_list.py向Generating_timing_data.py传递“which_athlete”对应的值的方法:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/26/a5d9181cdff0471b72a1838b15526574)
Similarly, in this situation,
For example, this code concatenates any number of username fields, separated by commas:
You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute:
If an error is encountered when obtaining the contents of an uploaded file (for example, when the user interrupts the form submission by clicking on a Back or Cancel button) the done attribute of the object for the field will be set to the value -1.
The file upload draft standard entertains the possibility of uploading multiple files from one field (using a recursive multipart/* encoding). When this occurs, the item will be a dictionary-like FieldStorage item. This can be determined by testing its type attribute, which should be multipart/form-data (or perhaps another MIME type matching multipart/*). In this case, it can be iterated over recursively just like the top-level form object.
When a form is submitted in the “old” format (as the query string or as a single data part of type application/x-www-form-urlencoded), the items will actually be instances of the class MiniFieldStorage. In this case, the list, file, and filename attributes are always None.
A form submitted via POST that also has a query string will contain both FieldStorage and MiniFieldStorage items.
FieldStorage class. The other classes defined in this module are provided mostly for backward compatibility. Instantiate it exactly once, without arguments.
FieldStoragereads 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.
1.1 要获取的“键”是唯一的
TheFieldStorageinstance 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
nameand
addrare 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...
Here the fields, accessed through
form[key], are themselves instances of FieldStorage (or MiniFieldStorage, depending on the form encoding).
The value attribute of the instance yields the string value of the field. The
getvalue()method returns this string value directly; it also accepts an optional second argument as a default to return if the requested key is not present.
如下图所示为第七章中,Generate_list.py向Generating_timing_data.py传递“which_athlete”对应的值的方法:
1.2 要获取的“键”不唯一
If the submitted form data contains more than one field with the same name, the object retrieved by form[key] is not a FieldStorage or MiniFieldStorage instance but a list of such instances.Similarly, in this situation,
form.getvalue(key)would return a list of strings. If you expect this possibility (when your HTML form contains multiple fields with the same name), use the
getlist()function, which always returns a list of values (so that you do not need to special-case the single item case).
For example, this code concatenates any number of username fields, separated by commas:
value = form.getlist("username") usernames = ",".join(value)
1.3 要获取字段代表是要上传的文件
If a field represents an uploaded file, accessing the value via the value attribute or thegetvalue()method reads the entire file in memory as a string. This may not be what you want.
You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute:
fileitem = form["userfile"] if fileitem.file: # It's an uploaded file; count lines linecount = 0 while 1: line = fileitem.file.readline() if not line: break linecount = linecount + 1
If an error is encountered when obtaining the contents of an uploaded file (for example, when the user interrupts the form submission by clicking on a Back or Cancel button) the done attribute of the object for the field will be set to the value -1.
The file upload draft standard entertains the possibility of uploading multiple files from one field (using a recursive multipart/* encoding). When this occurs, the item will be a dictionary-like FieldStorage item. This can be determined by testing its type attribute, which should be multipart/form-data (or perhaps another MIME type matching multipart/*). In this case, it can be iterated over recursively just like the top-level form object.
When a form is submitted in the “old” format (as the query string or as a single data part of type application/x-www-form-urlencoded), the items will actually be instances of the class MiniFieldStorage. In this case, the list, file, and filename attributes are always None.
A form submitted via POST that also has a query string will contain both FieldStorage and MiniFieldStorage items.
相关文章推荐
- web框架之cgi.FieldStorage()与数据提交
- Python开发实战教程(8)-向网页提交获取数据
- Python下CGIweb服务器实现(CGIHTTPServer) 《Head First Python》第七章
- CGI-Web服务器接收并显示数据 《Head First Python》第九章
- Head First Python(管理你的数据)
- bash-shell做CGI,用于获取HTTP POST提交的数据
- Head First Python 学习笔记-Chapter4:持久化--将数据写入文件
- 《Head First Programming》---python 2_文本数据
- htmlunit 自动化提交/获取网页数据,自动化测试
- 使用python获取网页中指定数据
- 《head first python》——理解数据:列表排序与集合
- 《Head First Python》笔记 第六章 定制数据对象
- 《Head First Python》笔记 第七章 Web开发
- 表单提交数据的方式 怎么在服务器获取 服务器控件 html服务器控件 提交服务器之前的验证 前台网页编写c#代码
- Python 爬虫学习2 向网页提交数据
- python爬虫(二):向网页提交数据
- C++使用POST方法向网页提交数据-----C++发送HTTP数据获取Google天气预报
- head_first_python对CGI的探讨
- [Head First Python]5. 推导数据:处理数据
- [Head First Python]6. 定制数据对象:打包代码与数据