import os import subprocess import socket from time import sleep class RemoteWebGui: def __init__(self): pass #def init # CAl un get_first_xpra_port_free def get_first_display_free(self): ''' Returns first Display free from 42 ''' try: display=42 while(os.path.isfile('/tmp/.X'+str(display)+'-lock')): display+=1 return ":"+str(display) except Exception as e: print "Captured: "+str(e) return {'status': False, 'msg':'[RemoteGuiManager] '+str(e)} return ":42" #def get_first_display def get_first_free_port(self): ''' Returns first opened port, starting by 10013 ''' port=10013; sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM); while (port<99999): status=sock.connect_ex(('127.0.0.1', port)); print "chechking port "+str(port)+" is "+str(status); if status!=0: return port; port=port+1; return -1 def create_connection(self, xephyr_options=" -ac -terminate -screen 1024x768 "): # Cal afegir com a parametre el username i que retorne el port.... try: os.environ["HOME"]="/home/lliurex"; os.environ["XAUTHORITY"]="/home/lliurex/.Xauthority"; display=self.get_first_display_free() port=self.get_first_free_port(); xephyr_cmd="Xephyr "+xephyr_options+" "+display; xpra_cmd="xpra start --bind-tcp=0.0.0.0:"+str(port)+" --html=on --no-pulseaudio --exit-with-children --start-child='"+xephyr_cmd+"'"; print "Exec: "+xpra_cmd; p=subprocess.call([xpra_cmd], shell=True); # wait for port is listening sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM); status=111; while (status!=0): status=sock.connect_ex(('127.0.0.1', port)); print "port "+str(port)+" is available with status: "+str(status); return {'port':str(port), 'display':display}; except Exception as e: print e return -1 return 0 #def remote_execute def close_connection(self, port): try: cmd='xpra stop tcp:0.0.0.0:'+str(port); subprocess.call([cmd], shell=True); except Exception as e: print e return -1 return 0 def run_into_connection(self, command, display): try: cmd='export DISPLAY='+display+";"+command; #//p=subprocess.call([cmd], shell=True); p=subprocess.Popen([cmd], shell=True); return p except Exception as e: print e return -1 return 0 #class RemoteGuiManager