CGI-Web服务器接收并显示数据 《Head First Python》第九章

实现效果

提交时间的网页test_form.py将数据提交至add_timing_data.py后,后者将前者传送的关键数据显示出来。

这里写图片描述
这里写图片描述

代码

test_form.py

import yate
print(yate.start_response('text/html'))
print(yate.do_form('add_timing_data.py',['TimeValue'],text='send'))

add_timing_data.py

from __future__ import print_function

import cgi
import os
import time
import sys
import yate

print(yate.start_response('text/plain'))
addr = os.environ['REMOTE_ADDR']
host = os.environ['REMOTE_HOST']
method = os.environ['REQUEST_METHOD']
cur_time = time.asctime(time.localtime())
print(host + "," + addr + "," + cur_time + ":" + method + ":", end='\n',file=sys.stdout)
form=cgi.FieldStorage()
for each_form_item in form.keys():
   print(each_form_item + '->'+form[each_form_item].value, end='\n',file=sys.stdout)
print('OK.')

这里关键点有:

1 传送数据到CGI脚本—cgi.FieldStorage()

这在文章《cgi.FieldStorage()获取网页间提交的数据》已有详细描述。

2 查询CGI的内置环境—os库

os — Miscellaneous operating system interfaces

This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module.

os.environ

A mapping object representing the string environment. For example, environ[‘HOME’] is the pathname of your home directory (on some platforms), and is equivalent to getenv(“HOME”) in C.

This mapping is captured the first time the os module is imported, typically during Python startup as part of processing site.py. Changes to the environment made after this time are not reflected in os.environ, except for changes made by modifying os.environ directly.

If the platform supports the putenv() function, this mapping may be used to modify the environment as well as query the environment. putenv() will be called automatically when the mapping is modified.

3 负责输出格式控制的—print

print([object, …][, sep=’ ‘][, end=’\n’][, file=sys.stdout])

“Print object(s) to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.”

打印每一个对象,对象之间用间隔符sep隔开,用end结束。

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no object is given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used.

Note:

在python2中使用这种格式的print函数,需要在代码开头引入print函数

from __future__ import print_function

“This function is not normally available as a built-in since the name print is recognized as the print statement. To disable the statement and use the print() function, use this future statement at the top of your module:”

from __future__ import print_function
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值