File: east_of_the_sun.rst

package info (click to toggle)
python-stem 1.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,892 kB
  • sloc: python: 33,713; java: 312; makefile: 131; sh: 27
file content (105 lines) | stat: -rw-r--r-- 3,063 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
East of the Sun & West of the Moon
==================================

The following is an overview of some of the utilities Stem provides.

* :ref:`terminal-styling`
* :ref:`multiprocessing`
* :ref:`connection-resolution`

.. _terminal-styling:

Terminal Styling
----------------

Know what's better than text? Pretty text!

OSX, Linux, BSD... really, everything except Windows supports terminal
formatting through `ANSI escape sequences
<https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes>`_. Doing this
yourself is easy, but we also provide a module to make it `even easier
<../api/util/term.html>`_.

|

.. image:: /_static/words_with.png

|

.. literalinclude:: /_static/example/words_with.py
   :language: python

.. _multiprocessing:

Multiprocessing
---------------

Python's `multiprocessing module
<https://docs.python.org/2/library/multiprocessing.html>`_ gives building
blocks to parallelize around the `Global Interpreter Lock
<https://en.wikipedia.org/wiki/Global_interpreter_lock>`_. However, honestly
it's clunky to use.

Ever just wanted to simply turn your threads into subprocesses? `We can do
that <../api/util/system.html#stem.util.system.DaemonTask>`_.

**Threaded**

.. literalinclude:: /_static/example/fibonacci_threaded.py
   :language: python

::

  % python fibonacci_threaded.py
  took 21.1 seconds

**Multi-process**

.. literalinclude:: /_static/example/fibonacci_multiprocessing.py
   :language: python

::

  % python fibonacci_multiprocessing.py
  took 6.2 seconds
.. _connection-resolution:

Connection Resolution
---------------------

Connection information is a useful tool for learning more about network
applications like Tor. Our :func:`stem.util.connection.get_connections`
function provides an easy method for accessing this information, with a few
caveats...

* Connection resolvers are platform specific. We `support several
  <../api/util/connection.html#stem.util.connection.Resolver>`_ platforms but not all.

* By default Tor runs with a feature called **DisableDebuggerAttachment**. 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
  **DisableDebuggerAttachment 0** to your torrc.

Please note that if you operate an exit relay it is **highly** discouraged for
you to look at or record this information. Not only is doing so eavesdropping,
but likely also a violation of wiretap laws. 

With that out of the way, how do you look up this information? Below is a
simple script that dumps Tor's present connections.

.. literalinclude:: /_static/example/utilities.py
   :language: python

::

  % 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 => 38.229.79.2:443
    192.168.0.1:58822 => 68.169.35.102:443