使用webob发送数据块

时间:2012-02-29 10:09:34

标签: wsgi webob

我尝试使用webob编写一个简单的服务器客户端程序。 1.客户端使用'Transfer-Encoding'发送数据,'chunked' 2.然后在服务器端打印接收的数据。

Server.py正确接收数据。 但是,我从webob收到了一堆错误信息。

然而,任何人都会告诉我发生了什么或只是 给一个简单的指导线,用webob编写这样一个简单的程序(发送块)。 谢谢!

代码和错误如下:

server.py

from webob import Request
from webob import Response
class ChunkApp(object):
def __init__(self):
    pass

def __call__(self, environ, start_response):
    req = Request(environ)

    for buf in self.chunkreadable(req.body_file, 65535):
        print buf

    resp = Response('chunk received')
    return resp(environ, start_response)

def chunkreadable(self, iter, chunk_size=65536):
    return self.chunkiter(iter, chunk_size) if \
                hasattr(iter, 'read') else iter

def chunkiter(self, fp, chunk_size=65536):
    while True:
        chunk = fp.read(chunk_size)
        if chunk:
            yield chunk
        else:
            break
if __name__ == '__main__':
    app = ChunkApp()
    from wsgiref.simple_server import make_server
    httpd = make_server('localhost', 8080, app)
    print 'Serving on http://localhost:%s' % '8080'
    try:
       httpd.serve_forever()
    except KeyboardInterrupt:
        print '^C'

client.py

 from httplib import HTTPConnection
 conn = HTTPConnection("localhost:8080")
 conn.putrequest('PUT', "/")
 body = "1234567890"

 conn.putheader('Transfer-Encoding', 'chunked')
 conn.endheaders()
 for chunk in body:
 #print '%x\r\n%s\r\n' % (len(chunk), chunk)
    conn.send('%x\r\n%s\r\n' % (len(chunk), chunk))
 conn.send('0\r\n\r\n')

错误

Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
    self.finish_response()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
    self.write(data)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 210, in write
    self.send_headers()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 268, in send_headers
    self.send_preamble()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 192, in send_preamble
    'Date: %s\r\n' % format_date_time(time.time())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 324, in write
    self.flush()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52575)
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 310, in process_request
    self.finish_request(request, client_address)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 641, in __init__
    self.finish()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 694, in finish
    self.wfile.flush()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

0 个答案:

没有答案
相关问题