~certify-web-dev/twisted/certify-trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html  PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
<title>Twisted Documentation: Using telnet to manipulate a twisted server</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
  </head>

  <body bgcolor="white">
    <h1 class="title">Using telnet to manipulate a twisted server</h1>
    <div class="toc"><ol/></div>
    <div class="content">
<span/>

<p>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.</p>

<p>Run <code class="shell">twistd telnet -p 4040 -u admin -w admin</code> 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 127.0.0.1 port 4040.</p>

<pre class="shell" xml:space="preserve">
$ <em>telnet localhost 4040</em>
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

twisted.manhole.telnet.ShellFactory
Twisted 1.1.0
username: <em>admin</em>
password: <em>admin</em>
&gt;&gt;&gt;
</pre>

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

<pre class="python-interpreter" xml:space="preserve">
&gt;&gt;&gt; <em>dir()</em>
['__builtins__']
</pre>

<p>Ok, not much. let's play a little more:</p>
<pre class="python-interpreter" xml:space="preserve">
&gt;&gt;&gt; <em>import __main__</em>
&gt;&gt;&gt; <em>dir(__main__)</em>
['__builtins__', '__doc__', '__name__', 'os', 'run', 'string', 'sys']

&gt;&gt;&gt; <em>service</em>
&lt;twisted.application.internet.TCPServer instance at 0x10270f48&gt;
&gt;&gt;&gt; <em>service._port</em>
&lt;twisted.manhole.telnet.ShellFactory on 4040&gt;
&gt;&gt;&gt; <em>service.parent</em>
&lt;twisted.application.service.MultiService instance at 0x1024d7a8&gt;
</pre>

<p>The service object is the service used to serve the telnet shell,
and that it is listening on port 4040 with something called a
<code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.manhole.telnet.ShellFactory.html" title="twisted.manhole.telnet.ShellFactory">ShellFactory</a></code>. 
Its parent is a <code class="python">twisted.application.service.MultiService</code>,
a collection of services. We can keep getting the parent attribute
of services until we hit the root of all services.</p>

<p>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 
<code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.tap.telnet.html" title="twisted.tap.telnet">twisted.tap.telnet</a></code>.</p>

<p>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 <code class="shell">twistd
telnet -p ssl:443:privateKey=mykey.pem:certKey=cert.pem -u admin -w
admin</code>.  See <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.application.strports.html" title="twisted.application.strports">twisted.application.strports</a></code> for more examples of
options for listening on a port.</p>

</div>

    <p><a href="index.html">Index</a></p>
    <span class="version">Version: 10.0.0</span>
  </body>
</html>