yade-dev team mailing list archive
-
yade-dev team
-
Mailing list archive
-
Message #00713
[svn] r1541 - in trunk/gui: . py
Author: eudoxos
Date: 2008-10-11 15:28:17 +0200 (Sat, 11 Oct 2008)
New Revision: 1541
Added:
trunk/gui/py/PythonTCPServer.py
Modified:
trunk/gui/SConscript
trunk/gui/py/PythonUI_rc.py
Log:
1. Yade now runs TCP server at the first free port above 9000 (accessible from localhost only), which simulates python console. (let me know if you need this over network -- it is DANGEROUS, since someone can delete all your files etc without any authentication). The interpreter has its own namespace.
Modified: trunk/gui/SConscript
===================================================================
--- trunk/gui/SConscript 2008-10-11 07:45:14 UTC (rev 1540)
+++ trunk/gui/SConscript 2008-10-11 13:28:17 UTC (rev 1541)
@@ -75,5 +75,6 @@
env.File('runtime.py','py'),
env.File('ipython.py','py'),
env.File('plot.py','py'),
+ env.File('PythonTCPServer.py','py'),
])
Added: trunk/gui/py/PythonTCPServer.py
===================================================================
--- trunk/gui/py/PythonTCPServer.py 2008-10-11 07:45:14 UTC (rev 1540)
+++ trunk/gui/py/PythonTCPServer.py 2008-10-11 13:28:17 UTC (rev 1541)
@@ -0,0 +1,75 @@
+import SocketServer
+import sys
+
+from yade.wrapper import *
+
+class PythonConsoleSocketEmulator(SocketServer.BaseRequestHandler):
+ def setup(self):
+ if not self.client_address[0].startswith('127.0.0'):
+ print "TCP Connection from non-127.0.0.* address %s rejected"%self.client_address[0]
+ return
+ print self.client_address, 'connected!'
+ self.request.send("""__ __ ____ __ _____ ____ ____
+\ \ / /_ _| _ \ ___ ___ / / |_ _/ ___| _ \
+ \ V / _` | | | |/ _ \ / _ \ / / | || | | |_) |
+ | | (_| | |_| | __/ | (_) / / | || |___| __/
+ |_|\__,_|____/ \___| \___/_/ |_| \____|_|
+
+(connected from %s:%d)
+>>> """%(str(self.client_address[0]),self.client_address[1]))
+ def displayhook(self,s):
+ import pprint
+ self.request.send(pprint.pformat(s))
+ def handle(self):
+ import code,cStringIO,traceback
+ buf=[]
+ while True:
+ data = self.request.recv(1024).rstrip()
+ if data=='\x04' or data=='exit' or data=='quit': # \x04 == ^D
+ return
+ buf.append(data)
+ orig_displayhook,orig_stdout=sys.displayhook,sys.stdout
+ sio=cStringIO.StringIO()
+ continuation=False
+ #print "buffer:",buf
+ try:
+ comp=code.compile_command('\n'.join(buf))
+ if comp:
+ sys.displayhook=self.displayhook
+ sys.stdout=sio
+ exec comp
+ self.request.send(sio.getvalue())
+ buf=[]
+ else:
+ self.request.send('... '); continuation=True
+ except:
+ self.request.send(traceback.format_exc())
+ buf=[]
+ finally:
+ sys.displayhook,sys.stdout=orig_displayhook,orig_stdout
+ if not continuation: self.request.send('\n>>> ')
+ def finish(self):
+ print self.client_address, 'disconnected!'
+ self.request.send('\nBye ' + str(self.client_address) + '\n')
+
+class PythonTCPServer:
+ def __init__(self,minPort=9000,host='',maxPort=65536,background=True):
+ import socket
+ self.port=-1
+ self.host=host
+ tryPort=minPort
+ if maxPort==None: maxPort=minPort
+ while self.port==-1 and tryPort<=maxPort:
+ try:
+ self.server=SocketServer.ThreadingTCPServer((host,tryPort),PythonConsoleSocketEmulator)
+ self.port=tryPort
+ print "Python TCP server listening on %s:%d"%(host if host else 'localhost',self.port)
+ if background:
+ import thread; thread.start_new_thread(self.server.serve_forever,())
+ else: self.server.serve_forever()
+ except socket.error:
+ tryPort+=1
+ if self.port==-1: raise RuntimeError("No free port to listen on in range %d-%d"%(minPort,maxPort))
+
+if __name__=='__main__':
+ PythonTCPServer(background=False)
Modified: trunk/gui/py/PythonUI_rc.py
===================================================================
--- trunk/gui/py/PythonUI_rc.py 2008-10-11 07:45:14 UTC (rev 1540)
+++ trunk/gui/py/PythonUI_rc.py 2008-10-11 13:28:17 UTC (rev 1541)
@@ -5,7 +5,7 @@
from yade import runtime
import sys
-sys.excepthook=sys.__excepthook__ # apport on ubuntu override this, we don't need it
+sys.excepthook=sys.__excepthook__ # apport on ubuntu overrides this, we don't need it
# sys.path.insert(0,runtime.prefix+'/lib/yade'+runtime.suffix+'/extra')
from yade.wrapper import *
@@ -20,6 +20,10 @@
def _quit(): import sys; sys.exit(0)
__builtins__.quit=_quit
+## run the TCP server
+import yade.PythonTCPServer
+yade.PythonTCPServer.PythonTCPServer(minPort=9000)
+
## run simulation if requested from the command line
if runtime.simulation:
print "Running simulation "+runtime.simulation