def import_exec_globals(config, session): ''' get exec_globals directory cycle over its files if the file name not in importregistry send its contents to server put filename in importregistry ''' for module in get_plugins(config): dirname = os.path.join(os.path.dirname(module.__file__), 'exec_globals') for fname in sorted(os.listdir(dirname)): if fname.endswith('.py'): name = 'execs:' + fname[:-3] try: session.query(ImportRegistry).filter(ImportRegistry.name==name).one() except NoResultFound: path = os.path.join(dirname, fname) with open(path, 'r') as f: eg = f.read() kb = Client((config('kb_host'), int(config('kb_port')))) kb.send_bytes('compiler:exec_globals:' + eg) kb.send_bytes('FINISH-TERMS') for fact in iter(kb.recv_bytes, 'END'): print(fact) kb.close() ir = ImportRegistry(name) session.add(ir)
def send_reduction_keys(rdd, op_handler_socket, start_time, qid='0'): list_rdd = rdd.collect() reduction_str = ",".join([r for r in list_rdd]) reduction_socket = Client(tuple(op_handler_socket)) reduction_socket.send_bytes("k," + qid + "," + reduction_str + "\n") print("Sending P2: ", qid, list_rdd, reduction_str, " at time", time.time() - start_time)
def send_reduction_keys(rdd, op_handler_socket, start_time, qid='0'): list_rdd = list(set(rdd.collect())) reduction_str = ",".join([str(r) for r in list_rdd]) reduction_socket = Client(tuple(op_handler_socket)) reduction_socket.send_bytes("k," + qid + "," + reduction_str + "\n") print("For", qid, "stream processor sending ", [str(x) for x in list_rdd], " at time", time.time() - start_time)
class NLQClient: def __init__(self, host, port, authkey, dataset=None, mode=None): self.host = host self.port = port self.authkey = authkey self.dataset = dataset self.mode = mode def connect(self): address = (self.host, self.port) while True: try: self.conn = Client(address, authkey=self.authkey) break except Exception as e: traceback.print_exc() time.sleep(2) pass def run(self, tid, schema, nlq, tsq_level, literals, timeout=None, minimal_join_paths=False): task = ProtoTask() task.id = str(tid) task.dataset = self.dataset or '' task.mode = self.mode or '' task.tsq_level = tsq_level task.db_name = schema.db_id task.timeout = timeout or 0 task.minimal_join_paths = minimal_join_paths if isinstance(nlq, list): for token in nlq: task.nlq_tokens.append(token) else: task.nlq_tokens.append(nlq) task.literals.CopyFrom(literals) self.conn.send_bytes(task.SerializeToString()) msg = self.conn.recv_bytes() proto_cands = ProtoCandidates() proto_cands.ParseFromString(msg) return proto_cands def close(self): self.conn.send_bytes(b'close') self.conn.close()
def workerConnection(self, wid): """Thread with worker connection""" worker = self.workers[wid] self.called += 1 if self.verboseLog: print "[INFO] Connecting to %s:%d..." % (worker.ipaddress, worker.port) #TODO make try except statement to catch unresponsive hosts address = (worker.ipaddress, worker.port) try: conn = Client(address, authkey=worker.password) worker.alive = True # Add a good flag # Connect and get ready token resp = conn.recv() if self.verboseLog: print resp[0] worker.corecount = resp[1] self.C.append(resp[1]) # Add the number of available cores to collection with open("job.tar.gz", 'rb') as f: conn.send_bytes(f.read()) f.close() # PAUSE HERE and wait for all threads to return core number self.wait_for_core_count.wait() print "[INFO] %d Job files allocated to worker %s" % (len(worker.jobFiles), worker.ipaddress) rec = conn.recv() if self.verboseLog: print rec[0] conn.send([self.inputJob[0], {"input" : worker.jobFiles, "joboptions" : self.joboptions}]) output = conn.recv() if output[0]: self.output.append(output[0]) #Write to stack of histograms self.success += 1 else: print "[ERROR] Job failed at %s:%d" % address # TODO We should resubmit the jobs to another worker.... conn.close() print "[INFO] Connection to %s closed." % worker.ipaddress except: print "[WARNING] Connection to %s:%d failed." % (address[0], address[1])#, sys.exc_info()[1][1]) finally: self.alive -= 1
class SLAMClient: def __init__(self, port=6000, server_ip='localhost'): address = (server_ip, port) self.conn = Client(address, authkey=b'SLAM!') self.port = port self.server_ip = server_ip def send_data(self, data): data_bytes = pickle.dumps(data, protocol=2) self.conn.send_bytes(data_bytes) def close(self): self.conn.close()
def ask_kb(config, q): ''' ask kb synchronously ''' conn = Client((config('kb_host'), int(config('kb_port')))) conn.send_bytes(q) conn.send_bytes('FINISH-TERMS') recv, resp = '', '' while recv != 'END': resp = recv recv = conn.recv_bytes() conn.close() return resp
def push_out(self): address = ('127.0.0.1', Spider.PUSH_OUT_PORT) connection = Client(address, authkey=self.authkey) log.info('Spider : connection established from {}'.format(address)) while True: log.debug('in spider push_out') url = self.pop_one() if url: try: connection.send_bytes(url) log.debug('SPIDER PUSH : {}'.format(url)) except Exception, e: log.warning('Spider PUSH OUT ERROR: {}'.format(e)) continue
def push_out(self): address = ('127.0.0.1', Frontier.PUSH_OUT_PORT) connection = Client(address, authkey=self.authkey) log.info('Frontier : connection established from {}'.format(address)) counter = 0 while True: url = self.back_queue.push_out() if url: try: connection.send_bytes(url) if counter % 2000 == 0: log.info('FRONTIER PUSH TOTAL: {}'.format(counter)) counter += 1 except Exception,e: log.warning('Frontier PUSH OUT ERROR:{}'.format(e)) continue
def on_get(self, req, resp): resp.set_header('Access-Control-Allow-Origin', '*') sound = req.get_param('sound') seconds = req.get_param_as_float('seconds') nresults = req.get_param_as_int('nresults') or 10 connection = TcpClient(self.index_server_address) query_data = {'sound': sound, 'seconds': seconds, 'nresults': nresults} connection.send_bytes(json.dumps(query_data).encode()) raw_resp = connection.recv_bytes() results = json.loads(raw_resp.decode()) resp.media = { 'total_count': len(results), 'items': results }
def terms_client(config): ''' CLI for terms server ''' while True: terms = raw_input('>> ') if terms in ('quit', 'q', 'exit'): break conn = Client((config('kb_host'), int(config('kb_port')))) conn.send_bytes(terms) conn.send_bytes('FINISH-TERMS') recv, resp = '', '' while recv != 'END': resp = recv recv = conn.recv_bytes() print(recv) conn.close() print('bye!')
def import_ontologies(config, session): ''' get directory cycle over trm files if the file name not in importregistry break file content on dots send pieces to server put filename in importregistry ''' for module in get_plugins(config): fname = os.path.join(os.path.dirname(module.__file__), 'ontology', 'terms.trm') totell = [] kb = Client((config('kb_host'), int(config('kb_port')))) with open(fname, 'r') as f: for line in f.readlines(): if line: kb.send_bytes(line) kb.send_bytes('FINISH-TERMS') for fact in iter(kb.recv_bytes, 'END'): print(fact) kb.close()
def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False): conn = Client((self.host, self.port)) conn.send_bytes(bytes(code, 'UTF-8')) conn.send_bytes(bytes('FINISH-TERMS', 'UTF-8')) recv, resp = bytes(), str() while recv != b'END': if recv: resp += ', ' + recv.decode('UTF-8') recv = conn.recv_bytes() conn.close() if not silent: stream_content = {'name': 'stdout', 'text': resp} self.send_response(self.iopub_socket, 'stream', stream_content) return {'status': 'ok', # The base class increments the execution count 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}, }
def run(self): kb = Client((self.config('kb_host'), int(self.config('kb_port')))) fact = self.fact localdata.data = self.data localdata.user = self.user if self.user == 'admin': fact += '.' else: fact = '(want %s, do %s).' % (self.user, fact) kb.send_bytes(fact) kb.send_bytes('FINISH-TERMS') for fact in iter(kb.recv_bytes, 'END'): toweb = apply_fact(self.tserver, fact) toweb = json.dumps(toweb).encode('utf8') try: with self.wslock: self.wsock.send(toweb) except WebSocketError: break kb.close()
#!/usr/bin/env python import sys import os import socket from multiprocessing.connection import Client if len(sys.argv) < 2: print "You must pass a parameter. (open, close, trigger, away, home, state)" else: address = ('192.168.86.9', 6000) conn = Client(address) conn.send_bytes(sys.argv[1]) print conn.recv_bytes() conn.close()
class Wdb(object): """Wdb debugger main class""" _instances = {} _sockets = [] enabled = True breakpoints = set() watchers = defaultdict(list) @staticmethod def get(no_create=False): """Get the thread local singleton""" pid = os.getpid() thread = threading.current_thread() wdb = Wdb._instances.get((pid, thread)) if not wdb and not no_create: wdb = object.__new__(Wdb) Wdb.__init__(wdb) wdb.pid = pid wdb.thread = thread Wdb._instances[(pid, thread)] = wdb return wdb @staticmethod def pop(): """Remove instance from instance list""" pid = os.getpid() thread = threading.current_thread() Wdb._instances.pop((pid, thread)) def __new__(cls): return cls.get() def __init__(self): log.debug('New wdb instance %r' % self) self.obj_cache = {} self.tracing = False self.begun = False self.connected = False self.stepping = False self.extra_vars = {} self.last_obj = None self.reset() self.uuid = str(uuid4()) self.state = Running(None) self.full = False self.below = False self._socket = None def run_file(self, filename): """Run the file `filename` with trace""" import __main__ __main__.__dict__.clear() __main__.__dict__.update({ "__name__": "__main__", "__file__": filename, "__builtins__": __builtins__, }) with open(filename, "rb") as fp: statement = "exec(compile(%r, %r, 'exec'))" % ( fp.read(), filename) self.run(statement, filename) def run(self, cmd, fn=None, globals=None, locals=None): """Run the cmd `cmd` with trace""" if globals is None: import __main__ globals = __main__.__dict__ if locals is None: locals = globals self.reset() if isinstance(cmd, str): cmd = compile(cmd, fn or "<wdb>", "exec") self.start_trace() self.breakpoints.add(Breakpoint(fn, temporary=True)) try: execute(cmd, globals, locals) finally: self.stop_trace() def reset(self): """Refresh linecache""" import linecache linecache.checkcache() def connect(self): """Connect to wdb server""" log.info('Connecting socket on %s:%d' % (SOCKET_SERVER, SOCKET_PORT)) tries = 0 while not self._socket and tries < 10: try: self._socket = Client((SOCKET_SERVER, SOCKET_PORT)) except socket.error: tries += 1 log.warning( 'You must start wdb.server. ' '(Retrying on %s:%d) [Try #%d]' % ( SOCKET_SERVER, SOCKET_PORT, tries)) if not self._socket: log.error('Could not connect to server') sys.exit(2) Wdb._sockets.append(self._socket) self._socket.send_bytes(self.uuid.encode('utf-8')) def trace_dispatch(self, frame, event, arg): """This function is called every line, function call, function return and exception during trace""" fun = getattr(self, 'handle_' + event) if fun and ( (event == 'line' and self.breaks(frame)) or (event == 'exception' and (self.full or frame == self.state.frame or (self.below and frame.f_back == self.state.frame))) or self.state.stops(frame, event)): fun(frame, arg) if event == 'return' and frame == self.state.frame: # Upping state if self.state.up(): # No more frames self.stop_trace() return # Threading / Multiprocessing support co = self.state.frame.f_code if (( co.co_filename.endswith('threading.py') and co.co_name.endswith('_bootstrap_inner') ) or (self.state.frame.f_code.co_filename.endswith( os.path.join('multiprocessing', 'process.py')) and self.state.frame.f_code.co_name == '_bootstrap')): # Thread / Process is dead self.stop_trace() self.die() return if (event == 'call' and not self.stepping and not self.full and not (self.below and frame.f_back == self.state.frame) and not self.get_file_breaks(frame.f_code.co_filename)): # Don't trace anymore here return return self.trace_dispatch def trace_debug_dispatch(self, frame, event, arg): """Utility function to add debug to tracing""" trace_log.info('Frame:%s. Event: %s. Arg: %r' % ( pretty_frame(frame), event, arg)) trace_log.debug('state %r breaks ? %s stops ? %s' % ( self.state, self.breaks(frame, no_remove=True), self.state.stops(frame, event) )) if event == 'return': trace_log.debug( 'Return: frame: %s, state: %s, state.f_back: %s' % ( pretty_frame(frame), pretty_frame(self.state.frame), pretty_frame(self.state.frame.f_back))) if self.trace_dispatch(frame, event, arg): return self.trace_debug_dispatch trace_log.debug("No trace %s" % pretty_frame(frame)) def start_trace(self, full=False, frame=None, below=False): """Start tracing from here""" if self.tracing: return self.reset() log.info('Starting trace on %r' % self.thread) frame = frame or sys._getframe().f_back # Setting trace without pausing self.set_trace(frame, break_=False) self.tracing = True self.below = below self.full = full def set_trace(self, frame=None, break_=True): """Break at current state""" # We are already tracing, do nothing trace_log.info('Setting trace %s (stepping %s) (current_trace: %s)' % ( pretty_frame(frame or sys._getframe().f_back), self.stepping, sys.gettrace())) if self.stepping: return self.reset() trace = (self.trace_dispatch if trace_log.level >= 30 else self.trace_debug_dispatch) trace_frame = frame = frame or sys._getframe().f_back while frame: frame.f_trace = trace frame = frame.f_back self.state = Step(trace_frame) if break_ else Running(trace_frame) sys.settrace(trace) def stop_trace(self, frame=None): """Stop tracing from here""" self.tracing = False self.full = False frame = frame or sys._getframe().f_back while frame: del frame.f_trace frame = frame.f_back sys.settrace(None) log.info('Stopping trace on %r' % self.thread) def set_until(self, frame, lineno=None): """Stop on the next line number.""" self.state = Until(frame, frame.f_lineno) def set_step(self, frame): """Stop on the next line.""" self.state = Step(frame) def set_next(self, frame): """Stop on the next line in current frame.""" self.state = Next(frame) def set_return(self, frame): """Stop when returning from the given frame.""" self.state = Return(frame) def set_continue(self, frame): """Don't stop anymore""" self.state = Running(frame) if not self.tracing and not self.breakpoints: # If we were in a set_trace and there's no breakpoint to trace for # Run without trace self.stop_trace() def get_break(self, filename, lineno, temporary, cond, funcname): if lineno and not cond: return LineBreakpoint(filename, lineno, temporary) elif cond: return ConditionalBreakpoint( filename, lineno, cond, temporary) elif funcname: return FunctionBreakpoint(filename, funcname, temporary) else: return Breakpoint(filename, temporary) def set_break(self, filename, lineno=None, temporary=False, cond=None, funcname=None): """Put a breakpoint for filename""" log.info('Setting break fn:%s lno:%s tmp:%s cond:%s fun:%s' % ( filename, lineno, temporary, cond, funcname)) breakpoint = self.get_break( filename, lineno, temporary, cond, funcname) self.breakpoints.add(breakpoint) if not temporary: self._socket.send_bytes( b'Server|AddBreak|' + pickle.dumps(breakpoint, protocol=2)) log.info('Breakpoint %r added' % breakpoint) def clear_break(self, filename, lineno=None, temporary=False, cond=None, funcname=None): """Remove a breakpoint""" log.info('Removing break fn:%s lno:%s tmp:%s cond:%s fun:%s' % ( filename, lineno, temporary, cond, funcname)) breakpoint = self.get_break( filename, lineno, temporary or False, cond, funcname) if temporary is None and breakpoint not in self.breakpoints: breakpoint = self.get_break( filename, lineno, True, cond, funcname) try: self.breakpoints.remove(breakpoint) if not temporary: self._socket.send_bytes( b'Server|RmBreak|' + pickle.dumps(breakpoint, protocol=2)) log.info('Breakpoint %r removed' % breakpoint) except: log.info('Breakpoint %r not removed: not found' % breakpoint) def safe_repr(self, obj): """Like a repr but without exception""" try: return repr(obj) except Exception as e: return '??? Broken repr (%s: %s)' % (type(e).__name__, e) def safe_better_repr(self, obj): """Repr with inspect links on objects""" try: rv = self.better_repr(obj) except Exception: rv = None if rv: return rv self.obj_cache[id(obj)] = obj return '<a href="%d" class="inspect">%s</a>' % ( id(obj), escape(repr(obj))) def better_repr(self, obj): """Repr with html decorations""" if isinstance(obj, dict): if type(obj) != dict: dict_repr = type(obj).__name__ + '({' closer = '})' else: dict_repr = '{' closer = '}' if len(obj) > 2: dict_repr += '<table>' dict_repr += ''.join([ '<tr><td>' + self.safe_repr(key) + '</td><td>:</td>' '<td>' + self.safe_better_repr(val) + '</td></tr>' for key, val in sorted(obj.items(), key=lambda x: x[0])]) dict_repr += '</table>' else: dict_repr += ', '.join([ self.safe_repr(key) + ': ' + self.safe_better_repr(val) for key, val in sorted(obj.items(), key=lambda x: x[0])]) dict_repr += closer return dict_repr if any([ isinstance(obj, list), isinstance(obj, set), isinstance(obj, tuple)]): if type(obj) == list: iter_repr = '[' closer = ']' elif type(obj) == set: iter_repr = '{' closer = '}' elif type(obj) == tuple: iter_repr = '(' closer = ')' else: iter_repr = escape(obj.__class__.__name__) + '([' closer = '])' splitter = ', ' if len(obj) > 2: splitter += '\n' iter_repr += '\n' closer = '\n' + closer iter_repr += splitter.join( [self.safe_better_repr(val) for val in obj]) iter_repr += closer return iter_repr @contextmanager def capture_output(self, with_hook=True): """Steal stream output, return them in string, restore them""" self.hooked = '' def display_hook(obj): # That's some dirty hack self.hooked += self.safe_better_repr(obj) self.last_obj = obj stdout, stderr = sys.stdout, sys.stderr if with_hook: d_hook = sys.displayhook sys.displayhook = display_hook sys.stdout, sys.stderr = StringIO(), StringIO() out, err = [], [] try: yield out, err finally: out.extend(sys.stdout.getvalue().splitlines()[1:]) err.extend(sys.stderr.getvalue().splitlines()) if with_hook: sys.displayhook = d_hook sys.stdout, sys.stderr = stdout, stderr def dmp(self, thing): """Dump the content of an object in a dict for wdb.js""" def safe_getattr(key): """Avoid crash on getattr""" try: return getattr(thing, key) except Exception as e: return 'Error getting attr "%s" from "%s" (%s: %s)' % ( key, thing, type(e).__name__, e) return dict( (escape(key), { 'val': self.safe_better_repr(safe_getattr(key)), 'type': type(safe_getattr(key)).__name__ }) for key in dir(thing) ) def get_file(self, filename): """Get file source from cache""" import linecache return to_unicode_string( ''.join(linecache.getlines(filename)), filename) def get_stack(self, f, t): """Build the stack from frame and traceback""" stack = [] if t and t.tb_frame == f: t = t.tb_next while f is not None: stack.append((f, f.f_lineno)) f = f.f_back stack.reverse() i = max(0, len(stack) - 1) while t is not None: stack.append((t.tb_frame, t.tb_lineno)) t = t.tb_next if f is None: i = max(0, len(stack) - 1) return stack, i def get_trace(self, frame, tb): """Get a dict of the traceback for wdb.js use""" import linecache frames = [] stack, _ = self.get_stack(frame, tb) current = 0 for i, (stack_frame, lno) in enumerate(stack): if frame == stack_frame: current = i break for i, (stack_frame, lno) in enumerate(stack): code = stack_frame.f_code filename = code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lno, stack_frame.f_globals) line = to_unicode_string(line, filename) line = line and line.strip() startlnos = dis.findlinestarts(code) lastlineno = list(startlnos)[-1][1] frames.append({ 'file': filename, 'function': code.co_name, 'flno': code.co_firstlineno, 'llno': lastlineno, 'lno': lno, 'code': line, 'level': i, 'current': current == i }) # While in exception always put the context to the top current = len(frames) - 1 return stack, frames, current def send(self, data): """Send data through websocket""" log.debug('Sending %s' % data) self._socket.send_bytes(data.encode('utf-8')) def receive(self, pickled=False): """Receive data through websocket""" log.debug('Receiving') data = self._socket.recv_bytes() if pickled: data = pickle.loads(data) log.debug('Got pickled %r' % data) return data log.debug('Got %s' % data) return data.decode('utf-8') def interaction( self, frame, tb=None, exception='Wdb', exception_description='Stepping', init=None): """User interaction handling blocking on socket receive""" log.info('Interaction for %r -> %r %r %r %r' % ( self.thread, frame, tb, exception, exception_description)) self.stepping = True if not self.connected: self.connect() log.debug('Launching browser and wait for connection') web_url = 'http://%s:%d/debug/session/%s' % ( WEB_SERVER or 'localhost', WEB_PORT or 1984, self.uuid) server = WEB_SERVER or '[wdb.server]' if WEB_PORT: server += ':%s' % WEB_PORT if WDB_NO_BROWSER_AUTO_OPEN: log.warning('You can now launch your browser at ' 'http://%s/debug/session/%s' % ( server, self.uuid)) elif not webbrowser.open(web_url): log.warning('Unable to open browser, ' 'please go to http://%s/debug/session/%s' % ( server, self.uuid)) self.connected = True interaction = Interaction( self, frame, tb, exception, exception_description, init=init) # For meta debugging purpose self._ui = interaction if self.begun: # Each new state sends the trace and selects a frame interaction.init() else: self.begun = True interaction.loop() def handle_call(self, frame, argument_list): """This method is called when there is the remote possibility that we ever need to stop in this function.""" fun = frame.f_code.co_name log.info('Calling: %r' % fun) init = 'Echo|%s' % dump({ 'for': '__call__', 'val': fun}) self.interaction( frame, init=init, exception_description='Calling %s' % fun) def handle_line(self, frame, arg): """This function is called when we stop or break at this line.""" log.info('Stopping at line %s' % pretty_frame(frame)) self.interaction(frame) def handle_return(self, frame, return_value): """This function is called when a return trap is set here.""" self.obj_cache[id(return_value)] = return_value self.extra_vars['__return__'] = return_value fun = frame.f_code.co_name log.info('Returning from %r with value: %r' % ( fun, return_value)) init = 'Echo|%s' % dump({ 'for': '__return__', 'val': return_value }) self.interaction( frame, init=init, exception_description='Returning from %s with value %s' % ( fun, return_value)) def handle_exception(self, frame, exc_info): """This function is called if an exception occurs, but only if we are to stop at or just below this level.""" type_, value, tb = exc_info # Python 3 is broken see http://bugs.python.org/issue17413 _value = value if not isinstance(_value, BaseException): _value = type_(value) fake_exc_info = type_, _value, tb log.error('Exception during trace', exc_info=fake_exc_info) self.obj_cache[id(exc_info)] = exc_info self.extra_vars['__exception__'] = exc_info exception = type_.__name__ exception_description = str(value) init = 'Echo|%s' % dump({ 'for': '__exception__', 'val': escape('%s: %s') % ( exception, exception_description)}) # User exception is 4 frames away from exception log.warning(pretty_frame(frame)) frame = frame or sys._getframe().f_back.f_back.f_back.f_back self.interaction( frame, tb, exception, exception_description, init=init) def breaks(self, frame, no_remove=False): """Return True if there's a breakpoint at frame""" for breakpoint in set(self.breakpoints): if breakpoint.breaks(frame): if breakpoint.temporary and not no_remove: self.breakpoints.remove(breakpoint) return True return False def get_file_breaks(self, filename): """List all file `filename` breakpoints""" return [breakpoint for breakpoint in self.breakpoints if breakpoint.on_file(filename)] def get_breaks_lno(self, filename): """List all line numbers that have a breakpoint""" return list( filter(lambda x: x is not None, [getattr(breakpoint, 'line', None) for breakpoint in self.breakpoints if breakpoint.on_file(filename)])) def die(self): """Time to quit""" log.info('Time to die') if self.connected: try: self.send('Die') except: pass self._socket.close() self.pop()
class Environment(object): """Supplement server client""" def __init__(self, executable=None, env=None, logfile=None): """Environment constructor :param executable: path to python executable. May be path to virtualenv interpreter start script like ``/path/to/venv/bin/python``. :param env: environment variables dict, e.g. ``DJANGO_SETTINGS_MODULE`` value. :param logfile: explicit log file, can be passed via environment SUPP_LOG_FILE """ self.executable = executable or sys.executable self.env = env self.logfile = logfile self.prepare_thread = None self.prepare_lock = Lock() def _run(self): from subprocess import Popen from multiprocessing.connection import Client, arbitrary_address if sys.platform == 'win32': addr = arbitrary_address('AF_PIPE') else: addr = arbitrary_address('AF_UNIX') supp_server = os.path.join(os.path.dirname(__file__), 'server.py') args = [self.executable, supp_server, addr] env = os.environ.copy() if self.env: env.update(self.env) if self.logfile and 'SUPP_LOG_FILE' not in env: env['SUPP_LOG_FILE'] = self.logfile self.proc = Popen(args, env=env) start = time.time() while True: try: self.conn = Client(addr) except Exception as e: if time.time() - start > 5: raise Exception('Supp server launching timeout exceed: ' + str(e)) time.sleep(0.3) else: break def _threaded_run(self): try: self._run() finally: self.prepare_thread = None def prepare(self): with self.prepare_lock: if self.prepare_thread: return if hasattr(self, 'conn'): return self.prepare_thread = Thread(target=self._threaded_run) self.prepare_thread.start() def run(self): with self.prepare_lock: if self.prepare_thread: self.prepare_thread.join() if not hasattr(self, 'conn'): self._run() def _call(self, name, *args, **kwargs): try: self.conn except AttributeError: self.run() self.conn.send_bytes(dumps((name, args, kwargs))) result, is_ok = loads(self.conn.recv_bytes()) if is_ok: return result else: raise Exception(result[1]) def lint(self, source, filename, syntax_only=False): return self._call('lint', source, filename, syntax_only) def assist(self, source, position, filename): """Return completion match and list of completion proposals :param source: code source :param position: tuple of (line, column) :param filename: absolute path of file with source code :returns: tuple (completion match, sorted list of proposals) """ return self._call('assist', source, position, filename) def location(self, source, position, filename): """Return position and file path where name under cursor is defined If position is None location wasn't finded. If file path is None, defenition is located in the same source. :param source: code source :param position: tuple of (line, column) :param filename: absolute path of file with source code :returns: tuple ((line, column), file path) """ return self._call('location', source, position, filename) # def get_docstring(self, project_path, source, position, filename): # """Return signature and docstring for current cursor call context # Some examples of call context:: # func(| # func(arg| # func(arg,| # func(arg, func2(| # call context is func2 # Signature and docstring can be None # :param project_path: absolute project path # :param source: unicode or byte string code source # :param position: character or byte cursor position # :param filename: absolute path of file with source code # :returns: tuple (signarure, docstring) # """ # return self._call('get_docstring', project_path, source, position, filename) def configure(self, config): """Reconfigure project :param config: dict with config key/values """ return self._call('configure', config) # def get_scope(self, project_path, source, lineno, filename, continous=True): # """ # Return scope name at cursor position # For example:: # class Foo: # def foo(self): # pass # | # def bar(self): # pass # get_scope return Foo.foo if continuous is True and Foo otherwise. # :param project_path: absolute project path # :param source: unicode or byte string code source # :param position: character or byte cursor position # :param filename: absolute path of file with source code # :param continous: allow parent scope beetween children if False # """ # return self._call('get_scope', project_path, source, lineno, filename, continous=continous) def eval(self, source): return self._call('eval', source) def close(self): """Shutdown server""" try: self.conn except AttributeError: pass else: self.conn.send_bytes(dumps(('close', (), {}), 2)) self.conn.close() del self.conn
from multiprocessing.connection import Client import sys conn = Client(address=("", 6000)) while True: line = sys.stdin.readline() if not line.strip(): break conn.send_bytes(line) reply = conn.recv_bytes() print "Reply: ", reply conn.close()
class Wdb(object): """Wdb debugger main class""" _instances = {} _sockets = [] enabled = True breakpoints = set() watchers = defaultdict(list) @staticmethod def get(no_create=False): """Get the thread local singleton""" pid = os.getpid() thread = threading.current_thread() wdb = Wdb._instances.get((pid, thread)) if not wdb and not no_create: wdb = object.__new__(Wdb) Wdb.__init__(wdb) wdb.pid = pid wdb.thread = thread Wdb._instances[(pid, thread)] = wdb return wdb @staticmethod def pop(): """Remove instance from instance list""" pid = os.getpid() thread = threading.current_thread() Wdb._instances.pop((pid, thread)) def __new__(cls): return cls.get() def __init__(self): log.debug('New wdb instance %r' % self) self.obj_cache = {} self.tracing = False self.begun = False self.connected = False self.stepping = False self.extra_vars = {} self.last_obj = None self.reset() self.uuid = str(uuid4()) self.state = Running(None) self.full = False self.below = False self._socket = None def run_file(self, filename): """Run the file `filename` with trace""" import __main__ __main__.__dict__.clear() __main__.__dict__.update({ "__name__": "__main__", "__file__": filename, "__builtins__": __builtins__, }) with open(filename, "rb") as fp: statement = "exec(compile(%r, %r, 'exec'))" % (fp.read(), filename) self.run(statement, filename) def run(self, cmd, fn=None, globals=None, locals=None): """Run the cmd `cmd` with trace""" if globals is None: import __main__ globals = __main__.__dict__ if locals is None: locals = globals self.reset() if isinstance(cmd, str): cmd = compile(cmd, fn or "<wdb>", "exec") self.start_trace() self.breakpoints.add(Breakpoint(fn, temporary=True)) try: execute(cmd, globals, locals) finally: self.stop_trace() def reset(self): """Refresh linecache""" import linecache linecache.checkcache() def connect(self): """Connect to wdb server""" log.info('Connecting socket on %s:%d' % (SOCKET_SERVER, SOCKET_PORT)) tries = 0 while not self._socket and tries < 10: try: self._socket = Client((SOCKET_SERVER, SOCKET_PORT)) except socket.error: tries += 1 log.warning('You must start wdb.server. ' '(Retrying on %s:%d) [Try #%d]' % (SOCKET_SERVER, SOCKET_PORT, tries)) if not self._socket: log.error('Could not connect to server') sys.exit(2) Wdb._sockets.append(self._socket) self._socket.send_bytes(self.uuid.encode('utf-8')) def trace_dispatch(self, frame, event, arg): """This function is called every line, function call, function return and exception during trace""" fun = getattr(self, 'handle_' + event) if fun and ((event == 'line' and self.breaks(frame)) or (event == 'exception' and (self.full or frame == self.state.frame or (self.below and frame.f_back == self.state.frame))) or self.state.stops(frame, event)): fun(frame, arg) if event == 'return' and frame == self.state.frame: # Upping state if self.state.up(): # No more frames self.stop_trace() return # Threading / Multiprocessing support co = self.state.frame.f_code if ((co.co_filename.endswith('threading.py') and co.co_name.endswith('_bootstrap_inner')) or (self.state.frame.f_code.co_filename.endswith( os.path.join('multiprocessing', 'process.py')) and self.state.frame.f_code.co_name == '_bootstrap')): # Thread / Process is dead self.stop_trace() self.die() return if (event == 'call' and not self.stepping and not self.full and not (self.below and frame.f_back == self.state.frame) and not self.get_file_breaks(frame.f_code.co_filename)): # Don't trace anymore here return return self.trace_dispatch def trace_debug_dispatch(self, frame, event, arg): """Utility function to add debug to tracing""" trace_log.info('Frame:%s. Event: %s. Arg: %r' % (pretty_frame(frame), event, arg)) trace_log.debug('state %r breaks ? %s stops ? %s' % (self.state, self.breaks(frame, no_remove=True), self.state.stops(frame, event))) if event == 'return': trace_log.debug( 'Return: frame: %s, state: %s, state.f_back: %s' % (pretty_frame(frame), pretty_frame( self.state.frame), pretty_frame(self.state.frame.f_back))) if self.trace_dispatch(frame, event, arg): return self.trace_debug_dispatch trace_log.debug("No trace %s" % pretty_frame(frame)) def start_trace(self, full=False, frame=None, below=False): """Start tracing from here""" if self.tracing: return self.reset() log.info('Starting trace on %r' % self.thread) frame = frame or sys._getframe().f_back # Setting trace without pausing self.set_trace(frame, break_=False) self.tracing = True self.below = below self.full = full def set_trace(self, frame=None, break_=True): """Break at current state""" # We are already tracing, do nothing trace_log.info('Setting trace %s (stepping %s) (current_trace: %s)' % (pretty_frame(frame or sys._getframe().f_back), self.stepping, sys.gettrace())) if self.stepping: return self.reset() trace = (self.trace_dispatch if trace_log.level >= 30 else self.trace_debug_dispatch) trace_frame = frame = frame or sys._getframe().f_back while frame: frame.f_trace = trace frame = frame.f_back self.state = Step(trace_frame) if break_ else Running(trace_frame) sys.settrace(trace) def stop_trace(self, frame=None): """Stop tracing from here""" self.tracing = False self.full = False frame = frame or sys._getframe().f_back while frame: del frame.f_trace frame = frame.f_back sys.settrace(None) log.info('Stopping trace on %r' % self.thread) def set_until(self, frame, lineno=None): """Stop on the next line number.""" self.state = Until(frame, frame.f_lineno) def set_step(self, frame): """Stop on the next line.""" self.state = Step(frame) def set_next(self, frame): """Stop on the next line in current frame.""" self.state = Next(frame) def set_return(self, frame): """Stop when returning from the given frame.""" self.state = Return(frame) def set_continue(self, frame): """Don't stop anymore""" self.state = Running(frame) if not self.tracing and not self.breakpoints: # If we were in a set_trace and there's no breakpoint to trace for # Run without trace self.stop_trace() def get_break(self, filename, lineno, temporary, cond, funcname): if lineno and not cond: return LineBreakpoint(filename, lineno, temporary) elif cond: return ConditionalBreakpoint(filename, lineno, cond, temporary) elif funcname: return FunctionBreakpoint(filename, funcname, temporary) else: return Breakpoint(filename, temporary) def set_break(self, filename, lineno=None, temporary=False, cond=None, funcname=None): """Put a breakpoint for filename""" log.info('Setting break fn:%s lno:%s tmp:%s cond:%s fun:%s' % (filename, lineno, temporary, cond, funcname)) breakpoint = self.get_break(filename, lineno, temporary, cond, funcname) self.breakpoints.add(breakpoint) if not temporary: self._socket.send_bytes(b'Server|AddBreak|' + pickle.dumps(breakpoint)) log.info('Breakpoint %r added' % breakpoint) def clear_break(self, filename, lineno=None, temporary=False, cond=None, funcname=None): """Remove a breakpoint""" log.info('Removing break fn:%s lno:%s tmp:%s cond:%s fun:%s' % (filename, lineno, temporary, cond, funcname)) breakpoint = self.get_break(filename, lineno, temporary or False, cond, funcname) if temporary is None and breakpoint not in self.breakpoints: breakpoint = self.get_break(filename, lineno, True, cond, funcname) try: self.breakpoints.remove(breakpoint) if not temporary: self._socket.send_bytes(b'Server|RmBreak|' + pickle.dumps(breakpoint)) log.info('Breakpoint %r removed' % breakpoint) except: log.info('Breakpoint %r not removed: not found' % breakpoint) def safe_repr(self, obj): """Like a repr but without exception""" try: return repr(obj) except Exception as e: return '??? Broken repr (%s: %s)' % (type(e).__name__, e) def safe_better_repr(self, obj): """Repr with inspect links on objects""" try: rv = self.better_repr(obj) except Exception: rv = None if rv: return rv self.obj_cache[id(obj)] = obj return '<a href="%d" class="inspect">%s</a>' % (id(obj), escape(repr(obj))) def better_repr(self, obj): """Repr with html decorations""" if isinstance(obj, dict): if type(obj) != dict: dict_repr = type(obj).__name__ + '({' closer = '})' else: dict_repr = '{' closer = '}' if len(obj) > 2: dict_repr += '<table>' dict_repr += ''.join([ '<tr><td>' + self.safe_repr(key) + '</td><td>:</td>' '<td>' + self.safe_better_repr(val) + '</td></tr>' for key, val in sorted(obj.items(), key=lambda x: x[0]) ]) dict_repr += '</table>' else: dict_repr += ', '.join([ self.safe_repr(key) + ': ' + self.safe_better_repr(val) for key, val in sorted(obj.items(), key=lambda x: x[0]) ]) dict_repr += closer return dict_repr if any([ isinstance(obj, list), isinstance(obj, set), isinstance(obj, tuple) ]): if type(obj) == list: iter_repr = '[' closer = ']' elif type(obj) == set: iter_repr = '{' closer = '}' elif type(obj) == tuple: iter_repr = '(' closer = ')' else: iter_repr = escape(obj.__class__.__name__) + '([' closer = '])' splitter = ', ' if len(obj) > 2: splitter += '\n' iter_repr += '\n' closer = '\n' + closer iter_repr += splitter.join( [self.safe_better_repr(val) for val in obj]) iter_repr += closer return iter_repr @contextmanager def capture_output(self, with_hook=True): """Steal stream output, return them in string, restore them""" self.hooked = '' def display_hook(obj): # That's some dirty hack self.hooked += self.safe_better_repr(obj) self.last_obj = obj stdout, stderr = sys.stdout, sys.stderr if with_hook: d_hook = sys.displayhook sys.displayhook = display_hook sys.stdout, sys.stderr = StringIO(), StringIO() out, err = [], [] try: yield out, err finally: out.extend(sys.stdout.getvalue().splitlines()[1:]) err.extend(sys.stderr.getvalue().splitlines()) if with_hook: sys.displayhook = d_hook sys.stdout, sys.stderr = stdout, stderr def dmp(self, thing): """Dump the content of an object in a dict for wdb.js""" def safe_getattr(key): """Avoid crash on getattr""" try: return getattr(thing, key) except Exception as e: return 'Error getting attr "%s" from "%s" (%s: %s)' % ( key, thing, type(e).__name__, e) return dict((escape(key), { 'val': self.safe_better_repr(safe_getattr(key)), 'type': type(safe_getattr(key)).__name__ }) for key in dir(thing)) def get_file(self, filename): """Get file source from cache""" import linecache return to_unicode_string(''.join(linecache.getlines(filename)), filename) def get_stack(self, f, t): """Build the stack from frame and traceback""" stack = [] if t and t.tb_frame == f: t = t.tb_next while f is not None: stack.append((f, f.f_lineno)) f = f.f_back stack.reverse() i = max(0, len(stack) - 1) while t is not None: stack.append((t.tb_frame, t.tb_lineno)) t = t.tb_next if f is None: i = max(0, len(stack) - 1) return stack, i def get_trace(self, frame, tb): """Get a dict of the traceback for wdb.js use""" import linecache frames = [] stack, _ = self.get_stack(frame, tb) current = 0 for i, (stack_frame, lno) in enumerate(stack): if frame == stack_frame: current = i break for i, (stack_frame, lno) in enumerate(stack): code = stack_frame.f_code filename = code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lno, stack_frame.f_globals) line = to_unicode_string(line, filename) line = line and line.strip() startlnos = dis.findlinestarts(code) lastlineno = list(startlnos)[-1][1] frames.append({ 'file': filename, 'function': code.co_name, 'flno': code.co_firstlineno, 'llno': lastlineno, 'lno': lno, 'code': line, 'level': i, 'current': current == i }) # While in exception always put the context to the top current = len(frames) - 1 return stack, frames, current def send(self, data): """Send data through websocket""" log.debug('Sending %s' % data) self._socket.send_bytes(data.encode('utf-8')) def receive(self, pickled=False): """Receive data through websocket""" log.debug('Receiving') data = self._socket.recv_bytes() if pickled: data = pickle.loads(data) log.debug('Got pickled %r' % data) return data log.debug('Got %s' % data) return data.decode('utf-8') def interaction(self, frame, tb=None, exception='Wdb', exception_description='Stepping', init=None): """User interaction handling blocking on socket receive""" log.info('Interaction for %r -> %r %r %r %r' % (self.thread, frame, tb, exception, exception_description)) self.stepping = True if not self.connected: self.connect() log.debug('Launching browser and wait for connection') web_url = 'http://%s:%d/debug/session/%s' % ( WEB_SERVER or 'localhost', WEB_PORT or 1984, self.uuid) server = WEB_SERVER or '[wdb.server]' if WEB_PORT: server += ':%s' % WEB_PORT if WDB_NO_BROWSER_AUTO_OPEN: log.warning('You can now launch your browser at ' 'http://%s/debug/session/%s' % (server, self.uuid)) elif not webbrowser.open(web_url): log.warning('Unable to open browser, ' 'please go to http://%s/debug/session/%s' % (server, self.uuid)) self.connected = True interaction = Interaction(self, frame, tb, exception, exception_description, init=init) # For meta debugging purpose self._ui = interaction if self.begun: # Each new state sends the trace and selects a frame interaction.init() else: self.begun = True interaction.loop() def handle_call(self, frame, argument_list): """This method is called when there is the remote possibility that we ever need to stop in this function.""" fun = frame.f_code.co_name log.info('Calling: %r' % fun) init = 'Echo|%s' % dump({'for': '__call__', 'val': fun}) self.interaction(frame, init=init, exception_description='Calling %s' % fun) def handle_line(self, frame, arg): """This function is called when we stop or break at this line.""" log.info('Stopping at line %s' % pretty_frame(frame)) self.interaction(frame) def handle_return(self, frame, return_value): """This function is called when a return trap is set here.""" self.obj_cache[id(return_value)] = return_value self.extra_vars['__return__'] = return_value fun = frame.f_code.co_name log.info('Returning from %r with value: %r' % (fun, return_value)) init = 'Echo|%s' % dump({'for': '__return__', 'val': return_value}) self.interaction( frame, init=init, exception_description='Returning from %s with value %s' % (fun, return_value)) def handle_exception(self, frame, exc_info): """This function is called if an exception occurs, but only if we are to stop at or just below this level.""" type_, value, tb = exc_info # Python 3 is broken see http://bugs.python.org/issue17413 _value = value if not isinstance(_value, BaseException): _value = type_(value) fake_exc_info = type_, _value, tb log.error('Exception during trace', exc_info=fake_exc_info) self.obj_cache[id(exc_info)] = exc_info self.extra_vars['__exception__'] = exc_info exception = type_.__name__ exception_description = str(value) init = 'Echo|%s' % dump( { 'for': '__exception__', 'val': escape('%s: %s') % (exception, exception_description) }) # User exception is 4 frames away from exception log.warning(pretty_frame(frame)) frame = frame or sys._getframe().f_back.f_back.f_back.f_back self.interaction(frame, tb, exception, exception_description, init=init) def breaks(self, frame, no_remove=False): """Return True if there's a breakpoint at frame""" for breakpoint in set(self.breakpoints): if breakpoint.breaks(frame): if breakpoint.temporary and not no_remove: self.breakpoints.remove(breakpoint) return True return False def get_file_breaks(self, filename): """List all file `filename` breakpoints""" return [ breakpoint for breakpoint in self.breakpoints if breakpoint.on_file(filename) ] def get_breaks_lno(self, filename): """List all line numbers that have a breakpoint""" return list( filter(lambda x: x is not None, [ getattr(breakpoint, 'line', None) for breakpoint in self.breakpoints if breakpoint.on_file(filename) ])) def die(self): """Time to quit""" log.info('Time to die') if self.connected: try: self.send('Die') except: pass self._socket.close() self.pop()
def order(self, command): conn = Client(self.adress, authkey=self.authkey) conn.send_bytes(command.encode()) conn.close()
class RemoteGdb(object): def __init__(self, vim, host, port): self.vim = vim self.sock = Client((host, port)) self.request_id = 0 self.response = {} def send_command(self, **kwargs): self.request_id += 1 try: self.sock.send_bytes(json.dumps(dict(dict({'dest': 'gdb'}, **kwargs), request_id=self.request_id)).encode('utf-8')) except IOError: print "Broken pipe encountered sending to the proxy. Terminating Exterminator." self.quit() return self.request_id def handle_events(self): if not self.sock.poll(): return while True: try: c = json.loads(self.sock.recv_bytes().decode('utf-8')) except (IOError, EOFError): print "Lost connection to GDB" self.quit() return if c['op'] == 'goto': window = self.find_window('navigation') if window is None: self.claim_window('navigation') c['filename'] = os.path.abspath(c['filename']) self.vim.command('badd %(filename)s' % c) self.vim.command("buffer %(filename)s" % c) self.vim.command("%(line)s" % c) self.vim.command("%(line)skP" % c) self.vim.command("norm zz") elif c['op'] == 'disp': winnr = int(self.vim.eval("winnr()")) window = self.find_window('display', 'bot 15new') self.vim.command("setlocal buftype=nowrite bufhidden=wipe modifiable nobuflisted noswapfile nowrap nonumber") contents = [ c['expr'], c['contents'] ] self.vim.current.window.buffer[:] = contents self.vim.command("setlocal nomodifiable") self.vim.command("%swincmd w" % winnr) elif c['op'] == 'response': self.response[c['request_id']] = c elif c['op'] == 'refresh': GDBPlugin = self.vim.bindeval('g:NERDTreeGDBPlugin') NERDTreeFromJSON = self.vim.Function('NERDTreeFromJSON') NERDTreeFromJSON(c['expr'], GDBPlugin) elif c['op'] == 'place': c['filename'] = os.path.abspath(c['filename']) self.vim.command("badd %s" % c['filename'].replace('$', '\\$')) c['bufnr'] = self.vim.eval("bufnr('%(filename)s')" % c) self.vim.command("sign place %(num)s name=%(name)s line=%(line)s buffer=%(bufnr)s" % c) elif c['op'] == 'replace': self.vim.command("sign place %(num)s name=%(name)s file=%(filename)s" % c) elif c['op'] == 'unplace': self.vim.command("sign unplace %(num)s" % c) elif c['op'] == 'quit': self.quit() return if not self.sock.poll(): return def quit(self): self.vim.command("sign unplace *") winnr = int(self.vim.eval("winnr()")) window = self.find_window('display') if window is not None: self.vim.command("q") self.vim.command("%swincmd w" % winnr) self.vim.gdb = None try: self.send_command(dest='proxy', op='quit') except: pass def send_trap(self): self.send_command(dest='proxy', op='trap') def send_quit(self): self.send_command(op='quit') def send_continue(self): self.send_command(op='go') self.send_trap() def send_exec(self, comm): self.send_command(op='exec', comm=comm) self.send_trap() def disable_break(self, filename, line): self.send_command(op='disable', loc=(filename, line)) self.send_trap() def toggle_break(self, filename, line): self.send_command(op='toggle', loc=(filename, line)) self.send_trap() def continue_until(self, filename, line): self.send_command(op='until', loc=(filename, line)) self.send_trap() def eval_expr(self, expr): request_id = self.send_command(op='eval', expr=str(expr)) return self.get_response(request_id) def get_response(self, request_id): start = time.time() while request_id not in self.response: self.send_trap() try: select.select([self.sock], [], [], 1) self.handle_events() except select.error: pass if time.time() - start > 5: return None response = self.response[request_id] del self.response[request_id] return response def fetch_children(self, expr): try: v = self.eval_expr(expr) return [ v['expr'], v['contents'] ] except: import traceback lines = traceback.format_exc().split('\n') p = len("%d" % len(lines)) return [ "Python client error", { "%0*d: %s" % (p, i, line): {} for i, line in enumerate(lines) } ] def track_expr(self, expr): if expr is not None: GDBPlugin = self.vim.bindeval('g:NERDTreeGDBPlugin') NERDTreeFromJSON = self.vim.Function('NERDTreeFromJSON') NERDTreeFromJSON(expr, GDBPlugin) self.send_command(op='track', expr=expr) def show_backtrace(self): request_id = self.send_command(op='bt') response = self.get_response(request_id) my_llist = self.vim.List([{ 'filename': filename, 'lnum': line, 'text': contents } for filename, line, contents in response['bt'] ]) setloclist = self.vim.Function('setloclist') setloclist(0, my_llist) self.vim.command('lopen') self.vim.command('GdbBindBufferToFrame') def claim_window(self, window_name): self.vim.command('let w:mandrews_output_window = "%s"' % window_name) def find_window(self, window_name, new_command=None): winnr = int(self.vim.eval("winnr()")) while True: if int(self.vim.eval("exists('w:mandrews_output_window')")) > 0: if str(self.vim.eval("w:mandrews_output_window")) == window_name: break self.vim.command("wincmd w") if winnr == int(self.vim.eval("winnr()")): if new_command is not None: self.vim.command(new_command) self.claim_window(window_name) break return return self.vim.current.window
class MicroControlClient: authkey = b'barracuda' server = None # subprocess.Popen object conn = None lock = threading.Lock() def __init__(self, port=0): if port == 0: port = get_free_port() self.address = ('localhost', port) #self.start_server() def start_connection(self): time.sleep(1) with self.lock: self.conn = Client(self.address, authkey=b'barracuda') def send_command(self, cmd): with self.lock: self.conn.send_bytes(pickle.dumps(cmd, 2)) def read_response(self): with self.lock: time.sleep(WAIT_TIME) response = self.conn.recv_bytes() response = pickle.loads(response, encoding='bytes') time.sleep(WAIT_TIME) return response def close_server(self): with self.lock: self.conn.send_bytes(pickle.dumps('close',2)) self.conn.close() self.server.terminate() def start_server(self): """ Opens the python2 subprocess that will run the server and micromanager code. :return: """ print(SERVER_FILE) with self.lock: self.server = subprocess.Popen([PYTHON2_PATH, SERVER_FILE, '{}'.format(self.address[1])], stdout=subprocess.PIPE, stdin=subprocess.PIPE) time.sleep(1) def open(self): """ Opens the Python 2 server and starts the connection""" if self.conn is None: self.start_server() self.start_connection() elif self.conn.closed: self.start_server() self.start_connection() return True def close(self): self.close_server() return True @staticmethod def ok_check(response,msg): """ Checks the response if it was recieved OK.""" if str(response.decode())!= 'Ok': logging.error('{}. Recieved: {}'.format(msg,response)) return False return True
#!/usr/bin/env python2 from multiprocessing.connection import Client address = ('localhost', 6000) conn = Client(address, authkey='dp23y4fm') conn.send_bytes('stop') conn.close()