Last active
December 16, 2015 19:59
-
-
Save wynemo/5488772 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import tornado.ioloop | |
import tornado.iostream | |
def test(): | |
def on_close(data): | |
print len(data), 'test on_close' | |
def on_data(data): | |
print len(data), 'test on_data' | |
def on_connected(): | |
data = 'GET / HTTP/1.1\r\n' | |
data += 'Host:dev.twitter.com\r\n\r\n' | |
stream.write(data) | |
stream.read_until_close(on_close, on_data) | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) | |
stream = tornado.iostream.IOStream(s) | |
stream.connect(('dev.twitter.com', 80), on_connected) | |
def test1(): | |
def on_close(data): | |
print len(data), 'test1 on_close' | |
def on_data(data): | |
print len(data), 'test1 on_data' | |
def on_connected(): | |
data = 'GET / HTTP/1.1\r\n' | |
data += 'Host:twitter.com\r\n\r\n' | |
stream.write(data) | |
stream.read_until_close(on_close, on_data) | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) | |
stream = tornado.iostream.IOStream(s) | |
stream.connect(('twitter.com', 80), on_connected) | |
def main(): | |
tornado.ioloop.IOLoop.instance().add_callback(test) | |
tornado.ioloop.IOLoop.instance().add_callback(test1) | |
tornado.ioloop.IOLoop.instance().start() | |
if __name__ == '__main__': | |
main() | |
#using tornado 3.0.1 | |
# result: | |
# 533 test on_close | |
# 249 test1 on_data | |
# 0 test1 on_close | |
# according to the documents, http://www.tornadoweb.org/en/stable/_modules/tornado/iostream.html#BaseIOStream.read_until_close | |
#if streaming_callback is given, the argument to final callback is empty | |
# test1 behave as the document says | |
# but not test | |
# so I'm confused, is there something wrong? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment