отправка кусков данных с помощью webob - PullRequest
2 голосов
/ 29 февраля 2012

Я пытался написать простую программу сервер-клиент, используя 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
...