コード
http 応答を読み取るには、Tornado IOStream.read_until_close を使用します。
from tornado import ioloop
from tornado import iostream
import socket
class Client(object):
def __init__(self, domen, uri):
self.uri = uri
self.domen = domen
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
self.stream = iostream.IOStream(s)
def open(self):
self.stream.connect((self.domen, 80), self.send_request)
def on_chunk_read(self, chunk):
print "chunk\n", chunk
def on_close(self, res):
print "res\n", res
self.stream.close()
ioloop.IOLoop.instance().stop()
def send_request(self):
self.stream.write(
"GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (self.uri, self.domen))
self.stream.read_until_close(self.on_close, self.on_chunk_read)
client = Client("www.google.com", "/")
client.open()
ioloop.IOLoop.instance().start()
出力
res
HTTP/1.0 302 Found
Location: http://www.google.ru/
...
<HTML>
...
</HTML>
ドキュメンテーションtornado.iostream
read_until_close(callback, streaming_callback=None)
Reads all data from the socket until it is closed.
If a streaming_callback is given, it will be called with chunks of data as they
become available, and the argument to the final callback will be empty.
Subject to max_buffer_size limit from IOStream constructor if a
streaming_callback is not used.
結論
コード 'print "chunk\n", chunk' が呼び出されていません。コールバックが呼び出されましたが、streaming_callback が呼び出されていません。
バグですか?