web python -- WSGI接口响应
2015-06-22 22:38
736 查看
将上一个例子的返回:
return [response_body]
改为:
return response_body
再次运行会发现速度变慢了。这是因此服务器对发送过来的字符串是按单个字节进行迭代的,所以最好对返回的字符串用一个可迭代对象包装一下。
如果返回的这个可迭代对象生成多个字符串,那么正文的长度即为这些字符串长度的总和。
接下来看一个例子:
#! /usr/bin/env python
from wsgiref.simple_server import make_server
def application(environ, start_response):
response_body = ['%s: %s' % (key, value)
for key, value in sorted(environ.items())]
response_body = '\n'.join(response_body)
# Response_body has now more than one string
response_body = ['The Beggining\n',
'*' * 30 + '\n',
response_body,
'\n' + '*' * 30 ,
'\nThe End']
# So the content-lenght is the sum of all string's lengths
content_length = 0
for s in response_body:
content_length += len(s)
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(content_length))]
start_response(status, response_headers)
return response_body
httpd = make_server('localhost', 8051, application)
httpd.handle_request()
转载自:http://www.xefan.com/archives/84010.html
return [response_body]
改为:
return response_body
再次运行会发现速度变慢了。这是因此服务器对发送过来的字符串是按单个字节进行迭代的,所以最好对返回的字符串用一个可迭代对象包装一下。
如果返回的这个可迭代对象生成多个字符串,那么正文的长度即为这些字符串长度的总和。
接下来看一个例子:
#! /usr/bin/env python
from wsgiref.simple_server import make_server
def application(environ, start_response):
response_body = ['%s: %s' % (key, value)
for key, value in sorted(environ.items())]
response_body = '\n'.join(response_body)
# Response_body has now more than one string
response_body = ['The Beggining\n',
'*' * 30 + '\n',
response_body,
'\n' + '*' * 30 ,
'\nThe End']
# So the content-lenght is the sum of all string's lengths
content_length = 0
for s in response_body:
content_length += len(s)
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(content_length))]
start_response(status, response_headers)
return response_body
httpd = make_server('localhost', 8051, application)
httpd.handle_request()
转载自:http://www.xefan.com/archives/84010.html
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法
- Python实现的基于ADB的Android远程工具