Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > e4be28b383be195ff28bfce2053e734a > files > 281

python-stem-doc-1.1.0-1.fc18.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>East of the Sun &amp; West of the Moon &mdash; Stem 1.1.0 documentation</title>
    
    <link rel="stylesheet" href="../_static/haiku.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/print.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/theme_extras.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.png"/>
    <link rel="top" title="Stem 1.1.0 documentation" href="../index.html" />
    <link rel="up" title="Contents" href="../contents.html" />
    <link rel="next" title="Mirror Mirror on the Wall" href="mirror_mirror_on_the_wall.html" />
    <link rel="prev" title="Tortoise and the Hare" href="tortoise_and_the_hare.html" /> 
  </head>
  <body>
      <div class="header"><img class="rightlogo" src="../_static/logo.png" alt="Logo"/><h1 class="heading"><a href="../index.html">
          <span>Stem Docs</span></a></h1>
        <h2 class="heading"><span>East of the Sun &amp; West of the Moon</span></h2>
      </div>
      <div class="topnav">
      
        <p>

        <ul id="navbar">
          <li><a href="../index.html">Home</a></li>
          <li><a href="../tutorials.html">Tutorials</a>
            <ul>
              <li><a href="the_little_relay_that_could.html">Hello World</a></li>
              <li><a href="to_russia_with_love.html">Client Usage</a></li>
              <li><a href="tortoise_and_the_hare.html">Event Listening</a></li>
              <li><a href="mirror_mirror_on_the_wall.html">Tor Descriptors</a></li>
              <li><a href="#">Utilities</a></li>
              <li><a href="double_double_toil_and_trouble.html">Examples</a></li>
            </ul>
          </li>
          <li><a href="../api.html">API</a>
            <ul>
              <li><a href="../api/control.html">stem.control</a></li>
              <li><a href="../api/connection.html">stem.connection</a></li>
              <li><a href="../api/socket.html">stem.socket</a></li>
              <li><a href="../api/process.html">stem.process</a></li>
              <li><a href="../api/response.html">stem.response</a></li>
              <li><a href="../api/exit_policy.html">stem.exit_policy</a></li>
              <li><a href="../api/version.html">stem.version</a></li>
              <li><a href="../api.html#descriptors">Descriptors</a></li>
              <li><a href="../api.html#utilities">Utilities</a></li>
            </ul>
          </li>
          <li><a href="https://trac.torproject.org/projects/tor/wiki/doc/stem">Development</a>
            <ul>
              <li><a href="../faq.html">FAQ</a></li>
              <li><a href="../change_log.html">Change Log</a></li>
              <li><a href="https://trac.torproject.org/projects/tor/wiki/doc/stem/bugs">Bug Tracker</a></li>
              <li><a href="../download.html">Download</a></li>
            </ul>
          </li>
        </ul>
        </p>

      </div>
      <div class="content">
        
        
  <div class="section" id="east-of-the-sun-west-of-the-moon">
<h1>East of the Sun &amp; West of the Moon<a class="headerlink" href="#east-of-the-sun-west-of-the-moon" title="Permalink to this headline">¶</a></h1>
<p>The following is an overview of some of the utilities stem provides.</p>
<ul class="simple">
<li><a class="reference internal" href="#connection-resolution"><em>Connection Resolution</em></a></li>
</ul>
<div class="section" id="connection-resolution">
<span id="id1"></span><h2>Connection Resolution<a class="headerlink" href="#connection-resolution" title="Permalink to this headline">¶</a></h2>
<p>Connection information is a useful tool for learning more about network
applications like Tor. Our <a class="reference internal" href="../api/util/connection.html#stem.util.connection.get_connections" title="stem.util.connection.get_connections"><tt class="xref py py-func docutils literal"><span class="pre">stem.util.connection.get_connections()</span></tt></a>
function provides an easy method for accessing this information, with a few
caveats...</p>
<ul class="simple">
<li>Connection resolvers are platform specific. We <a class="reference external" href="../api/util/connection.html#stem.util.connection.Resolver">support several</a> but not not
all, most notably Windows (<a class="reference external" href="https://trac.torproject.org/9850">ticket 9850</a>).</li>
<li>By default Tor runs with a feature called <strong>DisableDebuggerAttachment</strong>. This
prevents debugging applications like gdb from analyzing Tor unless it is run
as root. Unfortunately this also alters the permissions of the Tor process
/proc contents breaking numerous system tools (including our resolvers). To
use this function you need to either run as root (discouraged) or add
<strong>DisableDebuggerAttachment 0</strong> to your torrc.</li>
</ul>
<p>Please note that if you operate an exit relay it is <strong>highly</strong> discouraged for
you to look at or record this information. Not only is doing so eavesdropping,
but likely also a violation of wiretap laws.</p>
<p>With that out of the way, how do you look up this information? Below is a
simple script that dumps Tor&#8217;s present connections.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>

<span class="kn">from</span> <span class="nn">stem.util.connection</span> <span class="kn">import</span> <span class="n">get_connections</span><span class="p">,</span> <span class="n">get_system_resolvers</span>
<span class="kn">from</span> <span class="nn">stem.util.system</span> <span class="kn">import</span> <span class="n">get_pid_by_name</span>

<span class="n">resolvers</span> <span class="o">=</span> <span class="n">get_system_resolvers</span><span class="p">()</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">resolvers</span><span class="p">:</span>
  <span class="k">print</span> <span class="s">&quot;Stem doesn&#39;t support any connection resolvers on our platform.&quot;</span>
  <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="n">picked_resolver</span> <span class="o">=</span> <span class="n">resolvers</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>  <span class="c"># lets just opt for the first</span>
<span class="k">print</span> <span class="s">&quot;Our platform supports connection resolution via: </span><span class="si">%s</span><span class="s"> (picked </span><span class="si">%s</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">resolvers</span><span class="p">),</span> <span class="n">picked_resolver</span><span class="p">)</span>

<span class="n">tor_pids</span> <span class="o">=</span> <span class="n">get_pid_by_name</span><span class="p">(</span><span class="s">&#39;tor&#39;</span><span class="p">,</span> <span class="n">multiple</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">tor_pids</span><span class="p">:</span>
  <span class="k">print</span> <span class="s">&quot;Unable to get tor&#39;s pid. Is it running?&quot;</span>
  <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">tor_pids</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
  <span class="k">print</span> <span class="s">&quot;You&#39;re running </span><span class="si">%i</span><span class="s"> instances of tor, picking the one with pid </span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">tor_pids</span><span class="p">),</span> <span class="n">tor_pids</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
  <span class="k">print</span> <span class="s">&quot;Tor is running with pid </span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">tor_pids</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

<span class="k">print</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">Connections:</span><span class="se">\n</span><span class="s">&quot;</span>

<span class="k">for</span> <span class="n">conn</span> <span class="ow">in</span> <span class="n">get_connections</span><span class="p">(</span><span class="n">picked_resolver</span><span class="p">,</span> <span class="n">process_pid</span> <span class="o">=</span> <span class="n">tor_pids</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">process_name</span> <span class="o">=</span> <span class="s">&#39;tor&#39;</span><span class="p">):</span>
  <span class="k">print</span> <span class="s">&quot;  </span><span class="si">%s</span><span class="s">:</span><span class="si">%s</span><span class="s"> =&gt; </span><span class="si">%s</span><span class="s">:</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">local_address</span><span class="p">,</span> <span class="n">conn</span><span class="o">.</span><span class="n">local_port</span><span class="p">,</span> <span class="n">conn</span><span class="o">.</span><span class="n">remote_address</span><span class="p">,</span> <span class="n">conn</span><span class="o">.</span><span class="n">remote_port</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>% python example.py
Our platform supports connection resolution via: proc, netstat, sockstat, lsof, ss (picked proc)
Tor is running with pid 17303

Connections:

  192.168.0.1:59014 =&gt; 38.229.79.2:443
  192.168.0.1:58822 =&gt; 68.169.35.102:443</pre>
</div>
</div>
</div>


      </div>
      <div class="bottomnav">
      </div>

    <div class="footer">
    </div>
  </body>
</html>