<!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>Connection Utilities — 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="Enumerations" href="enum.html" /> <link rel="prev" title="Configuration File Handling" href="conf.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>Connection Utilities</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="../../tutorials/the_little_relay_that_could.html">Hello World</a></li> <li><a href="../../tutorials/to_russia_with_love.html">Client Usage</a></li> <li><a href="../../tutorials/tortoise_and_the_hare.html">Event Listening</a></li> <li><a href="../../tutorials/mirror_mirror_on_the_wall.html">Tor Descriptors</a></li> <li><a href="../../tutorials/east_of_the_sun.html">Utilities</a></li> <li><a href="../../tutorials/double_double_toil_and_trouble.html">Examples</a></li> </ul> </li> <li><a href="../../api.html">API</a> <ul> <li><a href="../control.html">stem.control</a></li> <li><a href="../connection.html">stem.connection</a></li> <li><a href="../socket.html">stem.socket</a></li> <li><a href="../process.html">stem.process</a></li> <li><a href="../response.html">stem.response</a></li> <li><a href="../exit_policy.html">stem.exit_policy</a></li> <li><a href="../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="module-stem.util.connection"> <span id="connection-utilities"></span><h1>Connection Utilities<a class="headerlink" href="#module-stem.util.connection" title="Permalink to this headline">¶</a></h1> <p>Connection and networking based utility functions.</p> <div class="highlight-python"><pre>get_connections - quieries the connections belonging to a given process get_system_resolvers - provides connection resolution methods that are likely to be available is_valid_ipv4_address - checks if a string is a valid IPv4 address is_valid_ipv6_address - checks if a string is a valid IPv6 address is_valid_port - checks if something is a valid representation for a port is_private_address - checks if an IPv4 address belongs to a private range or not expand_ipv6_address - provides an IPv6 address with its collapsed portions expanded get_mask_ipv4 - provides the mask representation for a given number of bits get_mask_ipv6 - provides the IPv6 mask representation for a given number of bits</pre> </div> <dl class="data"> <dt id="stem.util.connection.Resolver"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">Resolver</tt><big>(</big><em>enum</em><big>)</big><a class="headerlink" href="#stem.util.connection.Resolver" title="Permalink to this definition">¶</a></dt> <dd><p>Method for resolving a process’ connections.</p> <table border="1" class="docutils"> <colgroup> <col width="36%" /> <col width="64%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Resolver</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><strong>PROC</strong></td> <td>/proc contents</td> </tr> <tr class="row-odd"><td><strong>NETSTAT</strong></td> <td>netstat command</td> </tr> <tr class="row-even"><td><strong>SS</strong></td> <td>ss command</td> </tr> <tr class="row-odd"><td><strong>LSOF</strong></td> <td>lsof command</td> </tr> <tr class="row-even"><td><strong>SOCKSTAT</strong></td> <td>sockstat command under <a href="#id1"><span class="problematic" id="id2">*</span></a>nix</td> </tr> <tr class="row-odd"><td><strong>BSD_SOCKSTAT</strong></td> <td>sockstat command under FreeBSD</td> </tr> <tr class="row-even"><td><strong>BSD_PROCSTAT</strong></td> <td>procstat command under FreeBSD</td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="stem.util.connection.Connection"> <em class="property">class </em><tt class="descclassname">stem.util.connection.</tt><tt class="descname">Connection</tt><a class="headerlink" href="#stem.util.connection.Connection" title="Permalink to this definition">¶</a></dt> <dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></p> <p>Connection(local_address, local_port, remote_address, remote_port, protocol)</p> <dl class="attribute"> <dt id="stem.util.connection.Connection.local_address"> <tt class="descname">local_address</tt><a class="headerlink" href="#stem.util.connection.Connection.local_address" title="Permalink to this definition">¶</a></dt> <dd><p>Alias for field number 0</p> </dd></dl> <dl class="attribute"> <dt id="stem.util.connection.Connection.local_port"> <tt class="descname">local_port</tt><a class="headerlink" href="#stem.util.connection.Connection.local_port" title="Permalink to this definition">¶</a></dt> <dd><p>Alias for field number 1</p> </dd></dl> <dl class="attribute"> <dt id="stem.util.connection.Connection.protocol"> <tt class="descname">protocol</tt><a class="headerlink" href="#stem.util.connection.Connection.protocol" title="Permalink to this definition">¶</a></dt> <dd><p>Alias for field number 4</p> </dd></dl> <dl class="attribute"> <dt id="stem.util.connection.Connection.remote_address"> <tt class="descname">remote_address</tt><a class="headerlink" href="#stem.util.connection.Connection.remote_address" title="Permalink to this definition">¶</a></dt> <dd><p>Alias for field number 2</p> </dd></dl> <dl class="attribute"> <dt id="stem.util.connection.Connection.remote_port"> <tt class="descname">remote_port</tt><a class="headerlink" href="#stem.util.connection.Connection.remote_port" title="Permalink to this definition">¶</a></dt> <dd><p>Alias for field number 3</p> </dd></dl> </dd></dl> <dl class="function"> <dt id="stem.util.connection.get_connections"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">get_connections</tt><big>(</big><em>resolver</em>, <em>process_pid=None</em>, <em>process_name=None</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/connection.html#get_connections"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.connection.get_connections" title="Permalink to this definition">¶</a></dt> <dd><p>Retrieves a list of the current connections for a given process. The provides a list of Connection instances, which have four attributes...</p> <blockquote> <div><ul class="simple"> <li>local_address (str)</li> <li>local_port (int)</li> <li>remote_address (str)</li> <li>remote_port (int)</li> <li>protocol (str, generally either ‘tcp’ or ‘udp’)</li> </ul> </div></blockquote> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> <li><strong>resolver</strong> (<em>Resolver</em>) – method of connection resolution to use</li> <li><strong>process_pid</strong> (<em>int</em>) – pid of the process to retrieve</li> <li><strong>process_name</strong> (<em>str</em>) – name of the process to retrieve</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Raises :</th><td class="field-body"><ul class="first last simple"> <li><strong>ValueError</strong> if using <strong>Resolver.PROC</strong> or <strong>Resolver.BSD_PROCSTAT</strong> and the process_pid wasn’t provided</li> <li><strong>IOError</strong> if no connections are available or resolution fails (generally they’re indistinguishable). The common causes are the command being unavailable or permissions.</li> </ul> </td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.connection.get_system_resolvers"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">get_system_resolvers</tt><big>(</big><em>system=None</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/connection.html#get_system_resolvers"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.connection.get_system_resolvers" title="Permalink to this definition">¶</a></dt> <dd><p>Provides the types of connection resolvers likely to be available on this platform.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>system</strong> (<em>str</em>) – system to get resolvers for, this is determined by platform.system() if not provided</td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><strong>list</strong> of Resolvers likely to be available on this platform</td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.connection.is_valid_ipv4_address"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">is_valid_ipv4_address</tt><big>(</big><em>address</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/connection.html#is_valid_ipv4_address"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.connection.is_valid_ipv4_address" title="Permalink to this definition">¶</a></dt> <dd><p>Checks if a string is a valid IPv4 address.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>address</strong> (<em>str</em>) – string to be checked</td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><strong>True</strong> if input is a valid IPv4 address, <strong>False</strong> otherwise</td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.connection.is_valid_ipv6_address"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">is_valid_ipv6_address</tt><big>(</big><em>address</em>, <em>allow_brackets=False</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/connection.html#is_valid_ipv6_address"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.connection.is_valid_ipv6_address" title="Permalink to this definition">¶</a></dt> <dd><p>Checks if a string is a valid IPv6 address.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> <li><strong>address</strong> (<em>str</em>) – string to be checked</li> <li><strong>allow_brackets</strong> (<em>bool</em>) – ignore brackets which form ‘[address]’</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><strong>True</strong> if input is a valid IPv6 address, <strong>False</strong> otherwise</p> </td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.connection.is_valid_port"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">is_valid_port</tt><big>(</big><em>entry</em>, <em>allow_zero=False</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/connection.html#is_valid_port"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.connection.is_valid_port" title="Permalink to this definition">¶</a></dt> <dd><p>Checks if a string or int is a valid port number.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> <li><strong>entry</strong> (<em>list,str,int</em>) – string, integer or list to be checked</li> <li><strong>allow_zero</strong> (<em>bool</em>) – accept port number of zero (reserved by definition)</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><strong>True</strong> if input is an integer and within the valid port range, <strong>False</strong> otherwise</p> </td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.connection.is_private_address"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">is_private_address</tt><big>(</big><em>address</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/connection.html#is_private_address"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.connection.is_private_address" title="Permalink to this definition">¶</a></dt> <dd><p>Checks if the IPv4 address is in a range belonging to the local network or loopback. These include:</p> <blockquote> <div><ul class="simple"> <li>Private ranges: 10.*, 172.16.* - 172.31.*, 192.168.*</li> <li>Loopback: 127.*</li> </ul> </div></blockquote> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>address</strong> (<em>str</em>) – string to be checked</td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><strong>True</strong> if input is in a private range, <strong>False</strong> otherwise</td> </tr> <tr class="field-odd field"><th class="field-name">Raises :</th><td class="field-body"><strong>ValueError</strong> if the address isn’t a valid IPv4 address</td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.connection.expand_ipv6_address"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">expand_ipv6_address</tt><big>(</big><em>address</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/connection.html#expand_ipv6_address"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.connection.expand_ipv6_address" title="Permalink to this definition">¶</a></dt> <dd><p>Expands abbreviated IPv6 addresses to their full colon separated hex format. For instance...</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">expand_ipv6_address</span><span class="p">(</span><span class="s">"2001:db8::ff00:42:8329"</span><span class="p">)</span> <span class="go">"2001:0db8:0000:0000:0000:ff00:0042:8329"</span> <span class="gp">>>> </span><span class="n">expand_ipv6_address</span><span class="p">(</span><span class="s">"::"</span><span class="p">)</span> <span class="go">"0000:0000:0000:0000:0000:0000:0000:0000"</span> </pre></div> </div> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>address</strong> (<em>str</em>) – IPv6 address to be expanded</td> </tr> <tr class="field-even field"><th class="field-name">Raises :</th><td class="field-body"><strong>ValueError</strong> if the address can’t be expanded due to being malformed</td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.connection.get_mask_ipv4"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">get_mask_ipv4</tt><big>(</big><em>bits</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/connection.html#get_mask_ipv4"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.connection.get_mask_ipv4" title="Permalink to this definition">¶</a></dt> <dd><p>Provides the IPv4 mask for a given number of bits, in the dotted-quad format.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>bits</strong> (<em>int</em>) – number of bits to be converted</td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><strong>str</strong> with the subnet mask representation for this many bits</td> </tr> <tr class="field-odd field"><th class="field-name">Raises :</th><td class="field-body"><strong>ValueError</strong> if given a number of bits outside the range of 0-32</td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.connection.get_mask_ipv6"> <tt class="descclassname">stem.util.connection.</tt><tt class="descname">get_mask_ipv6</tt><big>(</big><em>bits</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/connection.html#get_mask_ipv6"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.connection.get_mask_ipv6" title="Permalink to this definition">¶</a></dt> <dd><p>Provides the IPv6 mask for a given number of bits, in the hex colon-delimited format.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>bits</strong> (<em>int</em>) – number of bits to be converted</td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><strong>str</strong> with the subnet mask representation for this many bits</td> </tr> <tr class="field-odd field"><th class="field-name">Raises :</th><td class="field-body"><strong>ValueError</strong> if given a number of bits outside the range of 0-128</td> </tr> </tbody> </table> </dd></dl> </div> </div> <div class="bottomnav"> </div> <div class="footer"> </div> </body> </html>