Hi,
The testing system I'm using for the project I'm trying to convert to
Tornado is using urllib2, and it's not feasible for me to change it.
The issue I'm having is that when I return chunked responses httplib
is raising a "IncompleteRead" exception.
I've managed to get a minimal working application that reproduces the
error. I tried the client on python 2.7 and 3.2 and got two different
errors. The server is running in 2.7 in both cases (as that's the
version I'll be running in production). It's also the case that if I
pass a header to write out a ton of spaces before I do the callback
everything works OK.
Code for both clients, errors, and the application below. Any help
would be appreciated!
-Jeff
====================
# server.py
import logging
import tornado.ioloop
import tornado.options
import tornado.web
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
def request_callback(async=True):
self.flush()
self.finish()
# if header is passed, write out 500k of "A"s
if 'Magic_urllib_fix' in self.request.headers:
self.write('A' * 500000)
self.flush()
# write first batch
self.write('B' * 12083)
self.flush()
tornado.ioloop.IOLoop.instance().add_callback(request_callback)
@tornado.web.asynchronous
def post(self):
return self.get()
application = tornado.web.Application([
(r"/.*", MainHandler),
], debug=True)
if __name__ == "__main__":
import sys
tornado.options.parse_command_line()
logging.info("starting torando web server")
application.listen(sys.argv[1])
tornado.ioloop.IOLoop.instance().start()
==============
# python3.2 client (python 2.7 server)
import urllib.request
def call(with_fix):
op = urllib.request.build_opener()
url = '
http://local.shopwiki.com:8000/'
if with_fix:
op.addheaders = [('MAGIC_URLLIB_FIX', 't')]
op.open(url).read()
call(with_fix=True)
call(with_fix=False)
# Output:
# Traceback (most recent call last):
# File "/usr/local/Cellar/python3/3.2/lib/python3.2/http/client.py",
line 529, in _read_chunked
# chunk_left = int(line, 16)
# ValueError: invalid literal for int() with base 16: ''
#
# During handling of the above exception, another exception occurred:
#
# Traceback (most recent call last):
# File "curl.py", line 10, in <module>
# call(with_fix=True)
# File "curl.py", line 8, in call
# op.open(url).read()
# File "/usr/local/Cellar/python3/3.2/lib/python3.2/http/client.py",
line 489, in read
# return self._read_chunked(amt)
# File "/usr/local/Cellar/python3/3.2/lib/python3.2/http/client.py",
line 534, in _read_chunked
# raise IncompleteRead(b''.join(value))
# http.client.IncompleteRead: IncompleteRead(512083 bytes read)
============================
# python 2.7 client (python 2.7 server)
import urllib2
def call(with_fix):
op = urllib2.build_opener()
url = '
http://local.shopwiki.com:8000/'
if with_fix:
op.addheaders = [('MAGIC_URLLIB_FIX', 't')]
op.open(url).read()
call(with_fix=True)
call(with_fix=False)
# Output:
# Traceback (most recent call last):
# File "curl.py", line 11, in <module>
# call(with_fix=False)
# File "curl.py", line 8, in call
# op.open(url).read()
# File "/usr/local/Cellar/python/2.7.1/lib/python2.7/socket.py",
line 351, in read
# data = self._sock.recv(rbufsize)
# File "/usr/local/Cellar/python/2.7.1/lib/python2.7/httplib.py",
line 533, in read
# return self._read_chunked(amt)
# File "/usr/local/Cellar/python/2.7.1/lib/python2.7/httplib.py",
line 576, in _read_chunked
# raise IncompleteRead(''.join(value))
# httplib.IncompleteRead: IncompleteRead(3891 bytes read)