summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorRyan Williams <rdw@lindenlab.com>2010-01-25 16:26:05 -0800
committerRyan Williams <rdw@lindenlab.com>2010-01-25 16:26:05 -0800
commit5acb08302935ca14bac4f4fb182b1540f05eda19 (patch)
tree277190eb211713793e6f1e79978622c7985e882d /examples
parentbb039a83b5476ffd8738d13bceffaccd0ec3a66e (diff)
downloadeventlet-5acb08302935ca14bac4f4fb182b1540f05eda19.tar.gz
Pulled examples into docs proper, for better linkage on the Web.
Diffstat (limited to 'examples')
-rw-r--r--examples/accept_loop.py51
-rw-r--r--examples/chat_server.py32
2 files changed, 32 insertions, 51 deletions
diff --git a/examples/accept_loop.py b/examples/accept_loop.py
deleted file mode 100644
index 9664881..0000000
--- a/examples/accept_loop.py
+++ /dev/null
@@ -1,51 +0,0 @@
-"""This is a simple echo server that demonstrates an accept loop. To use it,
-run this script and then run 'telnet localhost 6011' in a different terminal.
-
-If you send an empty line to the echo server it will close the connection while
-leaving the server running. If you send the word "shutdown" to the echo server
-it will gracefully exit, terminating any other open connections.
-
-The actual accept loop logic is fully contained within the run_accept_loop
-function. Everything else is setup.
-"""
-
-from eventlet.green import socket
-from eventlet.api import spawn
-
-class Acceptor(object):
- def __init__(self, port=6011):
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.sock.setsockopt(
- socket.SOL_SOCKET,
- socket.SO_REUSEADDR, 1)
- self.sock.bind(('localhost', port))
- self.sock.listen(50)
- self.sock.settimeout(0.5)
- self.done = False
-
- def run_accept_loop(self):
- while not self.done:
- try:
- spawn(self.handle_one_client, self.sock.accept())
- except socket.timeout:
- pass
-
- def handle_one_client(self, sockpair):
- sock, addr = sockpair
- print "Accepted client", addr
- fd = sock.makefile()
- line = fd.readline()
- while line.strip():
- fd.write(line)
- fd.flush()
- if line.startswith("shutdown"):
- self.done = True
- print "Received shutdown"
- break
- line = fd.readline()
- print "Done with client", addr
-
-if __name__ == "__main__":
- a = Acceptor()
- a.run_accept_loop()
- print "Exiting" \ No newline at end of file
diff --git a/examples/chat_server.py b/examples/chat_server.py
new file mode 100644
index 0000000..3897c0f
--- /dev/null
+++ b/examples/chat_server.py
@@ -0,0 +1,32 @@
+import eventlet
+from eventlet.green import socket
+
+participants = []
+
+def read_chat_forever(writer, reader):
+ line = reader.readline()
+ while line:
+ print "Chat:", line.strip()
+ for p in participants:
+ if p is not writer: # Don't echo
+ p.write(line)
+ p.flush()
+ line = reader.readline()
+ participants.remove(writer)
+ print "Participant left chat."
+
+try:
+ print "ChatServer starting up on port 3000"
+ server = socket.socket()
+ server.bind(('0.0.0.0', 3000))
+ server.listen(50)
+ while True:
+ new_connection, address = server.accept()
+ print "Participant joined chat."
+ new_writer = new_connection.makefile('w')
+ participants.append(new_writer)
+ eventlet.spawn_n(read_chat_forever,
+ new_writer,
+ new_connection.makefile('r'))
+except (KeyboardInterrupt, SystemExit):
+ print "ChatServer exiting."