blob: c1acf6cc6c29a70c00d7300df039e3365281f60a [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum22825e81991-07-01 18:32:32 +00002
3# Python implementation of an 'echo' tcp server: echo all data it receives.
4#
Guido van Rossum11349911995-01-12 12:41:20 +00005# This is the simplest possible server, servicing a single request only.
Guido van Rossum22825e81991-07-01 18:32:32 +00006
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)
Guido van Rossumea6f6ed1994-02-28 09:25:06 +000022 s.listen(1)
Guido van Rossumc99a4f91992-05-19 13:51:32 +000023 conn, (remotehost, remoteport) = s.accept()
24 print 'connected by', remotehost, remoteport
Guido van Rossum22825e81991-07-01 18:32:32 +000025 while 1:
26 data = conn.recv(BUFSIZE)
27 if not data:
28 break
29 conn.send(data)
30
31main()