python3.7怎么安装wsgiref_初试Python3.0 wsgiref遇到好些问题..

今天打算尝试一下python3.0。没想到刚开始就受挫。测试代码如下:

from wsgiref.simple_server import make_server

def hello_world_app(env,start_response):

start_response("200 OK",[("Content-type","text/plain;charset=utf-8")])

return ["Hello World"]

httpd=make_server('',8080,hello_world_app)

httpd.handle_request()

这是最最最简单的Hello World。。可是没想到啊没想到。。。

遇到问题一:

Exception happened during processing of request from ('127.0.0.1', 1199)

Traceback (most recent call last):

File "E:\phy\python3k\lib\socketserver.py", line 281, in _handle_request_noblo

ck

self.process_request(request, client_address)

File "E:\phy\python3k\lib\socketserver.py", line 307, in process_request

self.finish_request(request, client_address)

File "E:\phy\python3k\lib\socketserver.py", line 320, in finish_request

self.RequestHandlerClass(request, client_address, self)

File "E:\phy\python3k\lib\socketserver.py", line 614, in __init__

self.handle()

File "E:\phy\python3k\lib\wsgiref\simple_server.py", line 139, in handle

self.rfile, self.wfile, self.get_stderr(), self.get_environ()

File "E:\phy\python3k\lib\wsgiref\simple_server.py", line 117, in get_environ

k,v = h.split(':',1)

ValueError: need more than 1 value to unpack

说是split的时候错了,我查看了一下源码,没发现什么不对劲,但是将h输出一看,原来h中没有包含':',导致split失败。源码是这么写的:

def get_environ(self):

# other codes....

for h in self.headers:

k,v = h.split(':',1)

k=k.replace('-','_').upper(); v=v.strip()

if k in env:

continue # skip content length, type,etc.

if 'HTTP_'+k in env:

env['HTTP_'+k] += ','+v # comma-separate multiple headers

else:

env['HTTP_'+k] = v

return env

输出了一下,在客户端提交的headers里的东西几乎全部都不带':'符号,不知道是什么原因,可能和浏览器有关,我用的是IE7,先不管,让程序运行起来再说,于是我在代码上改成

for h in self.headers:

if ':' not in h:

h=h+":"

重新运行Hello World程序。

omg,又有错误。。。。。

遇到问题二:

Traceback (most recent call last):

File "E:\phy\python3k\lib\wsgiref\handlers.py", line 75, in run

self.finish_response()

File "E:\phy\python3k\lib\wsgiref\handlers.py", line 116, in finish_response

self.write(data)

File "E:\phy\python3k\lib\wsgiref\handlers.py", line 200, in write

self.send_headers()

File "E:\phy\python3k\lib\wsgiref\handlers.py", line 256, in send_headers

self.send_preamble()

File "E:\phy\python3k\lib\wsgiref\handlers.py", line 179, in send_preamble

self._write('HTTP/%s %s\r\n' % (self.http_version,self.status))

File "E:\phy\python3k\lib\wsgiref\handlers.py", line 387, in _write

self.stdout.write(data)

File "E:\phy\python3k\lib\socket.py", line 219, in write

return self._sock.send(b)

TypeError: send() argument 1 must be string or buffer, not str

观察了一下,这是在向客户端发送headers的时候发生的错误。说什么str不是string或buffer?

看了半天没找到引发错误的代码,能力有限。

“> connection.send('echo -> ' + data)That's fine for Python 2.6, but you must use b'echo -> ' with 3.0

什么!还要加b前缀?。。晕死,好吧,我上python官方网站看看官方推荐的做法。

from wsgiref.simple_server import make_server

# Every WSGI application must have an application object - a callable

# object that accepts two arguments. For that purpose, we're going to

# use a function (note that you're not limited to a function, you can

# use a class for example). The first argument passed to the function

# is a dictionary containing CGI-style envrironment variables and the

# second variable is the callable object (see PEP333)

def hello_world_app(environ, start_response):

status = b'200 OK' # HTTP Status

headers = [(b'Content-type', b'text/plain; charset=utf-8')] # HTTP Headers

start_response(status, headers)

# The returned object is going to be printed

return [b"Hello World"]

httpd = make_server('', 8000, hello_world_app)

print("Serving on port 8000...")

# Serve until process is killed

httpd.serve_forever()

我将这段代码复制到一个py文件中,运行,错误更多。。

Exception happened during processing of request from ('127.0.0.1', 1350)

Traceback (most recent call last):

File "E:\phy\python3k\lib\wsgiref\handlers.py", line 74, in run

self.result = application(self.environ, self.start_response)

File "demo1.py", line 12, in hello_world_app

start_response(status, headers)

File "E:\phy\python3k\lib\wsgiref\handlers.py", line 160, in start_response

assert type(status) is str,"Status must be a string"

AssertionError: Status must be a string

这是其中的一段,说状态码,也就是发送到客户端headers里的状态码必须为string。其他的错误中包括刚刚的TypeError: send() argument 1 must be string or buffer, not str

突然想起好像安装python的目录里就有例子,看了下simple_server的源码。里面是这样写的:

def demo_app(environ,start_response):

from io import StringIO

stdout = StringIO()

print("Hello world!", file=stdout)

print(file=stdout)

h = environ.items(); h.sort()

for k,v in h:

print(k,'=',repr(v), file=stdout)

start_response("200 OK", [('Content-Type','text/plain')])

return [stdout.getvalue()]

omg的。没想到啊没想到,竟也是错的。还是TypeError: send() argument 1 must be string or buffer, not str

This problem exists in Python 3 indeed. But the resulting error message is a complaint about invalid number of arguments. I wrote a bug report for that some time ago.

难道是传说中的bug?????

终于实在没辙了。。还是用回2.x吧,各位路过的大侠侠女们有类似情况并解决了的,强烈要求留下解决方法或联系方式。

谢谢先了。

问题补充:

不是环境没搭好的原因,请看我发的帖子:http://www.iteye.com/topic/354606

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值