Using telnet to manipulate a twisted server

    To start things off, we're going to create a simple server that just gives you remote access to a Python interpreter. We will use a telnet client to access this server.

    Run twistd telnet -p 4040 -u admin -w admin at your shell prompt. The Application has a telnet server that you specified to be on port 4040, and it will start listening for connections on this port. Try connecting with your favorite telnet utility to port 4040.

    $ telnet localhost 4040
    Connected to localhost.
    Escape character is '^]'.
    Twisted 1.1.0
    username: admin
    password: admin

    Now, you should see a Python prompt -- >>>. You can type any valid Python code here. Let's try looking around.

    >>> dir()

    Ok, not much. let's play a little more:

    >>> import __main__
    >>> dir(__main__)
    ['__builtins__', '__doc__', '__name__', 'os', 'run', 'string', 'sys']
    >>> service
    <twisted.application.internet.TCPServer instance at 0x10270f48>
    >>> service._port
    <twisted.manhole.telnet.ShellFactory on 4040>
    >>> service.parent
    <twisted.application.service.MultiService instance at 0x1024d7a8>

    The service object is the service used to serve the telnet shell, and that it is listening on port 4040 with something called a ShellFactory. Its parent is a twisted.application.service.MultiService, a collection of services. We can keep getting the parent attribute of services until we hit the root of all services.

    As you can see, this is quite useful - we can introspect a running process, see the internal objects, and even change their attributes. The telnet server can of course be used from straight Python code; you can see how to do this by reading the code for twisted.tap.telnet.

    A final note - if you want access to be more secure, you can even have the telnet server use SSL. Assuming you have the appropriate certificate and private key files, you can twistd telnet -p ssl:443:privateKey=mykey.pem:certKey=cert.pem -u admin -w admin. See twisted.application.strports for more examples of options for listening on a port.


    Version: 10.0.0