Created
January 17, 2014 03:31
Revisions
-
phuslu revised this gist
Oct 2, 2011 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,8 @@ import sys, os, re import logging from tornado.ioloop import IOLoop from tornado.iostream import IOStream from tornado.netutil import TCPServer logging.basicConfig(level=logging.INFO, format='%(levelname)s - - %(asctime)s %(message)s', datefmt='[%d/%b/%Y %H:%M:%S]') @@ -47,7 +48,7 @@ def _on_close(self): def main(): echo_server = EchoServer() echo_server.listen(8888) IOLoop.instance().start() if __name__ == '__main__': main() -
phuslu revised this gist
Sep 22, 2011 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -28,17 +28,17 @@ def __init__(self, stream, address): self.address = address self.stream_set.add(self.stream) self.stream.set_close_callback(self._on_close) self.stream.read_until('\n', self._on_read_line) def _on_read_line(self, data): logging.info('read a new line from %s', self.address) for stream in self.stream_set: stream.write(data, self._on_write_complete) def _on_write_complete(self): logging.info('write a line to %s', self.address) if not self.stream.reading(): self.stream.read_until('\n', self._on_read_line) def _on_close(self): logging.info('client quit %s', self.address) @@ -50,4 +50,4 @@ def main(): ioloop.IOLoop.instance().start() if __name__ == '__main__': main() -
phuslu revised this gist
Sep 22, 2011 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,15 @@ logging.basicConfig(level=logging.INFO, format='%(levelname)s - - %(asctime)s %(message)s', datefmt='[%d/%b/%Y %H:%M:%S]') class EchoServer(TCPServer): def __init__(self, io_loop=None, ssl_options=None, **kwargs): logging.info('a echo tcp server is started') TCPServer.__init__(self, io_loop=io_loop, ssl_options=ssl_options, **kwargs) def handle_stream(self, stream, address): EchoConnection(stream, address) class EchoConnection(object): stream_set = set([]) @@ -35,15 +44,6 @@ def _on_close(self): logging.info('client quit %s', self.address) self.stream_set.remove(self.stream) def main(): echo_server = EchoServer() echo_server.listen(8888) -
phuslu revised this gist
Sep 21, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ def __init__(self, stream, address): self.stream = stream self.address = address self.stream_set.add(self.stream) self.stream.set_close_callback(self._on_close) self.stream.read_until('\n', self._on_readline) def _on_readline(self, data): @@ -31,7 +31,7 @@ def _on_write(self): if not self.stream.reading(): self.stream.read_until('\n', self._on_readline) def _on_close(self): logging.info('client quit %s', self.address) self.stream_set.remove(self.stream) -
phuslu renamed this gist
Sep 21, 2011 . 1 changed file with 20 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,41 +4,49 @@ import sys, os, re import logging from tornado import iostream, ioloop from tornado.netutil import TCPServer logging.basicConfig(level=logging.INFO, format='%(levelname)s - - %(asctime)s %(message)s', datefmt='[%d/%b/%Y %H:%M:%S]') class EchoConnection(object): stream_set = set([]) def __init__(self, stream, address): logging.info('receive a new connection from %s', address) self.stream = stream self.address = address self.stream_set.add(self.stream) self.stream.set_close_callback(self.on_close) self.stream.read_until('\n', self._on_readline) def _on_readline(self, data): logging.info('read a new line from %s', self.address) for stream in self.stream_set: stream.write(data, self._on_write) def _on_write(self): logging.info('write a line to %s', self.address) if not self.stream.reading(): self.stream.read_until('\n', self._on_readline) def on_close(self): logging.info('client quit %s', self.address) self.stream_set.remove(self.stream) class EchoServer(TCPServer): def __init__(self, io_loop=None, ssl_options=None, **kwargs): logging.info('a echo tcp server is started') TCPServer.__init__(self, io_loop=io_loop, ssl_options=ssl_options, **kwargs) def handle_stream(self, stream, address): EchoConnection(stream, address) def main(): echo_server = EchoServer() echo_server.listen(8888) ioloop.IOLoop.instance().start() if __name__ == '__main__': -
phuslu created this gist
Sep 21, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ #!/usr/bin/env python #coding:utf-8 import sys, os, re import logging from tornado import ioloop, iostream, httpserver logging.basicConfig(level=logging.INFO, format='%(levelname)s - - %(asctime)s %(message)s', datefmt='[%d/%b/%Y %H:%M:%S]') class SocketConnection(object): stream_set = set([]) def __init__(self, stream, address, *args): logging.info('receive a new connection from %s', address) self.stream = stream self.address = address self.stream_set.add(self.stream) self.stream.set_close_callback(self.on_close) self.stream.read_until('\n', self.on_read_line) def on_read_line(self, data): logging.info('read a new line from %s', self.address) for stream in self.stream_set: stream.write(data, self.on_write_complete) def on_write_complete(self): logging.info('write a line to %s', self.address) if not self.stream.reading(): self.stream.read_until('\n', self.on_read_line) def on_close(self): logging.info('client quit %s', self.address) self.stream_set.remove(self.stream) def main(): httpserver.HTTPConnection = SocketConnection http_server = httpserver.HTTPServer(None) http_server.listen(8888) logging.info('tonado server started.') ioloop.IOLoop.instance().start() if __name__ == '__main__': main()