[shpy-commit] r2866 - shpy/trunk/dist/shpy/net

hpk@codespeak.net hpk@codespeak.net
Tue, 20 Jan 2004 17:38:33 +0100 (MET)


Author: hpk
Date: Tue Jan 20 17:38:33 2004
New Revision: 2866

Added:
   shpy/trunk/dist/shpy/net/shell.py   (contents, props changed)
Log:
a shell script to use with 'startserver.py' type processes. 

note that this script is not safe to be run from multiple clients 
because on the server side it grabs sys.stdout/stderr ...




Added: shpy/trunk/dist/shpy/net/shell.py
==============================================================================
--- (empty file)
+++ shpy/trunk/dist/shpy/net/shell.py	Tue Jan 20 17:38:33 2004
@@ -0,0 +1,84 @@
+"""
+a remote python shell 
+
+for injection into server_unpickle.py 
+"""
+import sys, os, socket, select, pickle
+
+try:
+    clientsock 
+except NameError:
+    import sys
+    host, port  = sys.argv[1].split(':')
+    port = int(port)
+    myself = open(os.path.abspath(sys.argv[0])).read()
+    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    sock.connect((host, port))
+    sockfile = sock.makefile('w+',0)
+    p = pickle.Pickler(sockfile)
+    p.dump(myself) 
+    print "send boot string"
+    inputlist = [ sock, sys.stdin ]
+    try:
+        while 1:
+            r,w,e = select.select(inputlist, [], [])
+            if sys.stdin in r:
+                line = raw_input()
+                sock.sendall(line + '\n')
+            if sock in r:
+                line = sock.recv(4096)
+                sys.stdout.write(line)
+                sys.stdout.flush()
+    except:
+        import traceback
+        print traceback.print_exc()
+
+    sys.exit(1)
+
+print "server side starting"
+# server side
+#
+from traceback import print_exc
+from threading import Thread
+
+class promptagent(Thread):
+    def __init__(self, clientsock):
+        Thread.__init__(self)
+        self.clientsock = clientsock
+
+    def run(self):
+        print "Entering thread prompt loop"
+        clientfile = self.clientsock.makefile('w')
+
+        filein = self.clientsock.makefile('r')
+        loc = self.clientsock.getsockname()
+
+        while 1:
+            try:
+                clientfile.write('%s %s >>> ' % loc)
+                clientfile.flush()
+                line = filein.readline()
+                if len(line)==0: raise EOFError,"nothing"
+                #print >>sys.stderr,"got line: " + line
+                if line.strip(): 
+                    oldout, olderr = sys.stdout, sys.stderr 
+                    sys.stdout, sys.stderr = clientfile, clientfile
+                    try:
+                        try:
+                            exec compile(line + '\n','<remote stdin>', 'single')
+                        except:
+                            print print_exc()
+                    finally:
+                        sys.stdout=oldout
+                        sys.stderr=olderr
+                clientfile.flush()
+            except EOFError,e:
+                print >>sys.stderr, "connection close, prompt thread returns"
+                break
+                #print >>sys.stdout, "".join(apply(format_exception,sys.exc_info()))
+
+        self.clientsock.close()
+
+prompter = promptagent(clientsock)
+prompter.start()
+print "promptagent - thread started"