Wiki

Clone wiki

rfoo / Home

rfoo

Pure python (cython NOT required) version of rfoo rpc/remote shell http://code.google.com/r/clach04-rfoo-purepython/source/browse

Branches of interest:

Server

"""Simple remote Python shell server"""

import sys
import os
import time
import logging
try:
    import thread
except ImportError:
    import _thread as thread


import rfoo
import rfoo.utils.rconsole as rconsole



def myspawn_server(namespace=None, port=rconsole.PORT):
    """Start console server in a new thread.
    Should be called from global scope only!
    May be insecure on shared machines.
    """

    logging.info('Enter, port=%d.', port)

    if namespace is None:
        namespace = sys._getframe(1).f_globals

    server = rfoo.InetServer(rconsole.ConsoleHandler, namespace)

    def _wrapper():
        try:
            server.start('0.0.0.0', port)
        except socket.error:
            logging.warning('Failed to bind rconsole to socket port, port=%r.', port)

    thread.start_new_thread(_wrapper, ())


rconsole.spawn_server()
#myspawn_server()



while 1:
    time.sleep(1)

Client

#! /usr/bin/env python
"""
    rconsole

    A Python console you can embed in a program and attach to remotely.

    To spawn a Python console in a script do the following in global scope
    only (!) of any module:

    from rfoo.utils import rconsole
    rconsole.spawn_server()

    This will start a listener for connections in a new thread. You may
    specify a port to listen on.

    To attach to the console from another shell use the rconsole script
    (rfoo/scripts/rconsole).

    SECURITY NOTE:
    The listener started with spawn_server() will accept any local 
    connection and may therefore be insecure to use in shared hosting
    or similar environments!
"""


import getopt
import sys
import os

import rfoo.utils.rconsole as rconsole


def print_usage():
    scriptName = os.path.basename(sys.argv[0])
    sys.stdout.write("""
Start remote console:
%(name)s [-h] [-pPORT]

-h, --help  Print this help.
-pPORT      Set PORT.
""" % {'name': scriptName})


def main():
    """Parse options and run script."""

    try:
        options, args = getopt.getopt(
            sys.argv[1:], 
            'hp:', 
            ['help']
            )
        options = dict(options)

    except getopt.GetoptError:
        print_usage()
        return 2

    if '-h' in options or '--help' in options:
        print_usage()
        return

    if '-p' in options:
        port = int(options.get('-p'))
    else:
        port = rconsole.PORT

    host = args[0]
    print host
    print args
    rconsole.interact(host=host, port=port)


if __name__ == '__main__':
    main()

Updated