2010-12-30 22:11:50 +00:00
|
|
|
#!/usr/bin/env python3
|
1995-04-10 11:46:03 +00:00
|
|
|
|
2010-12-30 22:11:50 +00:00
|
|
|
"""
|
|
|
|
Remote python server.
|
|
|
|
Execute Python commands remotely and send output back.
|
|
|
|
|
|
|
|
WARNING: This version has a gaping security hole -- it accepts requests
|
|
|
|
from any host on the Internet!
|
|
|
|
"""
|
1995-04-10 11:46:03 +00:00
|
|
|
|
|
|
|
import sys
|
2010-12-30 21:33:07 +00:00
|
|
|
from socket import socket, AF_INET, SOCK_STREAM
|
2007-07-17 20:59:35 +00:00
|
|
|
import io
|
1995-04-10 11:46:03 +00:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
PORT = 4127
|
|
|
|
BUFSIZE = 1024
|
|
|
|
|
|
|
|
def main():
|
2004-07-18 05:56:09 +00:00
|
|
|
if len(sys.argv) > 1:
|
2010-12-30 21:33:07 +00:00
|
|
|
port = int(sys.argv[1])
|
2004-07-18 05:56:09 +00:00
|
|
|
else:
|
|
|
|
port = PORT
|
|
|
|
s = socket(AF_INET, SOCK_STREAM)
|
|
|
|
s.bind(('', port))
|
|
|
|
s.listen(1)
|
2010-12-30 21:33:07 +00:00
|
|
|
while True:
|
2004-07-18 05:56:09 +00:00
|
|
|
conn, (remotehost, remoteport) = s.accept()
|
2010-12-30 21:33:07 +00:00
|
|
|
print('connection from', remotehost, remoteport)
|
|
|
|
request = b''
|
2004-07-18 05:56:09 +00:00
|
|
|
while 1:
|
|
|
|
data = conn.recv(BUFSIZE)
|
|
|
|
if not data:
|
|
|
|
break
|
2010-12-30 21:33:07 +00:00
|
|
|
request += data
|
|
|
|
reply = execute(request.decode())
|
|
|
|
conn.send(reply.encode())
|
2004-07-18 05:56:09 +00:00
|
|
|
conn.close()
|
1995-04-10 11:46:03 +00:00
|
|
|
|
|
|
|
def execute(request):
|
2004-07-18 05:56:09 +00:00
|
|
|
stdout = sys.stdout
|
|
|
|
stderr = sys.stderr
|
2007-07-17 20:59:35 +00:00
|
|
|
sys.stdout = sys.stderr = fakefile = io.StringIO()
|
2004-07-18 05:56:09 +00:00
|
|
|
try:
|
|
|
|
try:
|
2006-09-06 06:51:57 +00:00
|
|
|
exec(request, {}, {})
|
2004-07-18 05:56:09 +00:00
|
|
|
except:
|
2007-07-17 20:59:35 +00:00
|
|
|
print()
|
2004-07-18 05:56:09 +00:00
|
|
|
traceback.print_exc(100)
|
|
|
|
finally:
|
|
|
|
sys.stderr = stderr
|
|
|
|
sys.stdout = stdout
|
|
|
|
return fakefile.getvalue()
|
1995-04-10 11:46:03 +00:00
|
|
|
|
2010-12-30 21:33:07 +00:00
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|