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