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 mktap telnet -p 4040 -u admin -w admin
at
your shell prompt. If you list the contents of your current directory,
you'll notice a new file -- telnet.tap
. After you do this, run
twistd -f telnet.tap
. Since the Application has a
telnet server that you specified to be on port 4040, it will start listening
for connections on this port. Try connecting with your favorite telnet
utility to 127.0.0.1 port 4040.
$ telnet localhost 4040 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. twisted.manhole.telnet.ShellFactory 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() ['__builtins__']
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 in this tap.
As you can see, this is quite useful - we can introspect a
running process, see the internal objects, and even change
their attributes. We can add telnet support to existing
tap like so: mktap --append=foo.tap telnet -p 4040 -u user -w pass
.
The telnet server can of coursed be used from straight Python code as well. 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 mktap
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.