summaryrefslogtreecommitdiff
path: root/sys/src/cmd/python/Demo/sockets/echosvr.py
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@localhost>2011-05-03 11:25:13 +0000
committercinap_lenrek <cinap_lenrek@localhost>2011-05-03 11:25:13 +0000
commit458120dd40db6b4df55a4e96b650e16798ef06a0 (patch)
tree8f82685be24fef97e715c6f5ca4c68d34d5074ee /sys/src/cmd/python/Demo/sockets/echosvr.py
parent3a742c699f6806c1145aea5149bf15de15a0afd7 (diff)
add hg and python
Diffstat (limited to 'sys/src/cmd/python/Demo/sockets/echosvr.py')
-rwxr-xr-xsys/src/cmd/python/Demo/sockets/echosvr.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/sys/src/cmd/python/Demo/sockets/echosvr.py b/sys/src/cmd/python/Demo/sockets/echosvr.py
new file mode 100755
index 000000000..f8a9623d7
--- /dev/null
+++ b/sys/src/cmd/python/Demo/sockets/echosvr.py
@@ -0,0 +1,31 @@
+#! /usr/bin/env python
+
+# Python implementation of an 'echo' tcp server: echo all data it receives.
+#
+# This is the simplest possible server, servicing a single request only.
+
+import sys
+from socket import *
+
+# The standard echo port isn't very useful, it requires root permissions!
+# ECHO_PORT = 7
+ECHO_PORT = 50000 + 7
+BUFSIZE = 1024
+
+def main():
+ if len(sys.argv) > 1:
+ port = int(eval(sys.argv[1]))
+ else:
+ port = ECHO_PORT
+ s = socket(AF_INET, SOCK_STREAM)
+ s.bind(('', port))
+ s.listen(1)
+ conn, (remotehost, remoteport) = s.accept()
+ print 'connected by', remotehost, remoteport
+ while 1:
+ data = conn.recv(BUFSIZE)
+ if not data:
+ break
+ conn.send(data)
+
+main()