Пример #1
0
 def run(self):
     address = ("localhost", 6000)
     conn = Client(address, authkey="secret password")
     while True:
         command = conn.recv_bytes()
         if command == "quit":
             break
         self.animations[:] = parse_command(command, self.boxs, "receiver")
     conn.close()
Пример #2
0
 def get_rps_from_pycounters():
     # gets the "request per second" data from counter server via
     # multiprocessing Client from Listener which runs in a thread
     # fired from possibly another webserver process
     try:
         rps_client = Client("/tmp/rpssock")
         rps = rps_client.recv_bytes()
         rps_client.close()
     except:
         raise
         rps = 0
     return rps
Пример #3
0
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()
Пример #4
0
class ServiceConnect:
  def __delete__(self):
    if self.conn:
      self.conn.close()

  def conntect(self, host="localhost", port=6000):
    self.conn = Client((host, port))

  def request(self, req):
    self.conn.send(req)
    items = self.conn.recv_bytes()
    result = json.loads(items)
    return result
Пример #5
0
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
Пример #6
0
    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
        }
Пример #7
0
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!')
Пример #8
0
    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': {},
               }
Пример #9
0
 def run(self, cmd):
     '''
     传递命令给server, 同时展示传回信息.
     @ cmd: 命令, 以tab分割.
     '''
     conn = MultiClient(
         (config.hostname, config.port),
         authkey=config.authkey,
     )
     # 传送命令
     conn.send(cmd)
     try:
         # 收取信息
         recv_data = conn.recv_bytes()
     except EOFError:
         pass
     else:
         # 传回的数据是unicode编码的
         unicode_recv_data = eval(recv_data)
         # 按照设定编码print反馈数据
         print unicode_recv_data.encode(config.client_code)
     finally:
         conn.close()
Пример #10
0
def setMSGtoListener(msg):
    address = ('localhost', 6000)
    conn = Client(address, authkey='secret password')
    conn.send(msg)
    print conn.recv_bytes()
    conn.close()
Пример #11
0
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()
Пример #12
0
def client(msg):
    conn = Client(address, authkey=b'mypassword')
    conn.send(msg)
    print (conn.recv())
    print (conn.recv_bytes())
    conn.close()
from multiprocessing.connection import Client
from array import array

address = ('localhost', 6000)
conn = Client(address, authkey='secret password')

print conn.recv()                 # => [2.25, None, 'junk', float]

print conn.recv_bytes()            # => 'hello'

arr = array('i', [0, 0, 0, 0, 0])
print conn.recv_bytes_into(arr)     # => 8
print arr                         # => array('i', [42, 1729, 0, 0, 0])

conn.close()
Пример #14
0
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
Пример #15
0
from multiprocessing.connection import Client
from array import array

address = ('localhost', 6000)
conn = Client(address, authkey='secret password')

print conn.recv()  # => [2.25, None, 'junk', float]

print conn.recv_bytes()  # => 'hello'

arr = array('i', [0, 0, 0, 0, 0])
print conn.recv_bytes_into(arr)  # => 8
print arr  # => array('i', [42, 1729, 0, 0, 0])

conn.close()
Пример #16
0
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()


Пример #17
0
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()
Пример #18
0
#m = QueueManager(address=(HOST, PORT), authkey=b'abc')
#m.connect()
#fun = m.get_class()
#
#print(fun('comm_connect',0))


from multiprocessing.connection import Client
from array import array

address = ('localhost', 6000)
conn = Client(address, authkey=b'abc')

print (conn.recv())                 # => [2.25, None, 'junk', float])

print( conn.recv_bytes()    )        # => 'hello'

arr = array('i', [0, 0, 0, 0, 0])
#print (conn.recv_bytes_into(arr))     # => 8
print (arr     )                    # => array('i', [42, 1729, 0, 0, 0])

conn.close()


#queue.put('hello')
#print(kiwoom.aa())



#  Test Code
#kiwoom = Kiwoom()
Пример #19
0
# -*- coding: utf-8 -*-

from multiprocessing.connection import Client

address = ('localhost', 8000)

for x in range(0,5):
    conn = Client(address, authkey='secret password')
    conn.send('这是一个美丽的世界')
    print conn.recv_bytes()

    conn.close()
Пример #20
0
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
Пример #21
0
def client(msg):
    conn = Client(address, authkey=b'mypassword')
    conn.send(msg)
    print(conn.recv())
    print(conn.recv_bytes())
    conn.close()
Пример #22
0
#!/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()

Пример #23
0
 def work(self):
     conn = Client(self.address, authkey=self.password)
     print conn.recv_bytes()
     conn.close()
Пример #24
0
from multiprocessing.connection import Client

conn = Client(address=("", 6000))

data = conn.recv_bytes()
print data

obj = conn.recv()
print obj

conn.close()

Пример #25
0
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
Пример #26
0
 def _get_conn(self):
     conn_in = Client(self.address, authkey=b'testingToms')
     if self.strat == 'pipe':
         return conn_in.recv()
     buf = io.BytesIO(conn_in.recv_bytes())
     return pickle.load(buf)