blob: 1109a99e182b55db3981c5d36aaa6dbffb772ad6 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum4e620371995-04-10 11:46:03 +00002
3# Remote python server.
4# Execute Python commands remotely and send output back.
5# WARNING: This version has a gaping security hole -- it accepts requests
6# from any host on the Internet!
7
8import sys
9from socket import *
10import StringIO
11import traceback
12
13PORT = 4127
14BUFSIZE = 1024
15
16def main():
17 if len(sys.argv) > 1:
18 port = int(eval(sys.argv[1]))
19 else:
20 port = PORT
21 s = socket(AF_INET, SOCK_STREAM)
Jeremy Hyltona8d30d52000-08-25 15:38:41 +000022 s.bind(('', port))
Guido van Rossum4e620371995-04-10 11:46:03 +000023 s.listen(1)
24 while 1:
25 conn, (remotehost, remoteport) = s.accept()
26 print 'connected by', remotehost, remoteport
27 request = ''
28 while 1:
29 data = conn.recv(BUFSIZE)
30 if not data:
31 break
32 request = request + data
33 reply = execute(request)
34 conn.send(reply)
35 conn.close()
36
37def execute(request):
38 stdout = sys.stdout
39 stderr = sys.stderr
40 sys.stdout = sys.stderr = fakefile = StringIO.StringIO()
41 try:
42 try:
43 exec request in {}, {}
44 except:
45 print
46 traceback.print_exc(100)
47 finally:
48 sys.stderr = stderr
49 sys.stdout = stdout
50 return fakefile.getvalue()
51
52main()