summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2013-10-17 15:10:58 -0700
committerGuido van Rossum <guido@python.org>2013-10-17 15:10:58 -0700
commitba21aaf80c470165978251adb7c8b7d689f65d8b (patch)
tree2d2ef83c171cf1e16bd918576a9c03eecb14dc7b /examples
parent8eaa5e7976960232dcf1c7070e334beb33f75597 (diff)
downloadtrollius-ba21aaf80c470165978251adb7c8b7d689f65d8b.tar.gz
Add proper argument parsing to source/sink examples.
Diffstat (limited to 'examples')
-rw-r--r--examples/sink.py29
-rw-r--r--examples/source.py38
2 files changed, 56 insertions, 11 deletions
diff --git a/examples/sink.py b/examples/sink.py
index 855a4aa..119af6e 100644
--- a/examples/sink.py
+++ b/examples/sink.py
@@ -1,14 +1,28 @@
"""Test service that accepts connections and reads all data off them."""
+import argparse
import sys
from tulip import *
+ARGS = argparse.ArgumentParser(description="TCP data sink example.")
+ARGS.add_argument(
+ '--iocp', action='store_true', dest='iocp',
+ default=False, help='Use IOCP event loop (Windows only)')
+ARGS.add_argument(
+ '--host', action='store', dest='host',
+ default='127.0.0.1', help='Host name')
+ARGS.add_argument(
+ '--port', action='store', dest='port',
+ default=1111, type=int, help='Port number')
+
server = None
+
def dprint(*args):
print('sink:', *args, file=sys.stderr)
+
class Service(Protocol):
def connection_made(self, tr):
@@ -32,21 +46,26 @@ class Service(Protocol):
def connection_lost(self, how):
dprint('closed', repr(how))
+
@coroutine
-def start(loop):
+def start(loop, host, port):
global server
- server = yield from loop.create_server(Service, 'localhost', 1111)
+ server = yield from loop.create_server(Service, host, port)
dprint('serving', [s.getsockname() for s in server.sockets])
yield from server.wait_closed()
+
def main():
- if '--iocp' in sys.argv:
+ args = ARGS.parse_args()
+ if args.iocp:
from tulip.windows_events import ProactorEventLoop
loop = ProactorEventLoop()
set_event_loop(loop)
- loop = get_event_loop()
- loop.run_until_complete(start(loop))
+ else:
+ loop = get_event_loop()
+ loop.run_until_complete(start(loop, args.host, args.port))
loop.close()
+
if __name__ == '__main__':
main()
diff --git a/examples/source.py b/examples/source.py
index 7a5011d..f5d0291 100644
--- a/examples/source.py
+++ b/examples/source.py
@@ -1,12 +1,32 @@
"""Test client that connects and sends infinite data."""
+import argparse
import sys
from tulip import *
+
+ARGS = argparse.ArgumentParser(description="TCP data sink example.")
+ARGS.add_argument(
+ '--iocp', action='store_true', dest='iocp',
+ default=False, help='Use IOCP event loop (Windows only)')
+ARGS.add_argument(
+ '--stop', action='store_true', dest='stop',
+ default=False, help='Stop the server by sending it b"stop" as data')
+ARGS.add_argument(
+ '--host', action='store', dest='host',
+ default='127.0.0.1', help='Host name')
+ARGS.add_argument(
+ '--port', action='store', dest='port',
+ default=1111, type=int, help='Port number')
+
+args = None
+
+
def dprint(*args):
print('source:', *args, file=sys.stderr)
+
class Client(Protocol):
data = b'x'*16*1024
@@ -18,7 +38,7 @@ class Client(Protocol):
self.lost = False
self.loop = get_event_loop()
self.waiter = Future()
- if '--stop' in sys.argv[1:]:
+ if args.stop:
self.tr.write(b'stop')
self.tr.close()
else:
@@ -37,22 +57,28 @@ class Client(Protocol):
self.lost = True
self.waiter.set_result(None)
+
@coroutine
-def start(loop):
- tr, pr = yield from loop.create_connection(Client, '127.0.0.1', 1111)
+def start(loop, host, port):
+ tr, pr = yield from loop.create_connection(Client, host, port)
dprint('tr =', tr)
dprint('pr =', pr)
res = yield from pr.waiter
return res
+
def main():
- if '--iocp' in sys.argv:
+ global args
+ args = ARGS.parse_args()
+ if args.iocp:
from tulip.windows_events import ProactorEventLoop
loop = ProactorEventLoop()
set_event_loop(loop)
- loop = get_event_loop()
- loop.run_until_complete(start(loop))
+ else:
+ loop = get_event_loop()
+ loop.run_until_complete(start(loop, args.host, args.port))
loop.close()
+
if __name__ == '__main__':
main()