`
phyeas
  • 浏览: 164201 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

初试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?

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

于是上google去百度了一下,于是,就找到了这么一篇(http://www.velocityreviews.com/forums/t649886-socket-send-help.html ),上面说到:

> connection.send('echo -> ' + data)

That's fine for Python 2.6, but you must use b'echo -> ' with 3.0

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

以下就是我找到的官方的例子(http://docs.python.org/3.0/library/wsgiref.html ):

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

有要去google上百度一下。找到这么一篇帖子(http://www.python-forum.org/pythonforum/viewtopic.php?f=5&p=56616 ),最后的回帖里写到:

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吧,各位路过的大侠侠女们有类似情况并解决了的,强烈要求留下解决方法或联系方式。

 

谢谢先了。

分享到:
评论
1 楼 rockone 2014-12-17  
start_response()中的status和headers参数用“str”类型,return中的参数编码成“bytes”类型,同时在headers中注明charset类型。看看行不?

相关推荐

Global site tag (gtag.js) - Google Analytics