def _handleConnection(conn): conn.setblocking(True) f = conn.makefile() binstruct.write(f, (appinfo.appid, "SocketControl", 0.1)) f.flush() try: clientappid, clientname, clientver, clientstatus = binstruct.read(f) except binstruct.FormatError: print "socketcontrol.handleConnection: wrong signature" return if clientstatus != "ok": print "socketcontrol.handleConnection: status returned %s" % status return from State import state from queue import queue shellGlobals = { "state": state, "queue": queue, } globals = locals = shellGlobals COMPILE_STRING_FN = "<socketcontrol input>" while True: try: idx, s = binstruct.varDecode(f) except Exception: # probably closed return assert isinstance(s, (str, unicode)) try: c = utils.interactive_py_compile(s, COMPILE_STRING_FN) except Exception as e: answer = (idx, "compile-exception", (e.__class__.__name__, str(e))) else: try: ret = eval(c, globals, locals) except Exception as e: answer = (idx, "eval-exception", (e.__class__.__name__, str(e))) else: if ret is not None: try: ret = repr(ret) except Exception as e: ret = "<repr exception: %s: %s>" % ( e.__class__.__name__, str(e)) answer = (idx, "return", ret) f.write(binstruct.varEncode(answer).tostring()) f.flush()
def _handleConnection(conn): conn.setblocking(True) f = conn.makefile() binstruct.write(f, (appinfo.appid, "SocketControl", 0.1)) f.flush() try: clientappid,clientname,clientver,clientstatus = binstruct.read(f) except binstruct.FormatError: print "socketcontrol.handleConnection: wrong signature" return if clientstatus != "ok": print "socketcontrol.handleConnection: status returned %s" % status return from State import state from queue import queue shellGlobals = { "state": state, "queue": queue, } globals = locals = shellGlobals COMPILE_STRING_FN = "<socketcontrol input>" while True: try: idx,s = binstruct.varDecode(f) except Exception: # probably closed return assert isinstance(s, (str,unicode)) try: c = utils.interactive_py_compile(s, COMPILE_STRING_FN) except Exception as e: answer = (idx, "compile-exception", (e.__class__.__name__, str(e))) else: try: ret = eval(c, globals, locals) except Exception as e: answer = (idx, "eval-exception", (e.__class__.__name__, str(e))) else: if ret is not None: try: ret = repr(ret) except Exception as e: ret = "<repr exception: %s: %s>" % (e.__class__.__name__, str(e)) answer = (idx, "return", ret) f.write(binstruct.varEncode(answer).tostring()) f.flush()
def run(self): try: s = socket.socket(socket.AF_UNIX) s.connect(self.sockfile) f = s.makefile() res = binstruct.read(f) except socket.error as e: self.res = e if e.errno == 61: # Connection refused # Cleanup os.remove(self.sockfile) else: s.setblocking(True) # No timeout from now on. self.file = f self.res = res
def connect(verbose=False): global f, s if f: return s = socket.socket(socket.AF_UNIX) sockfile = None if __name__ == "__main__": if len(sys.argv) > 1: sockfile = sys.argv[1] assert os.path.exists(sockfile) s.connect(sockfile) if not sockfile: tmpdir = tempfile.gettempdir() files = glob("%s/%s-*-socketcontrol" % (tmpdir, appinfo.appid)) assert files, "no socket files found" for fn in files: sockfile = fn try: s.connect(sockfile) except socket.error: pass else: if verbose: print "socket:", sockfile break assert s s.setblocking(True) f = s.makefile() serverappid,servername,serverver = binstruct.read(f) if verbose: print "connected to", serverappid, servername, serverver assert serverappid == appinfo.appid assert serverver == 0.1 binstruct.write(f, (appinfo.appid, "SocketControl-InteractiveClient", 0.1, "ok")) f.flush()
def readPublicKeys(fn): keys = binstruct.read(fs.open(fn)) assert isinstance(keys, dict) assert "crypt" in keys assert "sign" in keys return keys
for fn in files: sockfile = fn try: s.connect(sockfile) except socket.error: pass else: print "socket:", sockfile break assert s s.setblocking(True) f = s.makefile() serverappid,servername,serverver = binstruct.read(f) print "connected to", serverappid, servername, serverver assert serverappid == appinfo.appid assert serverver == 0.1 binstruct.write(f, (appinfo.appid, "SocketControl-InteractiveClient", 0.1, "ok")) f.flush() try: import readline except ImportError: pass # ignore idx = 0 while True: idx += 1 try: s = raw_input("> ") except (KeyboardInterrupt,EOFError):
for fn in files: sockfile = fn try: s.connect(sockfile) except socket.error: pass else: print "socket:", sockfile break assert s s.setblocking(True) f = s.makefile() serverappid, servername, serverver = binstruct.read(f) print "connected to", serverappid, servername, serverver assert serverappid == appinfo.appid assert serverver == 0.1 binstruct.write(f, (appinfo.appid, "SocketControl-InteractiveClient", 0.1, "ok")) f.flush() try: import readline except ImportError: pass # ignore idx = 0 while True: