blob: f2455837b93cc449ec413b64a990f2c8bd2678a9 [file] [log] [blame]
Guido van Rossum22825e81991-07-01 18:32:32 +00001#! /usr/local/python
2
3# Python implementation of an 'echo' tcp server: echo all data it receives.
4#
5# This is the simplest possible server, sevicing a single request only.
6
7import sys
8from socket import *
9
10# The standard echo port isn't very useful, it requires root permissions!
11# ECHO_PORT = 7
12ECHO_PORT = 50000 + 7
13BUFSIZE = 1024
14
15def main():
16 if len(sys.argv) > 1:
17 port = int(eval(sys.argv[1]))
18 else:
19 port = ECHO_PORT
20 s = socket(AF_INET, SOCK_STREAM)
21 s.bind('', port)
22 s.listen(0)
23 conn, (host, remoteport) = s.accept()
24 print 'connected by', host, remoteport
25 while 1:
26 data = conn.recv(BUFSIZE)
27 if not data:
28 break
29 conn.send(data)
30
31main()