Mercurial > cosocket
changeset 6:fdcb6f3e1a0f
Separated out example code into a separate file.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 17 Apr 2009 17:04:30 -0700 |
parents | 4ec829dc7d1d |
children | d3ae3fc76711 |
files | example.py taw.py |
diffstat | 2 files changed, 30 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example.py Fri Apr 17 17:04:30 2009 -0700 @@ -0,0 +1,24 @@ +from taw import * + +def lame_http_server_coroutine(addr): + request = yield to_receive(terminator = '\r\n\r\n') + msg = 'hello %s.' % addr[0] + yield to_send('HTTP/1.1 200 OK\r\n' + + 'Content-Length: %d\r\n' % len(msg) + + 'Content-Type: text/plain\r\n\r\n' + + msg) + +def lame_http_client_coroutine(addr): + yield to_send('GET / HTTP/1.1\r\n\r\n') + response_headers = yield to_receive(terminator = '\r\n\r\n') + response = yield to_receive(bytes = 20) + print 'first response: %s' % repr(response) + response = yield to_receive(bytes = 20) + print 'second response: %s' % repr(response) + +if __name__ == '__main__': + server = ChattyCoroutineServer(('127.0.0.1', 8071), + lame_http_server_coroutine) + client = ChattyCoroutineClient(('www.google.com', 80), + lame_http_client_coroutine) + server.run()
--- a/taw.py Fri Apr 17 17:02:35 2009 -0700 +++ b/taw.py Fri Apr 17 17:04:30 2009 -0700 @@ -1,9 +1,8 @@ -import time import socket import asyncore import asynchat -class AsyncChatCoroutineBridge(asynchat.async_chat): +class _AsyncChatCoroutineBridge(asynchat.async_chat): def __init__(self, coroutine, conn = None): asynchat.async_chat.__init__(self, conn) self.set_terminator(None) @@ -66,15 +65,17 @@ def handle_accept(self): conn, addr = self.accept() coroutine = self.__coroutineFactory(addr) - AsyncChatCoroutineBridge(coroutine, conn) + _AsyncChatCoroutineBridge(coroutine, conn) -class ChattyCoroutineClient(AsyncChatCoroutineBridge): +class ChattyCoroutineClient(_AsyncChatCoroutineBridge): def __init__(self, addr, coroutineFactory): coroutine = coroutineFactory(addr) - AsyncChatCoroutineBridge.__init__(self, coroutine) + _AsyncChatCoroutineBridge.__init__(self, coroutine) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.connect(addr) +# Instructions that coroutines yield. + def to_receive(terminator = None, bytes = None): return {'op': 'receive', 'terminator': terminator, @@ -83,26 +84,3 @@ def to_send(content): return {'op': 'send', 'content': content} - -def lame_http_server_coroutine(addr): - request = yield to_receive(terminator = '\r\n\r\n') - msg = 'hello %s.' % addr[0] - yield to_send('HTTP/1.1 200 OK\r\n' + - 'Content-Length: %d\r\n' % len(msg) + - 'Content-Type: text/plain\r\n\r\n' + - msg) - -def lame_http_client_coroutine(addr): - yield to_send('GET / HTTP/1.1\r\n\r\n') - response_headers = yield to_receive(terminator = '\r\n\r\n') - response = yield to_receive(bytes = 20) - print 'first response: %s' % repr(response) - response = yield to_receive(bytes = 20) - print 'second response: %s' % repr(response) - -if __name__ == '__main__': - server = ChattyCoroutineServer(('127.0.0.1', 8071), - lame_http_server_coroutine) - client = ChattyCoroutineClient(('www.google.com', 80), - lame_http_client_coroutine) - server.run()