<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>reachtim</title>
  <subtitle>Tim Arnold — writing about Python, the insides of PDF and LaTeX, and keeping servers honest, plus galleries of crystals, curves and folded colour.</subtitle>
  <link href="https://reachtim.com/feeds/all.atom.xml" rel="self" />
  <link href="https://reachtim.com/" rel="alternate" />
  <id>https://reachtim.com/</id>
  <updated>2026-09-05T19:35:09Z</updated>
  <author>
    <name>Tim Arnold</name>
    <uri>https://reachtim.com</uri>
  </author>
  <entry>
    <title>The GPS That Was Certain, and Wrong</title>
    <link href="https://reachtim.com/articles/gps-certain-and-wrong.html" rel="alternate" />
    <id>https://reachtim.com/articles/gps-certain-and-wrong.html</id>
    <published>2026-09-03T00:00:00Z</published>
    <updated>2026-09-03T00:00:00Z</updated>
    <category term="Security" />
    <summary type="text">A single fake antenna name can move a survey rover two metres while it insists it has a perfect fix -- or, on a differently-compiled copy of the same program, bring a drone down within tens of metres of wherever the attacker chooses. A long walk through how, for people who have never touched GPS software.</summary>
    <content type="html">&lt;p&gt;There is a bug in a piece of free software called RTKLIB that I have been
picking at for a few weeks. On the surface it is a boring bug — the kind that
gets filed, ignored, and forgotten. Underneath it is something more interesting:
a way to make a very expensive, very precise GPS receiver report a location that
is wrong by a couple of metres, while it continues to insist that its answer is
perfect.&lt;/p&gt;
&lt;p&gt;No crash. No warning. Nothing in any log.&lt;/p&gt;
&lt;p&gt;I flew a simulated drone into it and the aircraft did exactly what it was told,
arrived where it believed it had been sent, and shut down normally — while
sitting well over a metre from where it actually was. Then I rebuilt the same
software with one different compiler setting and flew it again, and this time
the aircraft fell out of the sky on cue.&lt;/p&gt;
&lt;p&gt;This post is the long version, written for people who have never thought about
GPS software in their lives. If you already know what a mantissa is, you can
skim; I am going to explain everything.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Status as of 2026-08-31. Dates below are absolute rather than &amp;ldquo;a few weeks
ago,&amp;rdquo; so this post stays legible after the week it was written.&lt;/em&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;part-1-how-gps-knows-where-you-are-and-why-it-isnt-good-enough&#34;&gt;Part 1: How GPS knows where you are, and why it isn&amp;rsquo;t good enough&lt;/h2&gt;
&lt;p&gt;A GPS satellite does one useful thing: it shouts the time, constantly, very
accurately.&lt;/p&gt;
&lt;p&gt;Your receiver listens to several of these shouts at once. Because it knows how
fast radio travels, and it knows roughly what time it is, it can work out how
long each signal took to arrive. Multiply by the speed of light and you have a
distance to each satellite. Get four or more distances and there is only one
point in space where you could possibly be standing.&lt;/p&gt;
&lt;p&gt;That is the whole idea, and it works remarkably well for something that involves
listening to clocks in orbit. It will put you within a few metres.&lt;/p&gt;
&lt;p&gt;A few metres is fine for finding a restaurant. It is useless for a great many
other things. It is useless for surveying a property boundary. It is useless for
a tractor that has to plant this year&amp;rsquo;s rows between last year&amp;rsquo;s rows. It is
useless for a drone inspecting a bridge, or landing on a platform, or flying a
mapping grid where the photos have to line up.&lt;/p&gt;
&lt;p&gt;Why only a few metres? Because the signal has to cross the atmosphere to reach
you, and the atmosphere is not a vacuum, and it is not consistent. The
ionosphere slows the signal down a bit. The troposphere slows it down a bit
differently. The satellite&amp;rsquo;s own clock and orbit are known slightly imperfectly.
Each of these adds a small error to every distance, and the small errors add up
to a few metres of uncertainty.&lt;/p&gt;
&lt;p&gt;Here is the important part: &lt;strong&gt;those errors are almost identical for two
receivers standing near each other.&lt;/strong&gt; The same patch of atmosphere is sitting
over both of you. The same satellite clock is slightly wrong for both of you.&lt;/p&gt;
&lt;p&gt;That shared error is the opening.&lt;/p&gt;
&lt;h2 id=&#34;part-2-rtk-and-the-trick-that-gets-you-centimetres&#34;&gt;Part 2: RTK, and the trick that gets you centimetres&lt;/h2&gt;
&lt;p&gt;If two receivers a few kilometres apart are both being lied to in the same way,
and one of them &lt;em&gt;already knows exactly where it is&lt;/em&gt;, then it can work out how
big the lie is right now and tell the other one.&lt;/p&gt;
&lt;p&gt;That is Real-Time Kinematic positioning — RTK. It has two halves:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;base station&lt;/strong&gt;: a receiver bolted to something that does not move, at a
  location that has been surveyed precisely in advance. It knows its own
  coordinates to the centimetre.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;rover&lt;/strong&gt;: the receiver that is actually going somewhere — on a survey pole,
  a tractor, a drone.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;                      *       *       *          GPS satellites
                       \      |      /
                        \     |     /            the same signals,
                         \    |    /             carrying the same errors
        ~~~~~~~~~~~~~~~~~~\~~~|~~~/~~~~~~~~~~~~~~~~~~~~~~~~
              atmosphere   \  |  /   (slows both of them, equally)
        ~~~~~~~~~~~~~~~~~~~~\~|~/~~~~~~~~~~~~~~~~~~~~~~~~~~
                             \|/
                    +---------+                +---------+
                    |  BASE   | =============&amp;gt; |  ROVER  |
                    +---------+   corrections  +---------+
                  bolted down,                  on a pole,
                  knows exactly                 a tractor,
                  where it is                   a drone

                  &#34;the errors right      &#34;thanks - subtracting
                   now are THIS big&#34;      them from mine&#34;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The base watches the same satellites as the rover. Because it knows where it
really is, it can compute what the satellite distances &lt;em&gt;should&lt;/em&gt; be, compare them
to what it is actually measuring, and derive the error in each one. It packages
those corrections up and streams them to the rover continuously — over a radio
link, or the internet, or a phone connection.&lt;/p&gt;
&lt;p&gt;The rover applies the corrections and the shared error cancels out. Instead of a
few metres, you get one to two centimetres.&lt;/p&gt;
&lt;p&gt;This is not exotic kit. Farm equipment uses it. Construction sites use it.
Surveyors use it all day. Drone mapping depends on it.&lt;/p&gt;
&lt;h3 id=&#34;the-linchpin&#34;&gt;The linchpin&lt;/h3&gt;
&lt;p&gt;Read that again and notice what the whole scheme rests on: &lt;strong&gt;the base station
knowing exactly where it is.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;RTK does not really measure where the rover is. It measures the &lt;em&gt;distance and
direction from the base to the rover&lt;/em&gt;, very precisely, and then adds that to
where the base says it is.&lt;/p&gt;
&lt;p&gt;So if the base station&amp;rsquo;s stated coordinate is wrong by two metres, the geometry
is still perfect — the rover measures its offset from the base beautifully — but
every position it produces is shifted by exactly those two metres. The rover has
accurately measured its distance from a lie.&lt;/p&gt;
&lt;p&gt;Nothing about that looks like an error. The measurements are all clean and
consistent. The rover&amp;rsquo;s own confidence checks pass, because they are checking
the quality of the measurements, and the measurements &lt;em&gt;are&lt;/em&gt; good. It is only the
starting point that is wrong.&lt;/p&gt;
&lt;p&gt;That is the target.&lt;/p&gt;
&lt;h2 id=&#34;part-3-the-software&#34;&gt;Part 3: The software&lt;/h2&gt;
&lt;p&gt;RTKLIB is an open-source library that implements all of this. It was written by
a Japanese academic, Tomoji Takasu, and it is genuinely excellent — for a long
time it was the only way to do serious RTK without buying proprietary equipment.&lt;/p&gt;
&lt;p&gt;It is also everywhere. It ships in Windows applications that surveyors run. It
is packaged by Linux distributions. A popular fork called &amp;ldquo;demo5&amp;rdquo; is what most
hobbyists with low-cost receivers use. And its code has been copied into
commercial GPS products.&lt;/p&gt;
&lt;p&gt;One more fact matters: &lt;strong&gt;the stable branch has not received a commit since
December 2020.&lt;/strong&gt; The project is, in practice, unmaintained.&lt;/p&gt;
&lt;h2 id=&#34;part-4-the-message&#34;&gt;Part 4: The message&lt;/h2&gt;
&lt;p&gt;The base station talks to the rover in a format called RTCM3. Most of the
messages are corrections — satellite by satellite, here is how wrong your
distance measurement is.&lt;/p&gt;
&lt;p&gt;But a few messages are just housekeeping. Message &lt;strong&gt;type 1033&lt;/strong&gt; is one of these.
It carries five pieces of text describing the base station&amp;rsquo;s hardware:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the antenna model&lt;/li&gt;
&lt;li&gt;the antenna serial number&lt;/li&gt;
&lt;li&gt;the receiver model&lt;/li&gt;
&lt;li&gt;the receiver firmware version&lt;/li&gt;
&lt;li&gt;the receiver serial number&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Nothing but labels. &lt;code&gt;TRIMBLE NETR9&lt;/code&gt;, that sort of thing. Nobody navigates by
them.&lt;/p&gt;
&lt;p&gt;Because these are text of varying length, the format uses a common trick. Each
string is sent as &lt;strong&gt;a length, then that many characters&lt;/strong&gt;. So the receiver reads
one byte that says &amp;ldquo;the next thing is 13 characters long&amp;rdquo;, then reads 13
characters. Like an envelope with the page count written on the outside.&lt;/p&gt;
&lt;p&gt;That length byte is a single byte, so it can say any number from 0 to 255.&lt;/p&gt;
&lt;p&gt;Hold that thought.&lt;/p&gt;
&lt;h2 id=&#34;part-5-the-bug&#34;&gt;Part 5: The bug&lt;/h2&gt;
&lt;p&gt;Inside RTKLIB there is a place to put each of those five strings. The receiver
model, for example, gets a slot 64 characters wide. That is generous; real
receiver names are far shorter.&lt;/p&gt;
&lt;p&gt;The code does something careful and then something careless.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The careful bit:&lt;/strong&gt; when it reads the characters off the wire, it uses a
temporary holding area of 32 characters and it explicitly refuses to read more
than 31 of them. However big a number the attacker writes on the envelope, only
31 characters ever get read in.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The careless bit:&lt;/strong&gt; when it then copies that text into the permanent 64-character
slot, it uses &lt;strong&gt;the length the attacker declared&lt;/strong&gt; — not the amount it actually
read, and not the size of the slot.&lt;/p&gt;
&lt;p&gt;So an attacker sends a message that says &amp;ldquo;the receiver model is 255 characters
long,&amp;rdquo; and the program dutifully copies 255 characters into a space built for
64. It runs off the end by about 190 characters and keeps writing into whatever
happens to sit next in memory.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;  memory:  receiver-model slot      other fields        BASE COORDINATES
           |&amp;lt;--- 64 bytes ---&amp;gt;|                       |&amp;lt;-X-&amp;gt;&amp;lt;-Y-&amp;gt;&amp;lt;-Z-&amp;gt;|
          +-------------------+----------------------+-----+-----+-----+
          | TRIMBLE NETR9     |  serial, version...  |     |     |     |
          +-------------------+----------------------+-----+-----+-----+
           ^                  ^                            ^
           |                  |                            |
           |   the slot ends here                          |
           |                                               |
           |&amp;lt;===== &#34;I am 255 characters long&#34; =============&amp;gt;|
                 31 real       then zeros, zeros, zeros...
                 characters
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That is a buffer overflow, and a security firm called FuzzingLabs found and
reported it, along with three similar ones, on 2026-06-09.&lt;/p&gt;
&lt;h3 id=&#34;whats-theirs-and-whats-new-here&#34;&gt;What&amp;rsquo;s theirs, and what&amp;rsquo;s new here&lt;/h3&gt;
&lt;p&gt;FuzzingLabs found this — the overflow, the unbounded length, the missing
check — and reported it accurately: an out-of-bounds write, attacker-controlled
length, reachable over an unauthenticated correction stream. That part is
theirs, and Part 14 credits it in full.&lt;/p&gt;
&lt;p&gt;What their write-up doesn&amp;rsquo;t do is follow the spillover to where it lands. It
frames this as a memory-corruption bug and stops there — it never mentions the
base station&amp;rsquo;s coordinates, and their own proof-of-concept drives a field that
can&amp;rsquo;t reach them. Everything from here on — which of the five text fields
actually reaches the coordinates, what erasing rather than overwriting them
does, whether the receiver notices, and which real programs corrupt silently
versus crash — builds on their find.&lt;/p&gt;
&lt;h3 id=&#34;why-it-looks-boring&#34;&gt;Why it looks boring&lt;/h3&gt;
&lt;p&gt;If you have any security background, your instinct now is that this is a
takeover — overflow a buffer, overwrite something important, take control of the
machine. That instinct is wrong here, for two reasons.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Everything written past the end is zeros.&lt;/strong&gt; The copying function stops at the
end of the real text and then pads the rest of the declared length with zeros.
Since at most 31 real characters were ever read in, everything beyond that is
zero-fill. The attacker cannot write chosen values out of bounds. They can only
&lt;em&gt;erase&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Think of it as an eraser rather than a pen. You choose where the erasing starts
and how far it goes, and that is all you choose.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;And it never escapes its container.&lt;/strong&gt; The 64-character slot sits inside a much
larger structure — around 240 kilobytes of it. Even the worst case only reaches
a few hundred bytes past the slot, which is still deep inside that structure.
There is nothing there worth hitting. No return addresses, no function pointers,
none of the machinery an exploit would need.&lt;/p&gt;
&lt;p&gt;So: a bug that writes zeros into the middle of a large object it cannot get out
of. It is still out of bounds for the 64-byte slot it is writing into — the
most common automated memory-checking tools just don&amp;rsquo;t watch for that. They
track the edges of a whole allocation; the boundary between two fields inside
it is smaller than what they watch, so a write that crosses that boundary but
stays inside the container passes silently. A finer-grained check, one that
knows the declared size of each individual field, catches it immediately.&lt;/p&gt;
&lt;p&gt;Boring.&lt;/p&gt;
&lt;p&gt;Except for what happens to be sitting a couple of hundred bytes past the
receiver-model slot.&lt;/p&gt;
&lt;h3 id=&#34;a-second-bug-not-in-fuzzinglabs-report&#34;&gt;A second bug, not in FuzzingLabs&amp;rsquo; report&lt;/h3&gt;
&lt;p&gt;I found this one while checking that my fix didn&amp;rsquo;t break anything else, and
it has nothing to do with the coordinates.&lt;/p&gt;
&lt;p&gt;Each of the five text fields is read using its own length, computed from a
running position in the message. But the loop that actually copies the
characters only ever advances by the 31 bytes it read — never by however many
the sender declared. So if an &lt;em&gt;earlier&lt;/em&gt; field claims to be longer than 31
characters, every field parsed after it gets read from the wrong spot in the
message. The lengths still come out right, because those are read separately;
only the contents shift. The result looks like an ordinary receiver serial
number that happens to be nonsense, not an obvious parse failure.&lt;/p&gt;
&lt;p&gt;Every read stays inside the message, so this stays out of memory-safety
territory, and the field the coordinate attack depends on is read before the
walk drifts off course, so that attack is unaffected. Worth mentioning
anyway: it&amp;rsquo;s the same shape as the main bug, a length taken off the wire and
never checked against what actually happened.&lt;/p&gt;
&lt;h2 id=&#34;part-6-what-the-zeros-land-on-and-what-a-mantissa-is&#34;&gt;Part 6: What the zeros land on, and what a mantissa is&lt;/h2&gt;
&lt;p&gt;They land on the base station&amp;rsquo;s coordinates.&lt;/p&gt;
&lt;p&gt;Remember Part 2: the coordinate of the base station is the number the entire
system trusts. Those three numbers — an X, Y and Z position in a coordinate
system centred on the Earth — sit in memory a couple of hundred bytes past the
receiver-model text. Well within reach.&lt;/p&gt;
&lt;p&gt;Now, an attacker who could write &lt;em&gt;chosen&lt;/em&gt; values there would simply type in a
new location. But we established that this attacker can only write zeros. So
what does erasing part of a number actually do to it?&lt;/p&gt;
&lt;p&gt;This is the part that turns a boring bug into an interesting one, and it needs a
short detour into how computers store numbers with decimal points.&lt;/p&gt;
&lt;h3 id=&#34;scientific-notation-in-binary&#34;&gt;Scientific notation, in binary&lt;/h3&gt;
&lt;p&gt;You already know scientific notation. Instead of writing&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;3,978,242.4348
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;you write&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;3.9782424348 × 10⁶
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Two separate pieces of information: &lt;strong&gt;the digits&lt;/strong&gt; (3.9782424348) and &lt;strong&gt;where
the decimal point goes&lt;/strong&gt; (10⁶). Move the point around, and you can write
enormous numbers or tiny ones using the same handful of digits.&lt;/p&gt;
&lt;p&gt;Computers store decimal numbers exactly this way, but in binary. The format is
called a &lt;em&gt;double&lt;/em&gt;, it occupies 8 bytes, and those 8 bytes are divided into three
parts:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;part&lt;/th&gt;
&lt;th&gt;size&lt;/th&gt;
&lt;th&gt;what it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;sign&lt;/td&gt;
&lt;td&gt;1 bit&lt;/td&gt;
&lt;td&gt;positive or negative&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;exponent&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;11 bits&lt;/td&gt;
&lt;td&gt;where the point goes — the overall scale&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;mantissa&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;52 bits&lt;/td&gt;
&lt;td&gt;the actual digits&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The &lt;strong&gt;mantissa&lt;/strong&gt; is just the fancy word for &amp;ldquo;the digits.&amp;rdquo; It is the significant
part of the number, stripped of any information about how big the number is. The
exponent handles bigness; the mantissa handles precision.&lt;/p&gt;
&lt;p&gt;Fifty-two binary digits is a lot of precision. For a number around four million
— which is roughly what our coordinate is, measured in metres — those 52 bits
let you specify a position down to about &lt;em&gt;half a millionth of a millimetre&lt;/em&gt;,
under half a nanometre. Far finer than anyone needs.&lt;/p&gt;
&lt;h3 id=&#34;now-erase-some-digits&#34;&gt;Now erase some digits&lt;/h3&gt;
&lt;p&gt;Here is the crucial detail. On essentially every computer you will meet, the
8 bytes of a double are stored with the &lt;strong&gt;least important digits first&lt;/strong&gt;. So
when our attacker&amp;rsquo;s eraser sweeps through, the first thing it destroys is the
fine detail — and only as it continues does it start eating the digits that
matter.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;  one coordinate = one double = 8 bytes

   byte 0     1     2     3     4     5     6     7
      +-----+-----+-----+-----+-----+-----+-----+-----+
      |     |     |     |     |     |     |     |     |
      +-----+-----+-----+-----+-----+-----+-----+-----+
      |&amp;lt;------- mantissa: the digits -------&amp;gt;|&amp;lt;-exp-&amp;gt;|s|
      |                                      |
      | finest detail            coarsest detail
      | (billionths of a mm)     (metres, then hundreds of metres)
      |
      +-- the eraser starts HERE and works rightwards
          so precision is destroyed from the bottom up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Each byte erased wipes out 8 binary digits of precision. The number does not
become garbage. It becomes a &lt;strong&gt;rounder version of itself&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Picture a ruler marked in millimetres. Rub out every tenth marking and you can
now only report positions to the nearest centimetre. Rub out more and you are
down to the nearest 10 cm, then the nearest metre. The ruler still works. It
still gives you sensible-looking answers. The answers are just coarser, and
anything between the surviving marks gets snapped to a mark.&lt;/p&gt;
&lt;p&gt;Here is that idea on an actual number line, using the real coordinate from my
test data. The base station&amp;rsquo;s true X position is &lt;code&gt;-3,978,242.4348&lt;/code&gt; metres:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;  FULL PRECISION - every fine gradation still present

    -3978244        -3978243        -3978242        -3978241
   -----|---------------|---------------|---------------|-----&amp;gt;
                                 x
                                 |
                                 +--- true position of the base:
                                      -3978242.4348


  ERASE 4 BYTES - only every 2 metres survives

    -3978244                        -3978242
   -----|-------------------------------|----------------------&amp;gt;
                                 x      |
                                 |      |
                                 +-----&amp;gt;+
                                  snaps to the nearest
                                  surviving mark, toward zero

                                  new coordinate: -3978242.0000
                                  the base has &#34;moved&#34; 0.43 m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note which way it moves. Erasing digits always rounds the number &lt;em&gt;towards zero&lt;/em&gt;,
so the attacker does not even choose the direction — that falls out of the base
station&amp;rsquo;s own coordinates.&lt;/p&gt;
&lt;p&gt;Running that for every number of erased bytes, on the same real coordinate:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;bytes erased&lt;/th&gt;
&lt;th&gt;declared length&lt;/th&gt;
&lt;th&gt;mantissa digits left&lt;/th&gt;
&lt;th&gt;resulting grid&lt;/th&gt;
&lt;th&gt;coordinate becomes&lt;/th&gt;
&lt;th&gt;error&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;208&lt;/td&gt;
&lt;td&gt;44&lt;/td&gt;
&lt;td&gt;0.00012 mm&lt;/td&gt;
&lt;td&gt;−3978242.4348&lt;/td&gt;
&lt;td&gt;none you could measure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;209&lt;/td&gt;
&lt;td&gt;36&lt;/td&gt;
&lt;td&gt;0.03 mm&lt;/td&gt;
&lt;td&gt;−3978242.4348&lt;/td&gt;
&lt;td&gt;none you could measure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;210&lt;/td&gt;
&lt;td&gt;28&lt;/td&gt;
&lt;td&gt;8 mm&lt;/td&gt;
&lt;td&gt;−3978242.4297&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;5 mm&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;211&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2 m&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;−3978242.0000&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.43 m&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;212&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;512 m&lt;/td&gt;
&lt;td&gt;−3978240.0000&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2.43 m&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;213&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;131 km&lt;/td&gt;
&lt;td&gt;−3932160.0000&lt;/td&gt;
&lt;td&gt;46 km&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Each extra byte erased multiplies the coarseness by 256, because that is what
eight more binary digits does. The coordinate gets snapped down to the nearest
multiple of the grid spacing.&lt;/p&gt;
&lt;p&gt;Two things follow, and they are what make this attack practical.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The attacker chooses the scale, not the value.&lt;/strong&gt; They cannot say &amp;ldquo;move it two
metres north.&amp;rdquo; They can only say &amp;ldquo;round it off this hard.&amp;rdquo; Where it lands then
depends on the base station&amp;rsquo;s actual coordinates — which they may not know.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;But one of those rungs is universal.&lt;/strong&gt; Look at the row where the grid becomes
2 metres. Whatever the base station&amp;rsquo;s true coordinate is, rounding it down to
the nearest multiple of 2 metres moves it by &lt;em&gt;somewhere between zero and two
metres&lt;/em&gt;. Always. The attacker does not need to know anything about the base
station to guarantee an error of that size. Below that rung the error is too
small to matter; above it, the error gets so large that the receiver stops
producing a confident answer at all and the attack becomes obvious.&lt;/p&gt;
&lt;p&gt;There is a narrow band where the damage is big enough to matter and small enough
to stay invisible, and it is reachable without knowing anything about the target.&lt;/p&gt;
&lt;p&gt;The &amp;ldquo;declared length&amp;rdquo; column above is arithmetic anyone can redo from the
public source in an afternoon — offset of the coordinates, minus offset of the
field, minus the 31 bytes that get read correctly. It&amp;rsquo;s in the table now
because there&amp;rsquo;s no longer a reason to make a reader do that arithmetic
themselves.&lt;/p&gt;
&lt;h2 id=&#34;part-7-the-message-that-triggers-its-own-damage&#34;&gt;Part 7: The message that triggers its own damage&lt;/h2&gt;
&lt;p&gt;There is one more piece, and it is what makes this a live attack rather than a
curiosity.&lt;/p&gt;
&lt;p&gt;When RTKLIB finishes decoding a type 1033 message, it reports back &amp;ldquo;this was an
antenna information message.&amp;rdquo; The real-time server sees that report and responds
by taking the base station coordinates out of the message and adopting them as
the reference for all future position calculations.&lt;/p&gt;
&lt;p&gt;Without checking them.&lt;/p&gt;
&lt;p&gt;So a single message does both halves of the job: it corrupts the coordinate, and
it causes the corrupted coordinate to be put into service. No second message, no
waiting.&lt;/p&gt;
&lt;p&gt;And here is the detail I keep coming back to. RTKLIB has a second code path for
processing recorded data after the fact, rather than live. &lt;strong&gt;That path checks.&lt;/strong&gt;
Before adopting a base position it verifies the coordinate is not obviously
broken.&lt;/p&gt;
&lt;p&gt;Somebody thought about this problem. The check lives in the path that reads a
file on your desk after the flight. The path that takes bytes off a radio in
real time has no equivalent.&lt;/p&gt;
&lt;h2 id=&#34;part-8-what-the-receiver-does-about-it&#34;&gt;Part 8: What the receiver does about it&lt;/h2&gt;
&lt;p&gt;Nothing. That is the finding, and it took me a while to believe it.&lt;/p&gt;
&lt;p&gt;This particular run used a longer declared length than the 2-metre rung from
Part 6 — the coarser, less predictable setting, chosen here because it
happened to produce a clean, sizeable number against this base station. The
point below (whether the receiver&amp;rsquo;s quality flags notice) doesn&amp;rsquo;t depend on
which rung was used; it just needed one that was large enough to be
unmistakable in a table.&lt;/p&gt;
&lt;p&gt;I ran a clean data set and an attacked one all the way through, and compared
every single position the receiver produced:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;clean&lt;/th&gt;
&lt;th&gt;attacked&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;positions produced&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;flagged &amp;ldquo;RTK FIXED&amp;rdquo; (best quality)&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;flagged &amp;ldquo;float&amp;rdquo; (second best)&lt;/td&gt;
&lt;td&gt;68&lt;/td&gt;
&lt;td&gt;68&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;any quality flag different, at any moment?&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;no&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;how far the position moved&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2.435 m&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The injected error was 2.435 metres. The rover&amp;rsquo;s reported position moved by that
amount, exactly, and &lt;strong&gt;the quality flags were identical&lt;/strong&gt; — same count, same flag
at every single moment in the run. No dropouts. No degradation. No rejection.&lt;/p&gt;
&lt;p&gt;The receiver emits a position nearly two and a half metres from the truth, while
reporting precisely the confidence it reports when everything is perfect.&lt;/p&gt;
&lt;p&gt;It does not notice because there is nothing to notice. Its quality flags describe
how well it solved the geometry, and it solved the geometry beautifully. It has
no way to question the starting point it was handed.&lt;/p&gt;
&lt;h2 id=&#34;part-9-the-logs-are-watching-the-wrong-thing&#34;&gt;Part 9: The logs are watching the wrong thing&lt;/h2&gt;
&lt;p&gt;&amp;ldquo;Nothing is logged&amp;rdquo; is nearly true, and the precise version is worse.&lt;/p&gt;
&lt;p&gt;RTKLIB does have a diagnostic log, and the type 1033 decoder does write to it.
Turn the logging all the way up and you get one line per antenna message,
recording the text it received.&lt;/p&gt;
&lt;p&gt;That line cannot show the attack. It prints the &lt;em&gt;temporary holding buffer&lt;/em&gt; — the
one capped at 31 characters. So the log faithfully records up to 31 characters of
attacker-chosen text, which the attacker can make look completely ordinary. A
message declaring a length of 255 produces a log line indistinguishable from a
normal one.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The declared length — the number that causes all the damage — is never written
to any log, at any verbosity setting.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;And the step that actually does the harm, taking the corrupted coordinate and
putting it into service, has no logging at all. The only trace is a counter
ticking up by one.&lt;/p&gt;
&lt;p&gt;So: at default settings, nothing. With diagnostics compiled in and turned to
maximum, an operator gets one line recording a plausible-looking antenna name,
and still no record that the base station&amp;rsquo;s coordinates changed.&lt;/p&gt;
&lt;p&gt;The instrument exists. It is pointed in the wrong direction.&lt;/p&gt;
&lt;h2 id=&#34;part-10-the-compiler-setting-and-the-thing-i-got-wrong&#34;&gt;Part 10: The compiler setting, and the thing I got wrong&lt;/h2&gt;
&lt;p&gt;Now for the part that surprised me, because I predicted it wrongly in writing and
had to correct myself.&lt;/p&gt;
&lt;p&gt;Modern C compilers have an optional safety feature. Switch it on and, for certain
common operations, the compiler inserts a check: &lt;em&gt;does this copy actually fit
where it is going?&lt;/em&gt; If it does not fit, the program deliberately kills itself
rather than continue.&lt;/p&gt;
&lt;p&gt;The copy in this bug is exactly the kind that gets checked. So with that feature
active, this bug does not corrupt anything. The program simply dies. One message
is a remote crash — bad, but loud and obvious.&lt;/p&gt;
&lt;p&gt;I assumed the world divided along an obvious line: Windows applications built
without the feature would corrupt silently, and Linux builds would have it and
would crash. Linux distributions have formal policies requiring this hardening.
Embedded receivers run Linux. I wrote that prediction down.&lt;/p&gt;
&lt;p&gt;It is wrong. &lt;strong&gt;Every build I have measured corrupts silently.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Both Windows distributions.&lt;/strong&gt; I downloaded the official Windows tools and the
popular demo5 fork, fed them the malicious message, and watched them write a
corrupted base station coordinate into their own output files. Exit code zero, no
complaint. The feature belongs to the GNU C library; these are built with a
different compiler, against different libraries, where it doesn&amp;rsquo;t exist to
enable. It was never on the menu.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Debian&amp;rsquo;s own package.&lt;/strong&gt; This is the one that killed my prediction. Debian
packages RTKLIB, at exactly the version I had been studying, built for every
processor architecture they support. I pulled the ARM build apart and looked for
the hardened functions.&lt;/p&gt;
&lt;p&gt;There are none. Not one, anywhere in the binary. The ordinary unchecked versions
are all present. Debian&amp;rsquo;s package escapes the hardening its own policy nominally
requires — most likely because RTKLIB&amp;rsquo;s build files override the compiler
settings the distribution tries to inject.&lt;/p&gt;
&lt;p&gt;So installing this from your Linux distribution, on ARM, lands in the silent
corruption column alongside everything else.&lt;/p&gt;
&lt;p&gt;The crash is what you get by compiling the software yourself with the right
settings — nobody ships it.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;(One footnote for the suspicious: the internal layout that determines exactly
which erasures reach the coordinate turns out to be identical across every
processor architecture I checked — six of them, 32-bit and 64-bit alike. The same
malicious message works everywhere without adjustment.)&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&#34;part-11-flying-it&#34;&gt;Part 11: Flying it&lt;/h2&gt;
&lt;p&gt;Simulated positions on a screen only get you so far. I wanted to know what a
vehicle does.&lt;/p&gt;
&lt;p&gt;So I built a loop: a simulated drone&amp;rsquo;s true position feeds a synthetic GPS
signal generator, which feeds RTKLIB, which produces a position, which is fed
back to the drone&amp;rsquo;s autopilot as its only GPS. The malicious message gets spliced
into the correction stream mid-flight, exactly as it would arrive over a radio
link. The drone&amp;rsquo;s flight controller then computes its own false position from the
corrupted data and flies on that.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;        real base station                    ATTACKER
        (legitimate corrections)             (one fake antenna-name message)
                  |                                |
                  |                                |
                  +--------------+-----------------+
                                 |
                                 v
                        unauthenticated radio link
                                 |
                                 v
                        +------------------+
                        |     RTKLIB       |  &amp;lt;-- base coordinate corrupted
                        +------------------+      here, and adopted here
                                 |
                              position
                                 |
                                 v
                        +------------------+
                        |    autopilot     |
                        +------------------+
                                 |
                                 v
                           vehicle moves
                                 |
        +------------------------+
        |   (and its true position becomes the
        v    input to the GPS simulation, closing the loop)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Nothing is patched in by hand. The aircraft&amp;rsquo;s own position is what the simulated
satellites are computed from, and RTKLIB&amp;rsquo;s output is the aircraft&amp;rsquo;s only GPS — so
a corrupted base coordinate propagates all the way round into the flight control
loop and back out again.&lt;/p&gt;
&lt;p&gt;I flew it twice. Same aircraft, same flight plan, same malicious message. The
only difference was which version of RTKLIB was in the loop.&lt;/p&gt;
&lt;h3 id=&#34;the-version-everyone-runs&#34;&gt;The version everyone runs&lt;/h3&gt;
&lt;p&gt;The drone took off, hovered, received the malicious message, and carried on. When
told to fly to a specific point 30 metres away it flew there, announced arrival,
and landed normally.&lt;/p&gt;
&lt;p&gt;It was 1.4 metres from where it believed it was.&lt;/p&gt;
&lt;p&gt;Its flight log contains &lt;strong&gt;zero errors&lt;/strong&gt;. Not one warning, not one anomaly. GPS
quality reported as the highest possible value from takeoff to touchdown, without
a flicker.&lt;/p&gt;
&lt;h3 id=&#34;the-version-that-crashes&#34;&gt;The version that crashes&lt;/h3&gt;
&lt;p&gt;Same flight, hardened build. The malicious message arrives while the drone is
hovering:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;87.5 s   GPS quality: BEST -&amp;gt; NONE        (RTKLIB has just died)
91.4 s   ERROR: EKF primary changed
91.5 s   ERROR: EKF variance
91.5 s   ERROR: navigation failsafe
91.5 s   MODE -&amp;gt; LAND                     &#34;EKF Failsafe: changed to Land Mode&#34;
105.9 s  SmartRTL deactivated: bad position
120.6 s  Hit ground at 0.50 m/s
122.7 s  Disarming motors
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Four seconds from losing GPS to declaring an emergency. Thirty-three seconds from
losing GPS to sitting on the ground, descending at half a metre per second, which
is a landing rather than a fall. It abandoned the route it was flying. It logged
four distinct errors while doing it.&lt;/p&gt;
&lt;h3 id=&#34;which-is-worse&#34;&gt;Which is worse?&lt;/h3&gt;
&lt;p&gt;Instinct says the crash. Instinct is wrong.&lt;/p&gt;
&lt;p&gt;Every autonomous system is built to handle &lt;em&gt;&amp;ldquo;my sensor stopped working.&amp;rdquo;&lt;/em&gt; That is
an expected condition with a plan attached: raise an alarm, stop, hand back
control, land. Here it fired in four seconds and the aircraft put itself down
safely.&lt;/p&gt;
&lt;p&gt;Essentially nothing is built to handle &lt;em&gt;&amp;ldquo;my sensor is confidently wrong,&amp;rdquo;&lt;/em&gt; because
there is no signal to react to. No alarm can fire when every reading looks
perfect.&lt;/p&gt;
&lt;p&gt;So on the question of &lt;em&gt;whether anyone finds out&lt;/em&gt;, the version that crashes wins
outright — and it is the version nobody ships.&lt;/p&gt;
&lt;h2 id=&#34;part-12-but-the-crash-is-its-own-attack&#34;&gt;Part 12: But the crash is its own attack&lt;/h2&gt;
&lt;p&gt;I nearly stopped there, and stopping there would have been wrong. Look again at
one line from the crashed drone&amp;rsquo;s log:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;105.9 s  SmartRTL deactivated: bad position
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With no position, the drone &lt;strong&gt;could not fly home&lt;/strong&gt;. It did not know where home
was. It had exactly one option: land immediately.&lt;/p&gt;
&lt;p&gt;Now ask who decides &lt;em&gt;when&lt;/em&gt; that happens.&lt;/p&gt;
&lt;p&gt;The attacker does. It happens the moment they transmit. So an attacker who can
see the target can wait until it is over a lake, or a river, or the far side of a
fence, and then send one message to bring it down there.&lt;/p&gt;
&lt;p&gt;I measured how well that can be aimed. With no position estimate the drone has no
way to hold station, so it drifts for the whole descent:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;height when the emergency fired&lt;/td&gt;
&lt;td&gt;15 metres&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;descent&lt;/td&gt;
&lt;td&gt;29 seconds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;sideways drift before touchdown&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;10.13 metres&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;character of the drift&lt;/td&gt;
&lt;td&gt;one direction, never corrected&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;So the attacker selects a &lt;strong&gt;circle tens of metres across&lt;/strong&gt;, not a spot. Good
enough for a lake or a field. Not good enough for a specific lane of traffic.&lt;/p&gt;
&lt;p&gt;And 10 metres is the &lt;strong&gt;best case&lt;/strong&gt;. The simulation had no wind, the drone was
low, and it was nearly stationary when it was hit. Wind, height, or a moving
target all make it worse. One run is one measurement, not a guarantee.&lt;/p&gt;
&lt;p&gt;A human watching can still rescue it — the aircraft flies fine by hand once you
stop asking it where it is, and the alarms are loud. An automated flight with
nobody watching cannot be rescued.&lt;/p&gt;
&lt;h3 id=&#34;two-weapons-and-no-safe-option&#34;&gt;Two weapons, and no safe option&lt;/h3&gt;
&lt;p&gt;The two builds hand an attacker &lt;strong&gt;different tools&lt;/strong&gt;. Neither one is the
harmless case.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;                    ONE MALICIOUS MESSAGE
                             |
              +--------------+--------------+
              |                             |
       build without the            build with the
       safety check                 safety check
       (what everyone ships)        (compile it yourself)
              |                             |
              v                             v
       coordinate quietly            the program dies
       wrong by ~2 m                        |
              |                             v
              v                      aircraft loses GPS,
       vehicle flies on,             cannot fly home,
       reports success,              lands within ~10 m
       logs ZERO errors              of the attacker&#39;s choosing
              |                             |
              v                             v
      +----------------+           +------------------+
      | SILENT ATTACK  |           |   LOUD ATTACK    |
      | on ACCURACY    |           | on AVAILABILITY  |
      +----------------+           +------------------+
       nobody finds out             everybody finds out,
                                    but it is on the ground
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;the version that ships&lt;/th&gt;
&lt;th&gt;the hardened version&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;what it does&lt;/td&gt;
&lt;td&gt;moves the reported position&lt;/td&gt;
&lt;td&gt;brings the aircraft down&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;precision&lt;/td&gt;
&lt;td&gt;exact, attacker&amp;rsquo;s choice&lt;/td&gt;
&lt;td&gt;~10 m, or worse&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;magnitude&lt;/td&gt;
&lt;td&gt;~2 m before it starts looking wrong&lt;/td&gt;
&lt;td&gt;total — it stops flying&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;does anyone notice?&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;no&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;yes, immediately&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;useful for&lt;/td&gt;
&lt;td&gt;quietly ruining survey data, agriculture, landing accuracy, for as long as you like&lt;/td&gt;
&lt;td&gt;taking an aircraft out of the air at a chosen moment&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Which leads somewhere uncomfortable. &lt;strong&gt;If your goal is to put a drone in a lake,
the hardened build is the better tool for the job.&lt;/strong&gt; Two metres of silent error
will not do it. A forced landing will.&lt;/p&gt;
&lt;p&gt;Turning on the compiler&amp;rsquo;s safety feature does not fix this bug. It exchanges a
quiet attack for a loud one. That is a trade genuinely worth making if what you
care about is knowing you were attacked — which is most people, most of the time
— but it should be described as what it is.&lt;/p&gt;
&lt;h2 id=&#34;part-13-the-actual-fix&#34;&gt;Part 13: The actual fix&lt;/h2&gt;
&lt;p&gt;Five lines. Refuse to believe the declared length if it is bigger than what was
actually read:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n2&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n2&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n3&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n3&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;31&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The holding buffers are 32 characters, so this only stops the copy from claiming
more than ever arrived. I built the software with and without this change and ran
the same data through both: the attacked streams come back at the true
coordinate, and legitimate messages decode identically, so the change costs
nothing that a real base station would notice.&lt;/p&gt;
&lt;p&gt;I posted the analysis publicly on the bug report on 2026-07-27. As of
2026-08-31 there had been no reply, so I filed the fix as
&lt;a href=&#34;https://github.com/tomojitakasu/RTKLIB/pull/801&#34;&gt;PR #801&lt;/a&gt; — filed, not
merged.&lt;/p&gt;
&lt;h2 id=&#34;part-14-where-this-actually-stands&#34;&gt;Part 14: Where this actually stands&lt;/h2&gt;
&lt;p&gt;None of this is a secret. The bugs were reported publicly in June 2026 and the
analysis has been public since.&lt;/p&gt;
&lt;p&gt;All four reports have been open and unpatched since 2026-06-09. Each carries a
single comment from the person who found them, offering to file the paperwork
for an official vulnerability identifier and asking the maintainer how they&amp;rsquo;d
like to handle it. As of 2026-08-31, neither that comment nor mine (posted
2026-07-27 on #799) has drawn a reply.&lt;/p&gt;
&lt;p&gt;The demo5 fork shipped a new release on 2026-06-22 — &lt;strong&gt;thirteen days after&lt;/strong&gt;
the bugs were made public — with the flaw untouched.&lt;/p&gt;
&lt;p&gt;So &amp;ldquo;wait for the patch&amp;rdquo; is not advice that means anything here. There is nobody
to wait for. I would rather say that plainly than imply a fix is on its way.&lt;/p&gt;
&lt;p&gt;The same code is also present, unchanged, in Emlid&amp;rsquo;s public fork of RTKLIB —
same flaw, same internal layout, same missing check on the live path. For a
long time that was an observation about published source code only: the source
is affected, which is a different claim from any particular product behaving
that way.&lt;/p&gt;
&lt;p&gt;That changed for one Emlid product. Their firmware images are served
publicly with no purchase or login required. I downloaded one — Reach Plus
(RS+/M+), version 2.18.1, dated 2019 — opened the disk image without root
(partition carved with &lt;code&gt;dd&lt;/code&gt;, read with &lt;code&gt;debugfs&lt;/code&gt;), and found &lt;code&gt;librtk.so&lt;/code&gt;:
RTKLIB 2.4.3, Emlid&amp;rsquo;s own build, compiled for the receiver&amp;rsquo;s ARM processor.
The decoder is in there — inlined into the message dispatcher rather than
kept as its own named function, confirmed by tracing both of its diagnostic
strings back to live code inside it — and the library imports not one
fortified libc symbol. Same silent-corruption result as everything else
measured in this piece, this time on an actual shipped product&amp;rsquo;s actual
firmware.&lt;/p&gt;
&lt;p&gt;Reach M2 and RS2+ — the models currently for sale — are not yet checked.
Neither is whether Emlid&amp;rsquo;s current firmware differs from this 2019 build.
&amp;ldquo;The source is affected&amp;rdquo; now has one specific, dated exception where it&amp;rsquo;s
stronger than that; it hasn&amp;rsquo;t become &amp;ldquo;the product line behaves this way&amp;rdquo;
across the board.&lt;/p&gt;
&lt;p&gt;Later the same day: reading the binary says what it &lt;em&gt;would&lt;/em&gt; do. I wanted to
watch it actually do it. &lt;code&gt;qemu-aarch64-static&lt;/code&gt; — downloaded as a &lt;code&gt;.deb&lt;/code&gt; and
unpacked by hand, no root needed anywhere in this — will run one aarch64
binary under emulation on an ordinary x86 machine, resolving its shared
libraries from a directory you point it at. Pointed that directory at the
Reach Plus filesystem, ran the real &lt;code&gt;rtkrcv&lt;/code&gt; binary from the real firmware
against its own real configuration file, and fed it a correction stream
carrying the same malicious message over a local TCP connection standing in
for the radio link. Then asked &lt;code&gt;rtkrcv&lt;/code&gt; itself — its own console, the same
interface a real operator would use — what it currently believed the base
station&amp;rsquo;s position to be:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;pos xyz (m) base : -2694684.000,-4293642.366,3857878.924
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The true position was &lt;code&gt;-2694685.4730,-4293642.3660,3857878.9240&lt;/code&gt;. One axis
moved &lt;strong&gt;1.473 metres&lt;/strong&gt;, live, watched, on the actual shipped program. The
message counters matched what I&amp;rsquo;d sent exactly, and the console&amp;rsquo;s own error
log had nothing in it. I shut it down cleanly afterward with its own
&lt;code&gt;shutdown&lt;/code&gt; command; nothing crashed.&lt;/p&gt;
&lt;p&gt;This section started with a claim about source code, then a claim about
firmware contents. It ends with the actual program running and reporting the
corrupted position itself. What&amp;rsquo;s still open: Reach M2 and RS2+ remain
unchecked, and this was emulation, not the receiver itself powered on. Both
matter; neither erases what the emulator run settled.&lt;/p&gt;
&lt;p&gt;Reach is one confirmed instance of something bigger than one product. RTKLIB
is permissively licensed, which is exactly what lets it get folded into closed
firmware that never uses its name — one way to find another instance is the
way this one turned up: look at what a device actually ships, not what a
spec sheet claims. Precision-agriculture autosteer, survey and construction
stakeout, marine autopilots, and RTK-equipped mapping drones all draw from the
same small pool of open positioning code Reach draws from. Which of them,
specifically, remains untested.&lt;/p&gt;
&lt;h2 id=&#34;part-15-what-i-did-not-do&#34;&gt;Part 15: What I did not do&lt;/h2&gt;
&lt;p&gt;Worth being blunt about, because a lot of security writing is vaguer here than it
should be.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No aircraft, real or simulated-into-the-real-world, was ever attacked
  live.&lt;/strong&gt; Three things have since happened that this draft doesn&amp;rsquo;t narrate
  at the top yet: two small real radios sent the real malicious message
  over the air into RTKLIB&amp;rsquo;s own unmodified decoder (see
  &lt;code&gt;research/README.md&lt;/code&gt;, &amp;ldquo;Over a serial link&amp;rdquo; / the radio delivery work);
  one real receiver&amp;rsquo;s firmware was downloaded and read (Part 14, above);
  and that same receiver&amp;rsquo;s actual binary was then run, under emulation,
  against a real correction stream, with its own console confirming the
  live corruption (Part 14, later addendum). None of the three is a live
  attack on a powered-on device with a propeller attached.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;One base station, one rover, one data set&lt;/strong&gt;, and an old one. Whether the same
  invisibility holds across other satellite constellations and modern
  multi-frequency receivers is untested.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The drone results are one flight each.&lt;/strong&gt; One airframe, one flight profile,
  stock settings. A different vehicle or different emergency-response settings
  would give different numbers. The &lt;em&gt;direction&lt;/em&gt; of the result is the claim; the
  exact seconds are illustrative.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The 10-metre landing dispersion is a single sample in zero wind.&lt;/strong&gt; Treat it
  as a floor, not a bound. A real figure would need many runs across different
  heights and wind conditions, which I have not done.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The size of the error depends on the specific base station&lt;/strong&gt;, because what is
  being erased is the digits of its particular coordinate.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;why-i-think-it-matters&#34;&gt;Why I think it matters&lt;/h2&gt;
&lt;p&gt;The bug itself is small and the fix is five lines. What is interesting is the
shape of it.&lt;/p&gt;
&lt;p&gt;Every safety check in this system is aimed at the quality of the measurements.
Are the satellites healthy? Is the geometry good? Did the maths converge? All of
those checks passed, throughout, in every attacked run I did — because the
measurements really were fine.&lt;/p&gt;
&lt;p&gt;Nothing checks the &lt;em&gt;assumption&lt;/em&gt; the measurements are built on. The base station&amp;rsquo;s
coordinate arrives over an unauthenticated link, gets adopted without a glance,
and every downstream check then dutifully confirms that the arithmetic performed
on it was correct.&lt;/p&gt;
&lt;p&gt;That is not unique to GPS software. It is what most systems look like: heavily
instrumented at the point where errors are expected, and completely blind at the
point where a value is simply taken on faith.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;em&gt;Corrections welcome. Everything above is reproducible, and if you can show me a
shipped build that lands in the crash column, I would genuinely like to hear
about it — I have not found one. The full technical writeup — exact source,
struct offsets across six architectures, the reproduction harness, sanitizer
output, and every script used to generate a number in this piece — is at
[TODO: link once the research repo is public].&lt;/em&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>I Built a Fall Detector. The Data Killed It.</title>
    <link href="https://reachtim.com/articles/fall-detector.html" rel="alternate" />
    <id>https://reachtim.com/articles/fall-detector.html</id>
    <published>2026-08-30T00:00:00Z</published>
    <updated>2026-08-30T00:00:00Z</updated>
    <category term="Hardware" />
    <summary type="text">Twenty-five false alarms in five hours, and it still missed half the falls in a 523-fall dataset. Why wrist-worn fall detection failed for me, why it fails for almost everyone, and what survived the wreck.</summary>
    <content type="html">&lt;p&gt;I set the watch down on my desk, face up, the way you&amp;rsquo;d put down a phone. Ten
seconds later it buzzed, lit up, and started counting down to calling my family.&lt;/p&gt;
&lt;p&gt;Nothing had happened. Nobody had fallen. And it took me longer than I&amp;rsquo;d like to
admit to accept that the detector had not malfunctioned — it had done exactly
what I built it to do. A watch dropped onto a hard desk &lt;strong&gt;is&lt;/strong&gt; a fall, as far as
an accelerometer strapped to a wrist can tell. There was a sharp deceleration,
the orientation changed from upright to flat, and then it lay perfectly still.
Every stage of my carefully staged detection cascade fired in order, and the
cascade was right about every one of them.&lt;/p&gt;
&lt;p&gt;This is what came of building a wrist-worn fall detector, measuring it properly,
and concluding that it should never be allowed to page anyone. I&amp;rsquo;ll walk through
how these things are supposed to work, the two numbers that killed mine, and the
part I genuinely did not expect: the published research has been saying the same
thing since 2008, and mostly nobody acts on it.&lt;/p&gt;
&lt;h2 id=&#34;what-i-was-building-and-why&#34;&gt;What I was building, and why&lt;/h2&gt;
&lt;p&gt;A family member has limited mobility and lives at home. The goal was
unglamorous — a watch that notices a fall, waits a few seconds in case it&amp;rsquo;s
wrong, and escalates to a small relay server that texts and calls a list of
family contacts if nobody cancels.&lt;/p&gt;
&lt;p&gt;The hardware is a LilyGO T-Watch Ultra: an ESP32-S3, a Bosch BHI260AP motion
sensor, a screen, a vibration motor, a LoRa radio, and a battery. The firmware
is mine. The detection logic lives in plain C with no Arduino or FreeRTOS
dependencies, so it compiles on my laptop and I can replay recorded motion
through it in a test harness. There are signed over-the-air updates that refuse
to install older versions, HMAC-signed alerts with replay protection, and a
journal that writes an alert to flash &lt;em&gt;before&lt;/em&gt; it tries to send it, so a
network outage can&amp;rsquo;t quietly swallow one.&lt;/p&gt;
&lt;p&gt;All of that works. None of it is what went wrong.&lt;/p&gt;
&lt;h2 id=&#34;how-fall-detection-is-supposed-to-work&#34;&gt;How fall detection is supposed to work&lt;/h2&gt;
&lt;p&gt;Nearly every accelerometer-based fall detector is a state machine looking for
the same four-part signature, in order. Mine was built from the published
descriptions, and it looks like this.&lt;/p&gt;
&lt;figure class=&#34;diagram&#34; markdown=&#34;0&#34;&gt;
&lt;svg viewBox=&#34;0 0 880 280&#34; role=&#34;img&#34; aria-labelledby=&#34;casc-title casc-desc&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;title id=&#34;casc-title&#34;&gt;The four-stage fall detection cascade&lt;/title&gt;
&lt;desc id=&#34;casc-desc&#34;&gt;Four stages in sequence — free-fall, impact, orientation change, and stillness — each gating the next. Below them, the same four stages shown being satisfied by the ordinary act of setting the watch down on a desk, ending in a countdown to calling the family.&lt;/desc&gt;
&lt;style&gt;text{font-family:var(--font-mono);}&lt;/style&gt;
&lt;text x=&#34;40&#34; y=&#34;24&#34; font-size=&#34;11&#34; letter-spacing=&#34;0.14em&#34; fill=&#34;var(--muted)&#34;&gt;THE CASCADE&lt;/text&gt;

&lt;rect x=&#34;40&#34; y=&#34;44&#34; width=&#34;170&#34; height=&#34;58&#34; rx=&#34;4&#34; fill=&#34;none&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;125&#34; y=&#34;72&#34; font-size=&#34;13&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;1 · FREE-FALL&lt;/text&gt;
&lt;text x=&#34;125&#34; y=&#34;90&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;under 0.6 g, 80 ms&lt;/text&gt;

&lt;rect x=&#34;250&#34; y=&#34;44&#34; width=&#34;170&#34; height=&#34;58&#34; rx=&#34;4&#34; fill=&#34;none&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;335&#34; y=&#34;72&#34; font-size=&#34;13&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;2 · IMPACT&lt;/text&gt;
&lt;text x=&#34;335&#34; y=&#34;90&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;a spike over 4 g&lt;/text&gt;

&lt;rect x=&#34;460&#34; y=&#34;44&#34; width=&#34;170&#34; height=&#34;58&#34; rx=&#34;4&#34; fill=&#34;none&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;545&#34; y=&#34;72&#34; font-size=&#34;13&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;3 · ORIENTATION&lt;/text&gt;
&lt;text x=&#34;545&#34; y=&#34;90&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;gravity turns 40°&lt;/text&gt;

&lt;rect x=&#34;670&#34; y=&#34;44&#34; width=&#34;170&#34; height=&#34;58&#34; rx=&#34;4&#34; fill=&#34;none&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;755&#34; y=&#34;72&#34; font-size=&#34;13&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;4 · STILLNESS&lt;/text&gt;
&lt;text x=&#34;755&#34; y=&#34;90&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;quiet for 8 s&lt;/text&gt;

&lt;line x1=&#34;214&#34; y1=&#34;73&#34; x2=&#34;240&#34; y2=&#34;73&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;polygon points=&#34;240,69 248,73 240,77&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;line x1=&#34;424&#34; y1=&#34;73&#34; x2=&#34;450&#34; y2=&#34;73&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;polygon points=&#34;450,69 458,73 450,77&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;line x1=&#34;634&#34; y1=&#34;73&#34; x2=&#34;660&#34; y2=&#34;73&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;polygon points=&#34;660,69 668,73 660,77&#34; fill=&#34;var(--line-strong)&#34;/&gt;

&lt;text x=&#34;40&#34; y=&#34;150&#34; font-size=&#34;11&#34; letter-spacing=&#34;0.14em&#34; fill=&#34;var(--muted)&#34;&gt;WHAT HAPPENS WHEN I SET IT ON A DESK&lt;/text&gt;

&lt;text x=&#34;125&#34; y=&#34;180&#34; font-size=&#34;16&#34; fill=&#34;var(--teal)&#34; text-anchor=&#34;middle&#34;&gt;✓&lt;/text&gt;
&lt;text x=&#34;125&#34; y=&#34;200&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;the drop from&lt;/text&gt;
&lt;text x=&#34;125&#34; y=&#34;216&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;my hand&lt;/text&gt;

&lt;text x=&#34;335&#34; y=&#34;180&#34; font-size=&#34;16&#34; fill=&#34;var(--teal)&#34; text-anchor=&#34;middle&#34;&gt;✓&lt;/text&gt;
&lt;text x=&#34;335&#34; y=&#34;200&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;the tap on&lt;/text&gt;
&lt;text x=&#34;335&#34; y=&#34;216&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;hard wood&lt;/text&gt;

&lt;text x=&#34;545&#34; y=&#34;180&#34; font-size=&#34;16&#34; fill=&#34;var(--teal)&#34; text-anchor=&#34;middle&#34;&gt;✓&lt;/text&gt;
&lt;text x=&#34;545&#34; y=&#34;200&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;wrist upright to&lt;/text&gt;
&lt;text x=&#34;545&#34; y=&#34;216&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;screen flat&lt;/text&gt;

&lt;text x=&#34;755&#34; y=&#34;180&#34; font-size=&#34;16&#34; fill=&#34;var(--teal)&#34; text-anchor=&#34;middle&#34;&gt;✓&lt;/text&gt;
&lt;text x=&#34;755&#34; y=&#34;200&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;a desk does not&lt;/text&gt;
&lt;text x=&#34;755&#34; y=&#34;216&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;move at all&lt;/text&gt;

&lt;text x=&#34;440&#34; y=&#34;258&#34; font-size=&#34;12.5&#34; fill=&#34;var(--brick)&#34; text-anchor=&#34;middle&#34;&gt;a countdown starts, and then the family gets a call&lt;/text&gt;
&lt;/svg&gt;
&lt;figcaption&gt;Each stage gates the next, so nothing alerts on a single spike. The trouble is that the four conditions describe an object arriving on a hard surface, which is a much larger category of event than a person collapsing.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The logic is sound and the staging genuinely helps. A gesture that spikes past
4 g does nothing, because no free-fall preceded it. A free-fall with a soft
landing does nothing, because no impact followed. Getting straight back up
after a stumble cancels the whole thing at stage four.&lt;/p&gt;
&lt;p&gt;Read the sequence again, though, and notice what it actually describes: &lt;em&gt;an
object that dropped, hit something hard, ended up at a different angle, and
then stopped moving.&lt;/em&gt; A person collapsing on a kitchen floor fits. So does a
watch meeting a desk.&lt;/p&gt;
&lt;h2 id=&#34;the-first-number-how-often-it-cried-wolf&#34;&gt;The first number: how often it cried wolf&lt;/h2&gt;
&lt;p&gt;I wore it for a day and logged every verdict.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Twenty-five alerts in five hours and twenty minutes.&lt;/strong&gt; One false alarm every
thirteen minutes.&lt;/p&gt;
&lt;p&gt;The same watch, left on a table for fifteen hours, produced zero. Every one of
those alarms was caused by ordinary living — setting a mug down hard, dropping
my arm onto the armrest of a chair, putting the watch on and taking it off,
resting a hand flat on a table and leaving it there.&lt;/p&gt;
&lt;p&gt;Any one of those, on its own, would be a tuning problem. You raise a threshold,
you lengthen a window, you move on. Twenty-five an hour and a half is a
different kind of message.&lt;/p&gt;
&lt;h2 id=&#34;the-second-number-how-often-it-caught-an-actual-fall&#34;&gt;The second number: how often it caught an actual fall&lt;/h2&gt;
&lt;p&gt;The false alarm rate is the easy half to measure, because you only have to wear
the thing. Measuring the other half — how many real falls it catches — means
having real falls, and I am not going to produce those on demand.&lt;/p&gt;
&lt;p&gt;So I used a public research dataset,
&lt;a href=&#34;https://ieee-dataport.org/open-access/fallalld-comprehensive-dataset-human-falls-and-activities-daily-living&#34;&gt;FallAllD&lt;/a&gt;,
which recorded 15 volunteers performing falls and everyday activities with
sensors at the wrist, the waist, and on a neck pendant simultaneously. I pulled the wrist channel, converted
every trial into my replay format, and ran the exact production detection code
over all of it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;258 of 523 falls reached the alerting verdict. 49.3%.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&#34;where-that-data-comes-from-exactly&#34;&gt;Where that data comes from, exactly&lt;/h3&gt;
&lt;p&gt;Since the whole argument rests on this dataset, here is precisely what it is.&lt;/p&gt;
&lt;p&gt;FallAllD was collected by Majd Saleh and Régine Le Bouquin Jeannes, published
open-access on IEEE DataPort in April 2020, and described in &lt;em&gt;IEEE Sensors
Journal&lt;/em&gt; the following year. Fifteen participants wore three data-loggers
simultaneously — waist, wrist, and neck — each carrying an accelerometer,
gyroscope, magnetometer, and barometer. It runs to 26,420 files.&lt;/p&gt;
&lt;p&gt;Reading the archive rather than trusting a description of it, the structure is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;One file per sensor per trial, named like &lt;code&gt;S01_D1_A013_T01_A.dat&lt;/code&gt; — subject,
  device, activity, trial, sensor. Accelerometer and gyroscope arrive as
  &lt;strong&gt;separate&lt;/strong&gt; files and have to be paired; there is no combined record.&lt;/li&gt;
&lt;li&gt;Each is a headerless three-column CSV of raw 16-bit integers, exactly 4,760
  rows — &lt;strong&gt;20.000 seconds at 238 Hz&lt;/strong&gt;, constant across every file I checked.&lt;/li&gt;
&lt;li&gt;Activity IDs split cleanly: 001–044 are everyday activities, 101–135 are the
  35 fall types. Seventy-nine IDs across fifteen subjects.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Three things about it are worth stating plainly, because each one could have
quietly produced a wrong headline number.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The falls are simulated.&lt;/strong&gt; Participants fell deliberately, knowing it was
coming. This is standard practice and it is the only ethical way to build a
dataset like this, but it is exactly the gap Bagalà&amp;rsquo;s study is about.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;No README ships in the archive, so which device is the wrist is inferred.&lt;/strong&gt; I
worked it out from three independent signals that agree: twelve activities exist
only on device 2 and are all wrist-specific gestures the torso mounts never saw;
device 2 has the highest gyroscope RMS of the three; and device 2 shows by far
the widest spread of mean gravity direction across trials, which is the
signature of a mount that rotates freely. Waist and neck mounts do not do that.
Reading the wrong device would have silently produced a &lt;em&gt;waist&lt;/em&gt;-mounted recall
figure and I would never have known.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The raw values are integers, and the scale factor is not given.&lt;/strong&gt; My converter
refuses to run without an explicit scale, and there is a separate step that
infers it from physics — a resting wrist must read 1 g — and reports every
candidate against that standard. The scale turned out to be 1/4096 g per count.
Getting this wrong by a factor of 4096 produces a perfectly plausible-looking
recall number computed from nonsense, with nothing on the surface to show it.&lt;/p&gt;
&lt;p&gt;I also left the data at its native 238 Hz rather than resampling to the 100 Hz
my firmware runs at. I tried resampling first, and measuring it showed the
interpolation was quietly shaving peaks off impacts — the thing the detector
keys on.&lt;/p&gt;
&lt;p&gt;It missed slightly more than half. And here is the detail that mattered most to
me: of the falls it missed completely, &lt;strong&gt;61% had an impact peak comfortably
above the 4 g threshold.&lt;/strong&gt; The detector saw the impact perfectly well. It threw
the event away at the orientation stage.&lt;/p&gt;
&lt;h2 id=&#34;both-numbers-at-once&#34;&gt;Both numbers at once&lt;/h2&gt;
&lt;p&gt;Either number alone leaves you somewhere to go.&lt;/p&gt;
&lt;p&gt;A detector that catches 49% of falls but is otherwise silent is a weak safety
net that might still be worth wearing. A detector that catches nearly
everything but shouts too often is annoying, and you tighten the thresholds and
trade some sensitivity for quiet.&lt;/p&gt;
&lt;p&gt;Mine did both simultaneously. It missed half of what it existed for &lt;strong&gt;while&lt;/strong&gt;
paging roughly three times an hour. There&amp;rsquo;s no threshold adjustment that
resolves that, because the two problems pull in opposite directions and neither
one has any slack. Tightening it to silence the false alarms pushes recall below
49%. Loosening it to catch more falls makes an already unusable alarm rate
worse.&lt;/p&gt;
&lt;p&gt;There is no operating point. That&amp;rsquo;s the whole finding, and it took measuring
both axes to see it — which is precisely why I think most people never see it.&lt;/p&gt;
&lt;h2 id=&#34;why-it-misses-your-wrist-doesnt-know-what-your-body-is-doing&#34;&gt;Why it misses: your wrist doesn&amp;rsquo;t know what your body is doing&lt;/h2&gt;
&lt;p&gt;Stage three is where the falls died, and stage three is the one carrying an
assumption I&amp;rsquo;d never examined: &lt;strong&gt;that the sensor&amp;rsquo;s orientation tells you the
body&amp;rsquo;s orientation.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Strap a sensor to somebody&amp;rsquo;s waist and that assumption is close enough to true.
The torso is upright when standing and horizontal when lying down, and the
gravity vector rotates roughly 90° between the two.&lt;/p&gt;
&lt;p&gt;Your wrist does no such thing. Your arm rotates through most of that range while
you&amp;rsquo;re making a sandwich. It can end a genuine fall pointing in almost any
direction, including the one it started in — if you fall while reaching for a
railing, your forearm may finish more or less where it began, and the stage sees
nothing worth reporting.&lt;/p&gt;
&lt;p&gt;I could test this directly, because FallAllD recorded all three sensor positions
during the same falls. Same subjects, same events, matched trial by trial.&lt;/p&gt;
&lt;figure class=&#34;diagram&#34; markdown=&#34;0&#34;&gt;
&lt;svg viewBox=&#34;0 0 880 240&#34; role=&#34;img&#34; aria-labelledby=&#34;mount-title mount-desc&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;title id=&#34;mount-title&#34;&gt;Recall by sensor mounting position&lt;/title&gt;
&lt;desc id=&#34;mount-desc&#34;&gt;A bar chart of the same detector&#39;s recall on identical matched falls at three sensor positions: wrist 44.4 percent, waist 54.2 percent, and neck pendant 63.1 percent.&lt;/desc&gt;
&lt;style&gt;text{font-family:var(--font-mono);}&lt;/style&gt;
&lt;text x=&#34;40&#34; y=&#34;24&#34; font-size=&#34;11&#34; letter-spacing=&#34;0.14em&#34; fill=&#34;var(--muted)&#34;&gt;SAME DETECTOR, SAME FALLS, THREE MOUNTS (n=214 MATCHED TRIALS)&lt;/text&gt;

&lt;rect x=&#34;180&#34; y=&#34;106&#34; width=&#34;100&#34; height=&#34;84&#34; fill=&#34;var(--brick)&#34;/&gt;
&lt;text x=&#34;230&#34; y=&#34;96&#34; font-size=&#34;15&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;44.4%&lt;/text&gt;
&lt;text x=&#34;230&#34; y=&#34;212&#34; font-size=&#34;12.5&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;wrist&lt;/text&gt;

&lt;rect x=&#34;400&#34; y=&#34;87&#34; width=&#34;100&#34; height=&#34;103&#34; fill=&#34;var(--teal)&#34; opacity=&#34;0.55&#34;/&gt;
&lt;text x=&#34;450&#34; y=&#34;77&#34; font-size=&#34;15&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;54.2%&lt;/text&gt;
&lt;text x=&#34;450&#34; y=&#34;212&#34; font-size=&#34;12.5&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;waist&lt;/text&gt;

&lt;rect x=&#34;620&#34; y=&#34;70&#34; width=&#34;100&#34; height=&#34;120&#34; fill=&#34;var(--teal)&#34;/&gt;
&lt;text x=&#34;670&#34; y=&#34;60&#34; font-size=&#34;15&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;63.1%&lt;/text&gt;
&lt;text x=&#34;670&#34; y=&#34;212&#34; font-size=&#34;12.5&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;pendant&lt;/text&gt;

&lt;line x1=&#34;150&#34; y1=&#34;190&#34; x2=&#34;790&#34; y2=&#34;190&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;/svg&gt;
&lt;figcaption&gt;Moving the sensor from the wrist to the chest is worth about nineteen points of recall, which confirms the mechanism. It also lands well short of anything you&#39;d trust with someone&#39;s safety. On matched everyday activities the three mounts were indistinguishable (1.5%, 1.7%, 1.3%), so this says nothing about which mount raises fewer false alarms.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Nineteen points sitting in the mounting position tells you the orientation stage
was the problem all along. It also tells you that relocating the sensor doesn&amp;rsquo;t
rescue the idea — a pendant still misses more than a third of falls, and comes
with its own well-known problem, which is that people don&amp;rsquo;t wear pendants.
Stigma, forgetting, taking it off in the shower and leaving it there, leaving it
on the charger through the night.&lt;/p&gt;
&lt;p&gt;Wear rate doesn&amp;rsquo;t sit beside the recall number; it multiplies against it.
Fleming and Brayne
followed 110 people over ninety for a year and found that 82% of falls happened
while the person was alone, and that &lt;strong&gt;80% of those who fell and had a call
button did not use it&lt;/strong&gt; — not wearing it, knocked unconscious, or too shaken by
the fall to press. Those were manual buttons, so the figure measures wearing and
not detecting, but it&amp;rsquo;s the same term in the same product: whatever recall you
achieve, you multiply it by the fraction of falls where the device was on the
body at all.&lt;/p&gt;
&lt;p&gt;I haven&amp;rsquo;t measured that fraction for my own watch and I&amp;rsquo;m not going to invent
one, but the direction is clear without it. The pendant&amp;rsquo;s nineteen-point lead
survives only if the pendant is worn, and the best real-world evidence says it
mostly isn&amp;rsquo;t — which is exactly why wrist devices dominate the market despite
worse physics. Neither mount produces a number you&amp;rsquo;d stake someone&amp;rsquo;s safety on.&lt;/p&gt;
&lt;p&gt;None of this was new, either. In 2008, Kangas and colleagues ran essentially the
same comparison — accelerometers at the waist, the wrist and the head, threshold
algorithms simpler than mine — and got 97–98% sensitivity at the waist and the
head against staged falls by three middle-aged volunteers. Those top-line
numbers carry exactly the caveat this article is about. Their finding on the
third position needs no such caveat: the wrist, they concluded, &amp;ldquo;did not appear
to be an applicable site for fall detection.&amp;rdquo; Eighteen years before I measured
44.4%, that sentence was sitting in &lt;em&gt;Gait &amp;amp; Posture&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&#34;this-is-not-just-my-detector&#34;&gt;This is not just my detector&lt;/h2&gt;
&lt;p&gt;Here&amp;rsquo;s the part I wasn&amp;rsquo;t expecting, and the reason I&amp;rsquo;m writing this up rather
than quietly deleting the repo.&lt;/p&gt;
&lt;p&gt;In 2012, Fabio Bagalà and colleagues published a study in &lt;em&gt;PLOS ONE&lt;/em&gt;,
&lt;a href=&#34;https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0037062&#34;&gt;Evaluation of Accelerometer-Based Fall Detection Algorithms on Real-World Falls&lt;/a&gt;,
that took &lt;strong&gt;thirteen already-published fall detection algorithms&lt;/strong&gt; — algorithms
with strong reported results — and ran them against real falls recorded from
high-fall-risk patients going about their lives.&lt;/p&gt;
&lt;p&gt;Sensitivity collapsed to &lt;strong&gt;57.0% on average&lt;/strong&gt;, with the best of the thirteen
reaching 82.8%. And when they monitored three representative fallers for a day
each, the algorithms produced between &lt;strong&gt;3 and 85 false alarms per day.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Set that beside my results:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Bagalà 2012, thirteen published algorithms&lt;/th&gt;
&lt;th&gt;Mine, 2026&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Falls caught&lt;/td&gt;
&lt;td&gt;57.0% average&lt;/td&gt;
&lt;td&gt;49.3%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False alarms&lt;/td&gt;
&lt;td&gt;3–85 per day&lt;/td&gt;
&lt;td&gt;~25 in 5h20m&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;I landed inside their range on both axes, at the wrist, fourteen years later,
with a considerably better sensor. I had independently reproduced a result that
was already in the literature — which is reassuring about my measurement and
depressing about the state of the field.&lt;/p&gt;
&lt;p&gt;Two details make my number worse:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;My 49.3% was measured against staged falls.&lt;/strong&gt; FallAllD volunteers fell
  deliberately onto padding. Bagalà&amp;rsquo;s central point is that staged falls are the
  &lt;em&gt;easy&lt;/em&gt; case: real fallers grab furniture, twist, brace, and half-catch
  themselves, and those compensatory movements smear the clean signature the
  cascade is looking for. My 49.3% is a ceiling, and real-world performance
  would be lower. Staged data is also the field&amp;rsquo;s normal practice, which is the
  problem with the field&amp;rsquo;s numbers: Chaudhuri, Thompson and Demiris reviewed
  more than a hundred fall-detection papers in 2014 and found most of them built
  on simulated falls, concluding that a system trained that way cannot reach the
  reliability of one built on real ones.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Their algorithms were mostly waist-mounted.&lt;/strong&gt; As my own three-mount
  comparison shows, that&amp;rsquo;s the easier position by about nineteen points.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;so-how-do-the-commercial-ones-manage&#34;&gt;So how do the commercial ones manage?&lt;/h3&gt;
&lt;p&gt;They engineer around the problem rather than solving it.&lt;/p&gt;
&lt;p&gt;Apple biases hard toward severe impacts, waits about a minute for immobility,
and &lt;strong&gt;requires you to confirm&lt;/strong&gt; before it calls anyone. It still famously
triggers on roller coasters and ski slopes. Medical
alert pendants get better numbers largely by sitting on the torso, the mount my
own data says is worth nineteen points.&lt;/p&gt;
&lt;p&gt;That confirmation prompt is doing enormous work. It converts an unsolved
detection problem into a &amp;ldquo;prompt the human&amp;rdquo; problem, which is tractable. When a
vendor advertises 95% accuracy, ask two questions: measured against staged falls
or real ones, and what was the false alarm rate over the same period? The second
number is almost never printed beside the first, and the pair is what tells you
whether the thing works.&lt;/p&gt;
&lt;p class=&#34;callout&#34;&gt;&lt;span class=&#34;note&#34;&gt;Note: &lt;/span&gt;None of this is medical advice, and it isn&amp;rsquo;t an argument against buying a fall-detecting device. A detector that catches half of falls is better than nothing, and the confirmation prompt makes the false alarms survivable. It is an argument against believing the marketing number, and against relying on automatic detection as the only safety net for someone who lives alone.&lt;/p&gt;
&lt;h2 id=&#34;what-survived&#34;&gt;What survived&lt;/h2&gt;
&lt;p&gt;The detector is dead. I set a compile-time flag that lets the cascade keep
running, keep recording, and keep filling the corpus, while making it
structurally incapable of summoning anyone.&lt;/p&gt;
&lt;p&gt;What&amp;rsquo;s left turns out to be most of the value.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The escalating check-in.&lt;/strong&gt; A far simpler idea, and it needs no training data
at all: if the watch has seen no movement for a long time on a wrist it knows is
being worn, ask. Buzz, put a prompt on screen, wait a minute, ask harder, and
only escalate to family after several increasingly insistent prompts all go
unanswered. It catches the slow collapses the cascade is structurally blind to —
sliding out of a chair, not getting up in the morning — and it doesn&amp;rsquo;t need to
recognise a fall to work.&lt;/p&gt;
&lt;p&gt;It also failed on its first real day: thirteen pages in fifteen hours, three of
them for the crime of sitting at a desk for two hours. The difference is that
the cascade failed on physics, which no amount of design fixes, and the check-in
failed on an unfinished design, which is an ordinary engineering problem.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Knowing whether it&amp;rsquo;s on a wrist.&lt;/strong&gt; This one I did solve, and cleanly. Measured
over minutes, a worn wrist and a tabletop separate by a factor of about 350 in
angular movement — 80.96° against 0.21°. I was worried a sleeping arm would look
like a nightstand; the quietest five-minute window in an entire night of sleep
still read 1.67°, eight times the tabletop. That&amp;rsquo;s a real discriminator with
margin on both sides, and it kills the desk problem I opened this article with.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The instrument.&lt;/strong&gt; Detection logic in plain C, a replay harness that runs
recorded motion through the exact production code on a laptop, CI that gates
every push, and a dataset importer with a units-verification step you can&amp;rsquo;t
skip. Building that took longer than building the detector, and it&amp;rsquo;s the only
reason any of the numbers above exist.&lt;/p&gt;
&lt;p&gt;That last part is now on GitHub as
&lt;strong&gt;&lt;a href=&#34;https://github.com/tiarno/detector-replay&#34;&gt;detector-replay&lt;/a&gt;&lt;/strong&gt;, generalised so
the detector is pluggable — you point it at your own C code, tell it which of
your verdicts count as alarms, and it scores recall and false alarms separately.
It ships with the cascade above as the worked example, defects and all, the 23
labelled traces including the table set-down that started this, and the FallAllD
converter with its units gate. Everything in this article is reproducible from
it.&lt;/p&gt;
&lt;p&gt;I also wrote down a stopping condition &lt;strong&gt;before&lt;/strong&gt; collecting the data: if the
redesigned check-in produces more than about one false page per week over a
seven-day trial, the project stops. Deciding that in advance matters, because I
know exactly how persuasive I&amp;rsquo;d find my own excuses afterwards.&lt;/p&gt;
&lt;h2 id=&#34;what-id-tell-you-if-youre-starting-this&#34;&gt;What I&amp;rsquo;d tell you if you&amp;rsquo;re starting this&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Measure both axes, or you&amp;rsquo;ve measured nothing.&lt;/strong&gt; Recall alone and false-alarm
rate alone each have a story where things are basically fine. Together they
closed the question in an afternoon. I spent weeks tuning against a number I
hadn&amp;rsquo;t paired with anything.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Staged data flatters you.&lt;/strong&gt; Every fall in my dataset was performed on purpose
by someone who knew it was coming. Real falls are messier and partially caught,
and the published research is unambiguous that performance drops when you move
from one to the other. Treat any result from staged falls as an upper bound.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Check what your sensor can physically know.&lt;/strong&gt; My orientation stage rested on
an assumption about wrists that ten minutes of thought would have refuted. It
survived for months because I&amp;rsquo;d written it down confidently, in a design
document, where it looked like a decision instead of a guess. The three-mount
comparison that finally settled it was an afternoon&amp;rsquo;s work on data I already had.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Read your own instruments.&lt;/strong&gt; My firmware printed a hardware capability
bitmask on every single boot for nineteen days. I decoded two bits of it. When I
finally read the rest, I discovered the board had GPS and an NFC reader on it —
both of which my design document explicitly claimed were absent. A number nobody
reads isn&amp;rsquo;t observability; it just looks like it.&lt;/p&gt;
&lt;p&gt;The working rule I&amp;rsquo;ve ended up with, and the one thing here I&amp;rsquo;d defend without
qualification: &lt;strong&gt;build the instrument, wear the thing, read the data.&lt;/strong&gt; Every
failure in this project was a plausible premise that nobody had checked, and
every one of them was cheap to check the moment I stopped arguing about it and
went to measure.&lt;/p&gt;
&lt;h2 id=&#34;the-code&#34;&gt;The code&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href=&#34;https://github.com/tiarno/detector-replay&#34;&gt;github.com/tiarno/detector-replay&lt;/a&gt;&lt;/strong&gt; — the measurement
bench, the fall cascade as its worked example, the trace corpus, and the dataset converter. MIT.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re building a detector of your own — falls, seizures, gestures, machine
faults, anything that watches a motion stream and occasionally decides something
happened — it&amp;rsquo;s built to be pointed at your code rather than mine. The README
covers what to change. FallAllD isn&amp;rsquo;t redistributed there; you download it
yourself and convert it.&lt;/p&gt;
&lt;h2 id=&#34;sources&#34;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Bagalà, F. et al., &lt;a href=&#34;https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0037062&#34;&gt;Evaluation of Accelerometer-Based Fall Detection Algorithms on Real-World Falls&lt;/a&gt;, &lt;em&gt;PLOS ONE&lt;/em&gt;, 2012 — the thirteen-algorithm study, and the source of the simulated-versus-real gap.&lt;/li&gt;
&lt;li&gt;Chaudhuri, S., Thompson, H. and Demiris, G., &lt;a href=&#34;https://pubmed.ncbi.nlm.nih.gov/24406708/&#34;&gt;Fall detection devices and their use with older adults: a systematic review&lt;/a&gt;, &lt;em&gt;Journal of Geriatric Physical Therapy&lt;/em&gt;, 2014 — over a hundred papers, most of them measured on simulated falls.&lt;/li&gt;
&lt;li&gt;Fleming, J. and Brayne, C., &lt;a href=&#34;https://pmc.ncbi.nlm.nih.gov/articles/PMC2590903/&#34;&gt;Inability to get up after falling, subsequent time on floor, and summoning help&lt;/a&gt;, &lt;em&gt;BMJ&lt;/em&gt;, 2008 — 110 people over ninety, followed for a year; 80% of those who fell and had a call button didn&amp;rsquo;t use it.&lt;/li&gt;
&lt;li&gt;Kangas, M. et al., &lt;a href=&#34;https://pubmed.ncbi.nlm.nih.gov/18294851/&#34;&gt;Comparison of low-complexity fall detection algorithms for body attached accelerometers&lt;/a&gt;, &lt;em&gt;Gait &amp;amp; Posture&lt;/em&gt;, 2008 — waist, wrist and head compared directly, and the wrist ruled out.&lt;/li&gt;
&lt;li&gt;Saleh, M. and Le Bouquin Jeannes, R., &lt;a href=&#34;https://ieee-dataport.org/open-access/fallalld-comprehensive-dataset-human-falls-and-activities-daily-living&#34;&gt;FallAllD&lt;/a&gt;, IEEE DataPort, 2020 — 15 participants, simultaneous wrist, waist and neck loggers. Described in &lt;a href=&#34;https://ieeexplore.ieee.org/document/9171857/&#34;&gt;IEEE Sensors Journal&lt;/a&gt;, 2021.&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
  <entry>
    <title>LoRecon: LoRa Reconnaissance on a $20 Board</title>
    <link href="https://reachtim.com/articles/lorecon.html" rel="alternate" />
    <id>https://reachtim.com/articles/lorecon.html</id>
    <published>2026-08-15T00:00:00Z</published>
    <updated>2026-08-15T00:00:00Z</updated>
    <category term="Security" />
    <summary type="text">A teaching walkthrough of a passive LoRa sniffer for cheap ESP32 hardware — the scanning problem it solves, exactly what it can and can&#39;t decrypt, and how the pieces fit together.</summary>
    <content type="html">&lt;p&gt;My hacking partner and I spent an afternoon in a hotel listening to the LoRa
traffic drifting through the building. Mesh nodes relaying each other&amp;rsquo;s
messages. Someone&amp;rsquo;s sensor phoning home. By the time we packed up, we had 3,630
packets from 266 devices, and 57 messages we could read in plaintext — every
one of them opened with a known or default channel key, nothing custom.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s what this article is about: what a key like that actually opens, and
how LoRecon does the listening.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://github.com/haksht/lorecon&#34;&gt;LoRecon&lt;/a&gt; is the listener. It&amp;rsquo;s firmware for
a twenty-dollar ESP32 board that turns it into a self-contained LoRa
reconnaissance tool, driven from your phone over its own web interface. We built
it — I wrote the firmware, my buddy The-Foe built out the hardware and did the
field testing — and I&amp;rsquo;ll teach it the way I&amp;rsquo;d want it explained to me, starting
from the one fact that makes sniffing LoRa harder than sniffing WiFi.&lt;/p&gt;
&lt;h2 id=&#34;why-you-cant-just-listen&#34;&gt;Why you can&amp;rsquo;t just listen&lt;/h2&gt;
&lt;p&gt;On WiFi, one radio hears every other radio on the same channel. LoRa doesn&amp;rsquo;t
work that way. &amp;ldquo;LoRa&amp;rdquo; is just the modulation; the protocol on top is left to each
network, and every one picks its own &lt;strong&gt;frequency&lt;/strong&gt;, &lt;strong&gt;spreading factor&lt;/strong&gt;, &lt;strong&gt;bandwidth&lt;/strong&gt;,
and &lt;strong&gt;coding rate&lt;/strong&gt;, and a receiver has to match all four before it can
demodulate anything at all. Get one of the four wrong and you hear nothing at
all — just silence, and you never even see it.&lt;/p&gt;
&lt;p&gt;So a passive sniffer can&amp;rsquo;t just &amp;ldquo;listen.&amp;rdquo; It has to &lt;em&gt;guess&lt;/em&gt;, methodically,
across every combination anyone actually uses. LoRecon carries a table of 29
such combinations, covering five network families, and cycles through them one
at a time, dwelling about twelve seconds on each before moving on. A full lap is
roughly six minutes. Whatever&amp;rsquo;s in range and transmitting during its slice gets
captured, classified, and — where possible — decrypted, before the radio
retunes and moves to the next slot.&lt;/p&gt;
&lt;figure class=&#34;diagram&#34; markdown=&#34;0&#34;&gt;
&lt;svg viewBox=&#34;0 0 880 170&#34; role=&#34;img&#34; aria-labelledby=&#34;scan-title scan-desc&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;title id=&#34;scan-title&#34;&gt;The 29-configuration scan cycle&lt;/title&gt;
&lt;desc id=&#34;scan-desc&#34;&gt;A row of 29 small blocks grouped into five clusters — Meshtastic (11), LoRaWAN/TTN (7), Helium (6), MeshCore (3), and ISM (2) — with one block highlighted to show the radio&#39;s current position in the sweep.&lt;/desc&gt;
&lt;style&gt;text{font-family:var(--font-mono);}&lt;/style&gt;
&lt;text x=&#34;30&#34; y=&#34;24&#34; font-size=&#34;11&#34; letter-spacing=&#34;0.14em&#34; fill=&#34;var(--muted)&#34;&gt;SCAN CYCLE&lt;/text&gt;

&lt;!-- 29 bars: widths 22, gap 2 within a cluster, gap 14 between clusters --&gt;
&lt;g&gt;
&lt;rect x=&#34;60&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--teal)&#34;/&gt;
&lt;rect x=&#34;84&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;108&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;132&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;156&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;180&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;204&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;228&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;252&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;276&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;300&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;

&lt;rect x=&#34;336&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;360&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;384&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;408&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;432&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;456&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;480&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;

&lt;rect x=&#34;516&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;540&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;564&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;588&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;612&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;636&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;

&lt;rect x=&#34;672&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;696&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;720&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;

&lt;rect x=&#34;756&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;rect x=&#34;780&#34; y=&#34;48&#34; width=&#34;22&#34; height=&#34;26&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;/g&gt;

&lt;line x1=&#34;58&#34; y1=&#34;90&#34; x2=&#34;822&#34; y2=&#34;90&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;polygon points=&#34;822,85 830,90 822,95&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;text x=&#34;60&#34; y=&#34;108&#34; font-size=&#34;12&#34; fill=&#34;var(--muted)&#34;&gt;0:00&lt;/text&gt;
&lt;text x=&#34;822&#34; y=&#34;108&#34; font-size=&#34;12&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;end&#34;&gt;~6:00&lt;/text&gt;

&lt;text x=&#34;191&#34; y=&#34;128&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;Meshtastic&lt;/text&gt;
&lt;text x=&#34;191&#34; y=&#34;144&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;11 configs&lt;/text&gt;

&lt;text x=&#34;419&#34; y=&#34;128&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;LoRaWAN / TTN&lt;/text&gt;
&lt;text x=&#34;419&#34; y=&#34;144&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;7 configs&lt;/text&gt;

&lt;text x=&#34;587&#34; y=&#34;128&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;Helium&lt;/text&gt;
&lt;text x=&#34;587&#34; y=&#34;144&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;6 configs&lt;/text&gt;

&lt;text x=&#34;707&#34; y=&#34;128&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;MeshCore&lt;/text&gt;
&lt;text x=&#34;707&#34; y=&#34;144&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;3 configs&lt;/text&gt;

&lt;text x=&#34;779&#34; y=&#34;128&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;ISM&lt;/text&gt;
&lt;text x=&#34;779&#34; y=&#34;144&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;2 configs&lt;/text&gt;
&lt;/svg&gt;
&lt;figcaption&gt;Twenty-nine frequency/spreading-factor/bandwidth/coding-rate combinations, grouped by the network family that uses them. The highlighted block is wherever the sweep currently sits — roughly twelve seconds per config, about six minutes for a full loop.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;That&amp;rsquo;s also why the scan takes minutes instead of being instant: there&amp;rsquo;s no
shortcut around trying each combination in turn. It&amp;rsquo;s the same reason a
Meshtastic node in your backpack and a LoRaWAN sensor on someone&amp;rsquo;s roof can
share the same air without ever hearing each other — they were never listening
on the same &amp;ldquo;channel&amp;rdquo; in the first place. Back in that hotel, the six-minute lap
is exactly what turned a quiet band into 266 devices: give the sweep enough laps
and everything transmitting eventually lands in a slice you&amp;rsquo;re tuned to.&lt;/p&gt;
&lt;h2 id=&#34;what-it-does-once-it-hears-something&#34;&gt;What it does once it hears something&lt;/h2&gt;
&lt;p&gt;Once a packet is captured, LoRecon classifies it by protocol and tries to open
it. Three things can happen, depending on what it heard.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Default-key decryption.&lt;/strong&gt; It carries 23 known and historically common
  Meshtastic channel PSKs and tries each against what it captured. For MeshCore,
  the public channel key is fixed and shipped on every device, so that one&amp;rsquo;s
  automatic; a handful of common hashtag-room keys (&lt;code&gt;#general&lt;/code&gt;, &lt;code&gt;#emergency&lt;/code&gt;,
  and similar) are derived from the room name and tried too. For LoRaWAN, it
  checks 16 default AppKeys against any Join Request it catches.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Position extraction.&lt;/strong&gt; A successful decrypt of a Meshtastic &lt;code&gt;POSITION_APP&lt;/code&gt;
  packet yields GPS coordinates, exportable straight to KML or GeoJSON.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replay.&lt;/strong&gt; Ten storage slots, with a configurable repeat count and delay, for
  testing how your own gear reacts to a packet it&amp;rsquo;s seen before.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Everything captured is reachable from a six-tab web UI the board serves itself —
over its own access point if there&amp;rsquo;s no WiFi around — and exportable as CSV or
Wireshark-ready PCAP. From there, a Python toolkit that ships in the repo picks
up the file: a &lt;code&gt;report&lt;/code&gt; command that turns a capture into a prioritized findings
page, a &lt;code&gt;map&lt;/code&gt; command that plots extracted GPS onto an interactive map, and a
&lt;code&gt;topology&lt;/code&gt; command that draws the mesh as a graph, labeling nodes with whatever
operator name it could decrypt.&lt;/p&gt;
&lt;h2 id=&#34;the-boundary-what-a-default-key-doesnt-get-you&#34;&gt;The boundary: what a default key doesn&amp;rsquo;t get you&lt;/h2&gt;
&lt;p&gt;This is the part I&amp;rsquo;d want to know first if I were reading instead of writing:
&lt;strong&gt;a cracked default key does not hand you someone&amp;rsquo;s private messages.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Meshtastic firmware from 2.5.0 onward split its encryption into two systems. A
sweep like this treats them very differently.&lt;/p&gt;
&lt;p&gt;Everything a node broadcasts automatically — its position, its telemetry, its
node info, its traceroutes, routing packets, and anything typed into a &lt;em&gt;channel&lt;/em&gt;
(as opposed to a direct message) — is wrapped in one shared AES key per channel.
If that key matches one of the 23 known and historically common values LoRecon
carries, the database opens it in one pass.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s what happened to all 57 readable messages in the hotel capture: every
one decrypted with a known key, none with a custom PSK. Two distinct channels
were in use — a main one with 261 devices, and a separately named one with 17 —
and both decrypted with the identical unmodified default key, which is what
actually determined the outcome.&lt;/p&gt;
&lt;p&gt;Position broadcasts aren&amp;rsquo;t uniformly precise, either. Firmware supports a
range of reduced-precision settings, each defining a grid cell centered on the
reported point rather than blurring the true location — a limit set locally by
the app, not the radio link. Of the nodes broadcasting position in this same
capture, 91% used some reduced-precision setting; the remaining 9% broadcast
full, meter-level precision. Same channel, same key in both cases — just a
different setting.&lt;/p&gt;
&lt;aside class=&#34;callout&#34;&gt;
&lt;p&gt;&lt;strong&gt;Sidebar: how much does a reduced setting actually hide?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Reduced precision&amp;rdquo; isn&amp;rsquo;t one setting — the capture had nodes spread across
nine distinct levels, each cutting the position down to a different grid size:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;precision_bits&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Nodes&lt;/th&gt;
&lt;th&gt;Share&lt;/th&gt;
&lt;th&gt;Error radius&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0.6%&lt;/td&gt;
&lt;td&gt;±23.3 km&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;3.3%&lt;/td&gt;
&lt;td&gt;±5.8 km&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;13 (the common default)&lt;/td&gt;
&lt;td&gt;74&lt;/td&gt;
&lt;td&gt;41.1%&lt;/td&gt;
&lt;td&gt;±2.9 km&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;41&lt;/td&gt;
&lt;td&gt;22.8%&lt;/td&gt;
&lt;td&gt;±1.5 km&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;td&gt;17.2%&lt;/td&gt;
&lt;td&gt;±730 m&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;2.8%&lt;/td&gt;
&lt;td&gt;±365 m&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1.1%&lt;/td&gt;
&lt;td&gt;±182 m&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0.6%&lt;/td&gt;
&lt;td&gt;±91 m&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1.7%&lt;/td&gt;
&lt;td&gt;±46 m&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;32 (full)&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;8.9%&lt;/td&gt;
&lt;td&gt;meter-level&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The two coarsest settings — 3.9% of nodes — narrow a position to roughly
&amp;ldquo;somewhere in the metro area.&amp;rdquo; Everything from row five down, full precision
included, is about a third of all nodes and narrows to under a kilometer:
enough to say which venue an operator is transmitting from, not just which
city. I didn&amp;rsquo;t have this breakdown in front of me while I was writing the
position-extraction code — I&amp;rsquo;d have logged the precision level alongside the
coordinates from the start if I had.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;None of that says anything about why any particular node in the capture is set
up the way it is. For a lot of Meshtastic&amp;rsquo;s user base, the public channel is
exactly what they want: a way to reach and be reached by other operators
nearby, on a network anyone can join without arranging a key first.&lt;/p&gt;
&lt;p&gt;Direct messages and admin commands are a different story. Since firmware 2.5.0
they get a second, independent layer — Curve25519 public-key encryption, keyed
to the &lt;em&gt;recipient&amp;rsquo;s&lt;/em&gt; device, whose private key never leaves that device. No
database of default values gets you through that layer, because there is no
shared secret to guess. This is the correct primitive, used correctly.&lt;/p&gt;
&lt;figure class=&#34;diagram&#34; markdown=&#34;0&#34;&gt;
&lt;svg viewBox=&#34;0 0 880 320&#34; role=&#34;img&#34; aria-labelledby=&#34;enc-title enc-desc&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;title id=&#34;enc-title&#34;&gt;What a default channel key does and doesn&#39;t open&lt;/title&gt;
&lt;desc id=&#34;enc-desc&#34;&gt;A LoRa packet passes through a shared channel-PSK layer. Automatic broadcasts such as position, telemetry, and node info become plaintext once that layer opens. Direct messages and admin commands carry a second Curve25519 layer that a channel key never opens.&lt;/desc&gt;
&lt;style&gt;text{font-family:var(--font-mono);}&lt;/style&gt;
&lt;text x=&#34;30&#34; y=&#34;24&#34; font-size=&#34;11&#34; letter-spacing=&#34;0.14em&#34; fill=&#34;var(--muted)&#34;&gt;ENCRYPTION BOUNDARY&lt;/text&gt;

&lt;text x=&#34;440&#34; y=&#34;54&#34; font-size=&#34;13&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;LoRa packet, captured over the air&lt;/text&gt;
&lt;line x1=&#34;440&#34; y1=&#34;62&#34; x2=&#34;440&#34; y2=&#34;86&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;polygon points=&#34;435,86 440,94 445,86&#34; fill=&#34;var(--line-strong)&#34;/&gt;

&lt;rect x=&#34;270&#34; y=&#34;90&#34; width=&#34;340&#34; height=&#34;54&#34; rx=&#34;4&#34; fill=&#34;var(--surface)&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;440&#34; y=&#34;112&#34; font-size=&#34;13&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;CHANNEL PSK · AES-256-CTR&lt;/text&gt;
&lt;text x=&#34;440&#34; y=&#34;130&#34; font-size=&#34;11.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;shared key — often still the factory default&lt;/text&gt;

&lt;line x1=&#34;440&#34; y1=&#34;144&#34; x2=&#34;440&#34; y2=&#34;162&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;line x1=&#34;230&#34; y1=&#34;162&#34; x2=&#34;650&#34; y2=&#34;162&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;line x1=&#34;230&#34; y1=&#34;162&#34; x2=&#34;230&#34; y2=&#34;178&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;line x1=&#34;650&#34; y1=&#34;162&#34; x2=&#34;650&#34; y2=&#34;178&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;polygon points=&#34;225,178 230,186 235,178&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;polygon points=&#34;645,178 650,186 655,178&#34; fill=&#34;var(--line-strong)&#34;/&gt;

&lt;rect x=&#34;70&#34; y=&#34;180&#34; width=&#34;320&#34; height=&#34;120&#34; rx=&#34;4&#34; fill=&#34;var(--surface)&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;230&#34; y=&#34;202&#34; font-size=&#34;11&#34; letter-spacing=&#34;0.1em&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;AUTOMATIC BROADCASTS&lt;/text&gt;
&lt;text x=&#34;230&#34; y=&#34;220&#34; font-size=&#34;12&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;position · telemetry · nodeinfo&lt;/text&gt;
&lt;text x=&#34;230&#34; y=&#34;236&#34; font-size=&#34;12&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;routing · channel messages&lt;/text&gt;
&lt;g transform=&#34;translate(218,244)&#34; fill=&#34;none&#34; stroke=&#34;var(--brick)&#34; stroke-width=&#34;2&#34;&gt;
  &lt;rect x=&#34;2&#34; y=&#34;12&#34; width=&#34;20&#34; height=&#34;14&#34; rx=&#34;2&#34;/&gt;
  &lt;path d=&#34;M6 12 V9 a6 6 0 0 1 11 -3&#34;/&gt;
&lt;/g&gt;
&lt;text x=&#34;230&#34; y=&#34;292&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--brick)&#34; text-anchor=&#34;middle&#34;&gt;plaintext once the key hits&lt;/text&gt;

&lt;rect x=&#34;490&#34; y=&#34;180&#34; width=&#34;320&#34; height=&#34;120&#34; rx=&#34;4&#34; fill=&#34;var(--surface)&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;650&#34; y=&#34;202&#34; font-size=&#34;11&#34; letter-spacing=&#34;0.1em&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;DIRECT MESSAGES &amp;amp; ADMIN&lt;/text&gt;
&lt;text x=&#34;650&#34; y=&#34;220&#34; font-size=&#34;12&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;firmware 2.5.0 and newer&lt;/text&gt;
&lt;text x=&#34;650&#34; y=&#34;236&#34; font-size=&#34;12&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;Curve25519 key pair&lt;/text&gt;
&lt;g transform=&#34;translate(638,244)&#34; fill=&#34;none&#34; stroke=&#34;var(--teal)&#34; stroke-width=&#34;2&#34;&gt;
  &lt;rect x=&#34;2&#34; y=&#34;12&#34; width=&#34;20&#34; height=&#34;14&#34; rx=&#34;2&#34;/&gt;
  &lt;path d=&#34;M6 12 V8 a6 6 0 0 1 12 0 v4&#34;/&gt;
&lt;/g&gt;
&lt;text x=&#34;650&#34; y=&#34;292&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--teal)&#34; text-anchor=&#34;middle&#34;&gt;stays encrypted — no key for this&lt;/text&gt;
&lt;/svg&gt;
&lt;figcaption&gt;The channel PSK is one shared secret protecting everything a node sends automatically. A hit opens it all at once. Direct messages and admin commands sit behind a second, independent lock keyed to one device’s private key — a channel-PSK hit never reaches it.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If an operator sets a custom channel PSK, the 23-key database doesn&amp;rsquo;t match
anything, and the broadcasts stay encrypted blobs too. A sweep like this checks
the key, not the name — renaming a channel has no effect on it. Setting a
custom PSK is a thirty-second job in the app, and it&amp;rsquo;s the one change that
alters what a listener sees.&lt;/p&gt;
&lt;p&gt;Before firmware 2.5.0, direct messages were just channel messages with a
destination field, protected by the same shared key as everything else. Pre-2.5.0 devices don&amp;rsquo;t get the Curve25519
protection at all. So if you&amp;rsquo;re auditing an older deployment, keep in mind that
&amp;ldquo;we&amp;rsquo;re on the default key&amp;rdquo; and &amp;ldquo;our DMs are private&amp;rdquo; cannot both be true at
once.&lt;/p&gt;
&lt;h2 id=&#34;from-antenna-to-report&#34;&gt;From antenna to report&lt;/h2&gt;
&lt;p&gt;None of this needs a laptop in the field, and none of it needs a cloud service.
The whole pipeline fits on the board and the phone in your pocket.&lt;/p&gt;
&lt;figure class=&#34;diagram&#34; markdown=&#34;0&#34;&gt;
&lt;svg viewBox=&#34;0 0 880 200&#34; role=&#34;img&#34; aria-labelledby=&#34;path-title path-desc&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;title id=&#34;path-title&#34;&gt;LoRecon&#39;s end-to-end data path&lt;/title&gt;
&lt;desc id=&#34;path-desc&#34;&gt;LoRa RF flows one-way into the ESP32 board, which serves a web UI to a phone or laptop over WiFi. Captures export as CSV, PCAP, KML, or GeoJSON to Python tools that run entirely offline.&lt;/desc&gt;
&lt;style&gt;text{font-family:var(--font-mono);}&lt;/style&gt;
&lt;text x=&#34;440&#34; y=&#34;24&#34; font-size=&#34;11&#34; letter-spacing=&#34;0.14em&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;END-TO-END DATA PATH&lt;/text&gt;
&lt;text x=&#34;440&#34; y=&#34;44&#34; font-size=&#34;12.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;no cloud in this picture — a phone is enough, and every analysis step after export runs offline&lt;/text&gt;

&lt;rect x=&#34;16&#34; y=&#34;90&#34; width=&#34;130&#34; height=&#34;70&#34; rx=&#34;4&#34; fill=&#34;var(--surface)&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;81&#34; y=&#34;120&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;LoRa RF&lt;/text&gt;
&lt;text x=&#34;81&#34; y=&#34;138&#34; font-size=&#34;10.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;passive reception&lt;/text&gt;

&lt;line x1=&#34;146&#34; y1=&#34;125&#34; x2=&#34;228&#34; y2=&#34;125&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;polygon points=&#34;228,120 236,125 228,130&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;text x=&#34;191&#34; y=&#34;112&#34; font-size=&#34;10.5&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;receive only&lt;/text&gt;
&lt;text x=&#34;191&#34; y=&#34;178&#34; font-size=&#34;10&#34; fill=&#34;var(--brick)&#34; text-anchor=&#34;middle&#34;&gt;(no transmit path)&lt;/text&gt;

&lt;rect x=&#34;236&#34; y=&#34;90&#34; width=&#34;150&#34; height=&#34;70&#34; rx=&#34;4&#34; fill=&#34;var(--surface)&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;311&#34; y=&#34;120&#34; font-size=&#34;12&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;ESP32-S3 + SX1262&lt;/text&gt;
&lt;text x=&#34;311&#34; y=&#34;138&#34; font-size=&#34;10.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;scans &amp;amp; decrypts&lt;/text&gt;

&lt;line x1=&#34;386&#34; y1=&#34;125&#34; x2=&#34;438&#34; y2=&#34;125&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;polygon points=&#34;438,120 446,125 438,130&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;text x=&#34;416&#34; y=&#34;112&#34; font-size=&#34;10.5&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;WiFi&lt;/text&gt;

&lt;rect x=&#34;446&#34; y=&#34;90&#34; width=&#34;150&#34; height=&#34;70&#34; rx=&#34;4&#34; fill=&#34;var(--surface)&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;521&#34; y=&#34;120&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;Phone or laptop&lt;/text&gt;
&lt;text x=&#34;521&#34; y=&#34;138&#34; font-size=&#34;10.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;web UI, no install&lt;/text&gt;

&lt;line x1=&#34;596&#34; y1=&#34;125&#34; x2=&#34;648&#34; y2=&#34;125&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;polygon points=&#34;648,120 656,125 648,130&#34; fill=&#34;var(--line-strong)&#34;/&gt;
&lt;text x=&#34;626&#34; y=&#34;112&#34; font-size=&#34;10.5&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;export&lt;/text&gt;

&lt;rect x=&#34;656&#34; y=&#34;90&#34; width=&#34;214&#34; height=&#34;70&#34; rx=&#34;4&#34; fill=&#34;var(--surface)&#34; stroke=&#34;var(--line-strong)&#34; stroke-width=&#34;1.25&#34;/&gt;
&lt;text x=&#34;763&#34; y=&#34;120&#34; font-size=&#34;12.5&#34; font-weight=&#34;600&#34; fill=&#34;var(--ink)&#34; text-anchor=&#34;middle&#34;&gt;Python toolkit&lt;/text&gt;
&lt;text x=&#34;763&#34; y=&#34;138&#34; font-size=&#34;10.5&#34; fill=&#34;var(--muted)&#34; text-anchor=&#34;middle&#34;&gt;report · map · topology&lt;/text&gt;
&lt;/svg&gt;
&lt;figcaption&gt;CSV, PCAP, KML, and GeoJSON move the data from device to disk; `lorecon report`, `lorecon map`, and `lorecon topology` turn a saved capture into a findings page, a GPS map, and a mesh graph — all of it running on a machine that never has to touch the internet.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The RF leg is drawn one-way on purpose. LoRecon never transmits — replay writes
to storage slots you trigger manually, but nothing goes out over the air unless
you ask it to, and normal operation is receive-only from power-on. That&amp;rsquo;s a
deliberate design line.&lt;/p&gt;
&lt;h2 id=&#34;choosing-hardware&#34;&gt;Choosing hardware&lt;/h2&gt;
&lt;p&gt;Four boards run the same firmware and the same web UI; the differences are about
what you need in the field, not what the tool can do.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Board&lt;/th&gt;
&lt;th&gt;Why you&amp;rsquo;d pick it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Heltec WiFi LoRa 32 V3&lt;/td&gt;
&lt;td&gt;Cheapest, most proven. Start here.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Heltec WiFi LoRa 32 V4&lt;/td&gt;
&lt;td&gt;Optional L76K GPS, native USB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LilyGO T3-S3&lt;/td&gt;
&lt;td&gt;SD card slot — log PCAP/CSV to disk in the field&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LilyGO T-Beam Supreme&lt;/td&gt;
&lt;td&gt;SD, GPS, battery management, and 8 MB of PSRAM&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;That last column, PSRAM, earns its keep. A long unattended capture
dies of heap fragmentation on a bare ESP32 long before anything conceptually
interesting happens to it. The T-Beam Supreme with PSRAM enabled holds roughly
130 KB of minimum free heap after twenty hours with fifty devices in range,
against about 53 KB without. Most of the recent release cycle was stability
engineering of exactly that flavor — chunked API responses, heap guards, crash
context that survives a reboot. It&amp;rsquo;s unglamorous work, and it&amp;rsquo;s the difference
between a tool that runs on the bench and one that&amp;rsquo;s still running when you come
back to it.&lt;/p&gt;
&lt;h2 id=&#34;try-it-yourself&#34;&gt;Try it yourself&lt;/h2&gt;
&lt;p&gt;There&amp;rsquo;s a browser-based installer, so getting a board running doesn&amp;rsquo;t require
touching a toolchain:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://haksht.github.io/lorecon/install/&#34;&gt;https://haksht.github.io/lorecon/install/&lt;/a&gt; — plug the board in, open the page
in Chrome or Edge, click flash.&lt;/p&gt;
&lt;p&gt;If you want the Python analysis toolkit too — &lt;code&gt;report&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;topology&lt;/code&gt;, and
the rest — that lives in the source repo, not the binary release, so you&amp;rsquo;ll want
to clone it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;git clone https://github.com/haksht/lorecon.git
cd lorecon
python -m venv venv
venv\Scripts\activate.ps1        # or ./venv/bin/activate on macOS/Linux
pip install -e .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;lorecon --help&lt;/code&gt; confirms the install worked. From there, &lt;code&gt;lorecon report
capture.csv&lt;/code&gt; is the first thing worth running against anything you capture.&lt;/p&gt;
&lt;p&gt;Building the firmware yourself is PlatformIO, with OTA updates available after
the first USB flash. MIT licensed. The current release is 2.6.0.&lt;/p&gt;
&lt;p class=&#34;callout&#34;&gt;&lt;span class=&#34;note&#34;&gt;Note: &lt;/span&gt;This is for networks you own or have written permission to test. Passive reception is generally legal in the US; interception of content can implicate 18 U.S.C. § 2511, and the rules vary considerably by jurisdiction. The default-key testing exists to demonstrate a risk, not to hand anyone a way into someone else&amp;rsquo;s traffic.&lt;/p&gt;
&lt;h2 id=&#34;learn-more&#34;&gt;Learn more&lt;/h2&gt;
&lt;p&gt;The repo&amp;rsquo;s &lt;code&gt;docs/&lt;/code&gt; folder is the fuller version of everything above:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/haksht/lorecon/blob/main/docs/SETUP.md&#34;&gt;SETUP.md&lt;/a&gt; — flashing, first boot, WiFi provisioning&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/haksht/lorecon/blob/main/docs/USAGE.md&#34;&gt;USAGE.md&lt;/a&gt; — the web UI and field deployment&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/haksht/lorecon/blob/main/docs/TOOLS.md&#34;&gt;TOOLS.md&lt;/a&gt; — the full Python toolkit reference&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/haksht/lorecon/blob/main/docs/reference/ENCRYPTION.md&#34;&gt;reference/ENCRYPTION.md&lt;/a&gt; — exactly what&amp;rsquo;s decryptable and why, in more depth than the section above&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/haksht/lorecon/blob/main/docs/reference/NETWORK_HUNTING.md&#34;&gt;reference/NETWORK_HUNTING.md&lt;/a&gt; — the full 29-config table, and where each network family tends to show up&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/haksht/lorecon/blob/main/docs/developers/ARCHITECTURE.md&#34;&gt;developers/ARCHITECTURE.md&lt;/a&gt; — how the firmware itself is put together, for anyone extending it&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;credit-where-its-due&#34;&gt;Credit where it&amp;rsquo;s due&lt;/h2&gt;
&lt;p&gt;I wrote the firmware and the tooling. The hardware side — board selection, the
antenna and power work, and the field testing that turned up most of the bugs
worth fixing — is &lt;a href=&#34;https://github.com/The-Foe&#34;&gt;The-Foe&lt;/a&gt;, who I share the
&lt;a href=&#34;https://github.com/haksht&#34;&gt;haksht&lt;/a&gt; org with. A sniffer that works on the bench
and falls over after six hours in a bag is not a sniffer, and finding that out
is its own skill.&lt;/p&gt;
&lt;p&gt;The repo is at &lt;strong&gt;&lt;a href=&#34;https://github.com/haksht/lorecon&#34;&gt;github.com/haksht/lorecon&lt;/a&gt;&lt;/strong&gt;.
If it&amp;rsquo;s useful to you, a star helps other people find it.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>The contextmanager Decorator</title>
    <link href="https://reachtim.com/articles/contextmanager-decorator.html" rel="alternate" />
    <id>https://reachtim.com/articles/contextmanager-decorator.html</id>
    <published>2020-01-22T00:00:00Z</published>
    <updated>2020-01-22T00:00:00Z</updated>
    <category term="Python" />
    <summary type="text">Quick overview on usefulness of the contextmanager decorator and creating context managers from generators.</summary>
    <content type="html">&lt;h2 id=&#34;overview&#34;&gt;Overview&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Generally you create a context manager by creating a class with &lt;code&gt;__enter__&lt;/code&gt; and &lt;code&gt;__exit__&lt;/code&gt; methods, but this example shows you how to use the &lt;code&gt;@contextlib.contextmanager&lt;/code&gt; to create a simple context manager.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Context managers provide a cool programming pattern, especially if you&amp;rsquo;re forgetful or
just have too much to keep track of and you want to simplify your life.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll find them helpful when you have opened something and need to close it, locked something and need to release
it, or changed something and need to reset it.  There are several built in context managers that you&amp;rsquo;re probably
familiar with like &lt;code&gt;open&lt;/code&gt; to open a file or &lt;code&gt;socket&lt;/code&gt; to use a socket.  The bog-standard example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;myfile.txt&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;lines&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;readlines&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;do_stuff&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;lines&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;open&lt;/code&gt; context manager opens a file, returns an object we name as &lt;code&gt;f&lt;/code&gt;. When we&amp;rsquo;ve done all the
things we&amp;rsquo;re going to with it,
(we fall out of the &lt;code&gt;with&lt;/code&gt; statement block), the file is automatically closed for us.&lt;/p&gt;
&lt;p&gt;In this brief article, you&amp;rsquo;ll see how you can create a dead-simple context manager from a generator.&lt;/p&gt;
&lt;h2 id=&#34;lets-code&#34;&gt;Let&amp;rsquo;s Code&lt;/h2&gt;
&lt;h3 id=&#34;create-the-context-manager&#34;&gt;Create the Context Manager&lt;/h3&gt;
&lt;p&gt;First import the &lt;code&gt;contextlib&lt;/code&gt; module. It has several helpers
(read more here: &lt;a href=&#34;https://docs.python.org/3/library/contextlib.html&#34;&gt;contextlib module&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;re just going to decorate the &lt;code&gt;chdir&lt;/code&gt; function as a &lt;code&gt;contextlib.contextmanager&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;contextlib&lt;/span&gt;

&lt;span class=&#34;nd&#34;&gt;@contextlib&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contextmanager&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;chdir&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;path&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;sd&#34;&gt;&#34;&#34;&#34;&lt;/span&gt;
&lt;span class=&#34;sd&#34;&gt;    On enter, change directory to specified path.&lt;/span&gt;
&lt;span class=&#34;sd&#34;&gt;    On exit, change directory to original.&lt;/span&gt;
&lt;span class=&#34;sd&#34;&gt;    &#34;&#34;&#34;&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;this_dir&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;os&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getcwd&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;os&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;chdir&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;path&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;yield&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;finally&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;os&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;chdir&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;this_dir&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To make this work, the function must be a generator and &lt;code&gt;yield&lt;/code&gt; exactly once.
At the point of the &lt;code&gt;yield&lt;/code&gt;, the calling process is executed.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;chdir&lt;/code&gt;, the function takes a single argument, &lt;code&gt;path&lt;/code&gt;.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First it makes a note of the current directory and then changes to the &lt;code&gt;path&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then it yields control back to the caller.&lt;/li&gt;
&lt;li&gt;No matter what happens during that process, the function will &lt;code&gt;finally&lt;/code&gt; change back to the original directory.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;use-the-context-manager&#34;&gt;Use the Context Manager&lt;/h3&gt;
&lt;p&gt;Suppose you have some function &lt;code&gt;gather_paths&lt;/code&gt; you want to call for a set of directories.
The following example shows how the &lt;code&gt;chdir&lt;/code&gt; context manager could be used:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;chdir&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;/mydownloads/wordpress&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;gather_paths&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I like this little context manager; it keeps me from having to remember to switch back
to the original directory so I don&amp;rsquo;t get surprised later and find my program is executing
somewhere else.&lt;/p&gt;
&lt;p&gt;As long as I call the function as a context manager using the &lt;code&gt;with&lt;/code&gt; statement,
I don&amp;rsquo;t have to remember to change back to the original directory or do anything special.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Reading Binary Data with Python</title>
    <link href="https://reachtim.com/articles/reading-binary-data-python.html" rel="alternate" />
    <id>https://reachtim.com/articles/reading-binary-data-python.html</id>
    <published>2020-01-20T00:00:00Z</published>
    <updated>2020-01-20T00:00:00Z</updated>
    <category term="Python" />
    <summary type="text">Two ways to read binary data into a Python data structure.</summary>
    <content type="html">&lt;h2 id=&#34;overview&#34;&gt;Overview&lt;/h2&gt;
&lt;p&gt;When you deal with external binary data in Python, there are a couple of ways to get that
data into a data structure. You can use the &lt;code&gt;ctypes&lt;/code&gt; module to define the data structure
or you can use the &lt;code&gt;struct&lt;/code&gt; python module.&lt;/p&gt;
&lt;p&gt;You will see both methods used when you explore tool repositories
on the web. This article shows you how to use each one to read an IPv4 header off the network.
It&amp;rsquo;s up to you to decide which method you prefer; either way will work fine.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ctypes&lt;/code&gt; is a foreign function library for Python. It deals with C-based languages to provide
            C-compatible data types, and enables you to call functions in shared libraries.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;struct&lt;/code&gt; converts between Python values and C structs that are represented as Python bytes objects.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So &lt;code&gt;ctypes&lt;/code&gt; handles binary data types in addition to a lot of other functionality, while
handling binary data is the main purpose of the &lt;code&gt;struct&lt;/code&gt; module.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see how these two libraries are used when we need to decode an IPv4 header off the
network.&lt;/p&gt;
&lt;p&gt;First, here&amp;rsquo;s the structure of the IPv4 header. This is from the IETF RFC 791:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;A&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;summary&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;the&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;contents&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;the&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;internet&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;header&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;follows:


&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;                   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;                   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;                   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;3&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;Version&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;IHL&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;Type&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Service&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;          &lt;/span&gt;Total&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Length&lt;span class=&#34;w&#34;&gt;         &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;         &lt;/span&gt;Identification&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;Flags&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;Fragment&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Offset&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;Time&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;to&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Live&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;Protocol&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;         &lt;/span&gt;Header&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Checksum&lt;span class=&#34;w&#34;&gt;       &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;                       &lt;/span&gt;Source&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Address&lt;span class=&#34;w&#34;&gt;                          &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;                    &lt;/span&gt;Destination&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Address&lt;span class=&#34;w&#34;&gt;                        &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;                    &lt;/span&gt;Options&lt;span class=&#34;w&#34;&gt;                    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;Padding&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

&lt;span class=&#34;w&#34;&gt;                    &lt;/span&gt;Example&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Internet&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Datagram&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Header
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&#34;initial-data-from-the-network&#34;&gt;Initial Data from the Network&lt;/h3&gt;
&lt;p&gt;We need some data to work with, so let&amp;rsquo;s get a single packet from the network.
This little snippet show do fine. I ran this on Linux.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;socket&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;sys&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;sniff&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;host&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;sniffer&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;AF_INET&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SOCK_RAW&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;IPPROTO_ICMP&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;sniffer&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bind&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;((&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;host&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;sniffer&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setsockopt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;IPPROTO_IP&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;IP_HDRINCL&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;# read and return a single packet&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sniffer&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;recvfrom&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;65535&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;vm&#34;&gt;__name__&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;__main__&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;len&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sys&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;argv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;host&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sys&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;argv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;host&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;192.168.1.69&#39;&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;buff&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sniff&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;host&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We just grab a single raw packet from the network and put it into a variable, &lt;code&gt;buff&lt;/code&gt;.
So now that we have binary data, let&amp;rsquo;s look at how to use it.&lt;/p&gt;
&lt;h2 id=&#34;ctypes-module&#34;&gt;&lt;code&gt;ctypes&lt;/code&gt; module&lt;/h2&gt;
&lt;p&gt;The following code snippet defines a new class, &lt;code&gt;IP&lt;/code&gt; that
can read a packet and parse the header into its separate fields.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;ctypes&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;socket&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;struct&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nc&#34;&gt;IP&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Structure&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;_fields_&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;ihl&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;           &lt;span class=&#34;n&#34;&gt;c_ubyte&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;   &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;version&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;       &lt;span class=&#34;n&#34;&gt;c_ubyte&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;   &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;tos&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;           &lt;span class=&#34;n&#34;&gt;c_ubyte&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;   &lt;span class=&#34;mi&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;len&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;           &lt;span class=&#34;n&#34;&gt;c_ushort&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;id&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;            &lt;span class=&#34;n&#34;&gt;c_ushort&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;offset&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;        &lt;span class=&#34;n&#34;&gt;c_ushort&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;ttl&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;           &lt;span class=&#34;n&#34;&gt;c_ubyte&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;   &lt;span class=&#34;mi&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;protocol_num&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;  &lt;span class=&#34;n&#34;&gt;c_ubyte&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;   &lt;span class=&#34;mi&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;sum&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;           &lt;span class=&#34;n&#34;&gt;c_ushort&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;src&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;           &lt;span class=&#34;n&#34;&gt;c_uint32&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;dst&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;           &lt;span class=&#34;n&#34;&gt;c_uint32&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;fm&#34;&gt;__new__&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;cls&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;socket_buffer&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;None&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;cls&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;from_buffer_copy&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;socket_buffer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;fm&#34;&gt;__init__&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;socket_buffer&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;None&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
        &lt;span class=&#34;c1&#34;&gt;# human readable IP addresses&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;src_address&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;inet_ntoa&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;struct&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pack&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;&amp;lt;L&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dst_address&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;inet_ntoa&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;struct&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pack&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;&amp;lt;L&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dst&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can see that the &lt;code&gt;_fields_&lt;/code&gt; structure defines each part of the header,
giving the width in bits as the last argument. Being able to specify the bit
width is handy. Our &lt;code&gt;IP&lt;/code&gt; class inherits
from the &lt;code&gt;ctypes&lt;/code&gt; &lt;code&gt;Structure&lt;/code&gt; class, which specifies that we must have
a defined &lt;code&gt;_fields_&lt;/code&gt; structure before any instance is created.&lt;/p&gt;
&lt;h3 id=&#34;class-instantiation&#34;&gt;Class Instantiation&lt;/h3&gt;
&lt;p&gt;The wrinkle with &lt;code&gt;ctypes&lt;/code&gt; &lt;code&gt;Structure&lt;/code&gt; abstract base class is the &lt;code&gt;__new__&lt;/code&gt; method.
See the documentation for full details:
&lt;a href=&#34;https://docs.python.org/3.8/library/ctypes.html#ctypes.Structure&#34;&gt;ctypes module&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;__new__&lt;/code&gt; method takes the class reference as the first argument. It creates and
returns an instance of the class, which passes to the &lt;code&gt;__init__&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;We create the instance normally, but underneath, Python invokes the class
method &lt;code&gt;__new__&lt;/code&gt;, which fills out the &lt;code&gt;_fields_&lt;/code&gt; data structure immediately before
instantiation (when the &lt;code&gt;__init__&lt;/code&gt; method is called). As long as
you&amp;rsquo;ve defined the structure beforehand, just pass the &lt;code&gt;__new__&lt;/code&gt; method the
external (network packet) data, and the fields magically appear as attributes
on your instance.&lt;/p&gt;
&lt;h2 id=&#34;struct-module&#34;&gt;&lt;code&gt;struct&lt;/code&gt; module&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;struct&lt;/code&gt; module provides format characters that you used to specify the structure
of the binary data. The first character (in our case, &lt;code&gt;&amp;lt;&lt;/code&gt;) specifies the &amp;ldquo;endianness&amp;rdquo; of the
data. See the documentation for full details:
&lt;a href=&#34;https://docs.python.org/3.8/library/struct.html&#34;&gt;struct module&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;ipaddress&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;struct&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nc&#34;&gt;IP&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;fm&#34;&gt;__init__&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;buff&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;None&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;struct&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;unpack&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&amp;lt;BBHHHBBH4s4s&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;buff&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ver&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ihl&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&#34;mh&#34;&gt;0xF&lt;/span&gt;

        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tos&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;len&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;id&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;offset&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ttl&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;protocol_num&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sum&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;src&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dst&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;

        &lt;span class=&#34;c1&#34;&gt;# human readable IP addresses&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;src_address&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ipaddress&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ip_address&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dst_address&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ipaddress&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ip_address&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dst&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

        &lt;span class=&#34;c1&#34;&gt;# map protocol constants to their names&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;protocol_map&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&#34;ICMP&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&#34;TCP&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;17&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&#34;UDP&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here are the individual parts of the header.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;B 1 byte (&lt;code&gt;ver&lt;/code&gt;, &lt;code&gt;hdrlen&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;B 1 byte &lt;code&gt;tos&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;H 2 bytes &lt;code&gt;total len&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;H 2 bytes &lt;code&gt;identification&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;H 2 bytes &lt;code&gt;flags + frag offset&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;B 1 byte &lt;code&gt;ttl&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;B 1 byte &lt;code&gt;protocol&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;H 2 bytes &lt;code&gt;checksum&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;4s 4 bytes &lt;code&gt;src ip&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;4s 4 bytes &lt;code&gt;dst ip&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Everything is pretty straightforward, but with &lt;code&gt;ctypes&lt;/code&gt;, we could specify the bit-width
of the individual pieces. With &lt;code&gt;struct&lt;/code&gt;, there&amp;rsquo;s no format character for a &lt;code&gt;nybble&lt;/code&gt; (4 bits),
so we have to do some manipulation to get the &lt;code&gt;ver&lt;/code&gt; and &lt;code&gt;hdrlen&lt;/code&gt; from the first part of
the header.&lt;/p&gt;
&lt;h3 id=&#34;binary-manipulations&#34;&gt;Binary Manipulations&lt;/h3&gt;
&lt;p&gt;The wrinkle with &lt;code&gt;struct&lt;/code&gt; in this example is that we need to do some manipulation
of &lt;code&gt;header[0]&lt;/code&gt;, which contains a single byte but we need to create two variables
from that byte, each containing a &lt;code&gt;nybble&lt;/code&gt;.&lt;/p&gt;
&lt;h4 id=&#34;high-nybble&#34;&gt;High &lt;code&gt;nybble&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;We have one byte and for the &lt;code&gt;ver&lt;/code&gt; variable, we want the high-order &lt;code&gt;nybble&lt;/code&gt;.
The typical way you get the
high &lt;code&gt;nybble&lt;/code&gt; of a byte is to right-shift.&lt;/p&gt;
&lt;p&gt;We right shift the byte by 4 places, which is like prepending 4 zeros
at the front so the last 4 bytes fall off, leaving us with the first &lt;code&gt;nybble&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&amp;gt;&amp;gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;4&lt;/span&gt;
-----------------------------
&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4 id=&#34;low-nybble&#34;&gt;Low &lt;code&gt;nybble&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;We have one byte and for the &lt;code&gt;hdrlen&lt;/code&gt; variable, we  want the low-order &lt;code&gt;nybble&lt;/code&gt;. 
The typical way you get the
low &lt;code&gt;nybble&lt;/code&gt; of a byte is to &lt;code&gt;AND&lt;/code&gt; it with &lt;code&gt;F&lt;/code&gt; (00001111):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;F
&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;
-----------------------------
&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;rsquo;s look an example in the Python REPL:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;    &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;m&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;66&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;m&lt;/span&gt;
    &lt;span class=&#34;mi&#34;&gt;66&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;bin&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;s1&#34;&gt;&#39;0b1000010&#39;&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;# or 0100 0010&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;bin&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;s1&#34;&gt;&#39;0b100&#39;&lt;/span&gt;     &lt;span class=&#34;c1&#34;&gt;# or 0100&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;bin&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;mh&#34;&gt;0xF&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;s1&#34;&gt;&#39;0b10&#39;&lt;/span&gt;      &lt;span class=&#34;c1&#34;&gt;# or      0010&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, more specifically to our IPv4 case, the first byte in the header is
always &lt;code&gt;0x45 = 69 decimal = 01000101 binary&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;See what that looks like when we right-shift
it by 4 and then &lt;code&gt;AND&lt;/code&gt; it with &lt;code&gt;F&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;    &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;{0:08b}&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;format&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mh&#34;&gt;0x45&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;s1&#34;&gt;&#39;01000101&#39;&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;{0:04b}&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;format&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mh&#34;&gt;0x45&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;s1&#34;&gt;&#39;0100&#39;&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;{0:04b}&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;format&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mh&#34;&gt;0x45&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;mh&#34;&gt;0xF&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;s1&#34;&gt;&#39;0101&#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You don&amp;rsquo;t have to know binary manipulation backward and forward for decoding an IP header,
but there are some patterns like these (shift and &lt;code&gt;AND&lt;/code&gt;) you will see over and over again as you code and as you explore other hackers&amp;rsquo; code.&lt;/p&gt;
&lt;p&gt;That seems like a lot of work doesn&amp;rsquo;t it? In the case where we have to do some bit
shifting, it does take effort. But for many cases (e.g. ICMP),
everything works on an 8-byte boundary and so is very
simple to set up. Here is an &amp;ldquo;Echo Reply&amp;rdquo; ICMP message;
you can see that each parameter of the ICMP header can be
defined in a &lt;code&gt;struct&lt;/code&gt; with one of the existing format letters (BBHHH) (RFC777):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Echo&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;or&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Echo&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Reply&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Message

&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;                   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;                   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;                   &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;3&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;     &lt;/span&gt;Type&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;     &lt;/span&gt;Code&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;          &lt;/span&gt;Checksum&lt;span class=&#34;w&#34;&gt;             &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;           &lt;/span&gt;Identifier&lt;span class=&#34;w&#34;&gt;          &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;Sequence&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Number&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;     &lt;/span&gt;Data&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;...
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;+-+-+-+-+-
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;A quick way to parse that would simply be:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nc&#34;&gt;ICMP&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;fm&#34;&gt;__init__&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;buff&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;struct&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;unpack&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&amp;lt;BBHHH&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;buff&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;type&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;code&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sum&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;id&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;seq&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;header&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;You can use either the &lt;code&gt;ctypes&lt;/code&gt; module or the &lt;code&gt;struct&lt;/code&gt; module to read and parse
binary data. Here is an example of instantiating the class no matter which method
you use. You instantiate the &lt;code&gt;IP&lt;/code&gt; class with your packet data
in the variable &lt;code&gt;buff&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;   &lt;span class=&#34;n&#34;&gt;mypacket&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;IP&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;buff&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
   &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;sa&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mypacket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;src_address&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt; -&amp;gt; &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mypacket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dst_address&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With &lt;code&gt;ctypes&lt;/code&gt;, make sure you define your &lt;code&gt;_fields_&lt;/code&gt; structure and hand
the data to it in the &lt;code&gt;_new_&lt;/code&gt; method. When you instantiate the class, you&amp;rsquo;ll have the
access to the data attributes automatically.&lt;/p&gt;
&lt;p&gt;With &lt;code&gt;struct&lt;/code&gt;, you define how to read the data with a format string. For data attributes
that don&amp;rsquo;t lie on the 8-byte boundary, you may need to do some binary manipulation.&lt;/p&gt;
&lt;p&gt;In short, use whichever method fits your brain. But always be aware that you may see
code from others that use a different method. Hopefully, now you&amp;rsquo;ll see it and
understand it.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Include HTML in LaTeX</title>
    <link href="https://reachtim.com/articles/include-html-in-latex.html" rel="alternate" />
    <id>https://reachtim.com/articles/include-html-in-latex.html</id>
    <published>2019-05-06T00:00:00Z</published>
    <updated>2019-05-06T00:00:00Z</updated>
    <category term="LaTeX" />
    <summary type="text">How to include HTML content in your LaTeX documents.</summary>
    <content type="html">&lt;h4 class=&#34;article-title&#34; id=&#34;introduction&#34;&gt;Introduction&lt;/h4&gt;
&lt;p&gt;When I first started supporting multiple users of LaTeX in my day job,
a question I often heard was &amp;ldquo;How do I include an HTML table in my document?&amp;rdquo;.
The answer was, of course, &amp;ldquo;It doesn&amp;rsquo;t work that way; you can&amp;rsquo;t do that.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;But I found that, with some restrictions, you actually can include HTML by
converting it to PDF and including that in your LaTeX document. &lt;/p&gt;
&lt;p&gt;In this article I&amp;rsquo;ll show how I do that for myself and how you can include
HTML pages that keep the original styles, colors and so forth once it has 
been converted to PDF. And if you need them, you can keep the internal links
from the HTML so they work in your final PDF.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;wkhtmltopdf&#34;&gt;wkhtmltopdf&lt;/h4&gt;
&lt;p&gt;The tool &lt;code&gt;wkhtmltopdf&lt;/code&gt; converts HTML to PDF. The &lt;a href=&#34;https://wkhtmltopdf.org/&#34;&gt;wkhtmltopdf&lt;/a&gt;
website summarizes it:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) 
command line tools to render HTML into PDF and various 
image formats using the Qt WebKit rendering engine. 
These run entirely &amp;ldquo;headless&amp;rdquo; and do not require a display or display service.&lt;/p&gt;
&lt;p&gt;There is also a C library, if you&amp;rsquo;re into that kind of thing.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It&amp;rsquo;s a great tool. Here&amp;rsquo;s the command I use (but put it all on one line):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;/usr/local/bin/wkhtmltopdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-s&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Letter&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;--no-background&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;--minimum-font-size&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;--no-outline&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;--no-pdf-compression&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;--disable-external-links&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;--enable-internal-links&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;filename.html&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;filename.pdf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Those options request the conversion to be rendered at letter-size with no background.
You can play around with the font size so it matches your documents font size.&lt;/p&gt;
&lt;p&gt;Run this and you will have a single PDF with possibly multiple pages, using the same style, 
colors, and links that are in the HTML. &lt;/p&gt;
&lt;h5 class=&#34;article-title&#34; id=&#34;if-you-need-to-create-an-appendix&#34;&gt;If You Need to Create an Appendix&lt;/h5&gt;
&lt;p&gt;If you want the html you are including to be a separate chapter or appendix, then
instead of &lt;code&gt;--no-outline&lt;/code&gt;, use this command instead:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;--outline-depth&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;toc&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;--xsl-style-sheet&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;wk_toc.xsl&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;--toc-header-text&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;Appendix A&#34;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This asks for a table of contents using an XSL stylesheet with the specified header text.
Get my version of the &lt;code&gt;wk_toc.xsl&lt;/code&gt; stylesheet here:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://gist.github.com/tiarno/094ca5dc7f9c3ae560af9ace975881d3&#34;&gt;gist.github.com/tiarno&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Alternatively, you can get the file &lt;code&gt;wk_toc.xsl&lt;/code&gt; yourself and if you know a 
little XSL, you can modify it for your needs. To generate the file, 
give this command::&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;wkhtmltopdf --dump-default-toc-xsl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can read more about the options in the &lt;code&gt;wkhtmltopdf&lt;/code&gt; help documentation.&lt;/p&gt;
&lt;h5 class=&#34;article-title&#34; id=&#34;if-you-need-to-preserve-internal-links&#34;&gt;If You Need to Preserve Internal Links&lt;/h5&gt;
&lt;p&gt;Skip this section if you don&amp;rsquo;t need to preserve internal links in 
the original HTML.  &lt;/p&gt;
&lt;p&gt;If you do need internal links, you&amp;rsquo;ll find that there is already a 
package to help with that, the &lt;code&gt;pax&lt;/code&gt; package.
From the documentation:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If PDF files are in­cluded us­ing pdfTEX, PDF an­no­ta­tions are stripped. 
The pax project of­fers a so­lu­tion with­out al­ter­ing pdfTEX. 
A Java pro­gram (pax.jar) parses the PDF file that will later be in­cluded. 
The pro­gram then writes the data of the an­no­ta­tions into a file that can be read by TEX.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You can find it on &lt;a href=&#34;https://www.ctan.org/pkg/pax&#34;&gt;CTAN&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This package works to retain the internal links from the included PDF. Unfortunately,
&lt;code&gt;wkhtmltopdf&lt;/code&gt; creates the PDF in a way that is incompatible with this
Java program mentioned in the &lt;code&gt;pax&lt;/code&gt; package documentation. &lt;/p&gt;
&lt;p&gt;I mention it here to save you going down the rabbit hole of trying to get 
it to work. You still need the &lt;code&gt;pax&lt;/code&gt; package itself. But even though the 
Java jar file is fantastic for other pdfs,  with &lt;code&gt;wkhtmltopdf&lt;/code&gt;, you need a 
different solution.  &lt;/p&gt;
&lt;p&gt;Enter &lt;code&gt;PAXMaker&lt;/code&gt;.&lt;/p&gt;
&lt;h5 class=&#34;article-title&#34; id=&#34;paxmaker&#34;&gt;PAXMaker&lt;/h5&gt;
&lt;p&gt;This little tool is available on my GitHub repository:
&lt;a href=&#34;https://github.com/tiarno/paxmaker&#34;&gt;PAXMaker&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It requires the PyPDF2 Python package.
As you can read on that repo, you can use it by following these steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Convert your html file to pdf with wkhtmltopdf&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;paxmaker.py&lt;/code&gt; on the pdf (the program takes a single argument, the name of the pdf file)&lt;/li&gt;
&lt;li&gt;In the LaTeX file in which you want this pdf inserted: load the 
  &lt;code&gt;pdfpages&lt;/code&gt; and &lt;code&gt;pax&lt;/code&gt; packages, and include the pdf as described in the
  next section.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;paxmaker.py&lt;/code&gt; file does for the wkhtmltopdf-generated pdfs what the 
pax java program does for normal pdfs. It writes a &lt;code&gt;.pax&lt;/code&gt; file that will be
read by the LaTeX &lt;code&gt;pax&lt;/code&gt; package when you compile your final document.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;pdfpages&#34;&gt;pdfpages&lt;/h4&gt;
&lt;p&gt;So far you have generated a PDF file from the HTML, possibly preserving internal
links, and possibly creating it as an appendix. 
Now lets get that generated PDF into your LaTeX document.&lt;/p&gt;
&lt;p&gt;You include external PDFs in a LaTeX document with the &lt;code&gt;pdfpages&lt;/code&gt; package, by Andreas Matthias.
Get it on &lt;a href=&#34;https://www.ctan.org/pkg/pdfpages&#34;&gt;CTAN&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;This package simplifies the insertion of external multi-page PDF
documents into LaTeX documents. Pages can be freely selected and
similar to psnup it is possible to put several logical pages onto
each sheet of paper. Furthermore a lot of hypertext features like
hyperlinks and article threads are provided. This package supports
pdfTeX, VTeX, and XeTeX. With VTeX it is even possible to use
this package to insert PostScript files, in addition to PDF files.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Then, at the location you want the original HTML pages to be included,
use this tag:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\IfFileExists&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;filename.pdf&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\includepdf&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;[pages=-,addtotoc={1,subsection,2,title,htmllabel}]&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;filename&lt;span class=&#34;nb&#34;&gt;}}&lt;/span&gt; 
    &lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\par\textcolor&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;red&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;MISSING FILE!&lt;span class=&#34;nb&#34;&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;\IfFileExists&lt;/code&gt; command executes its first argument if the file exists or
its second argument if it doesn&amp;rsquo;t exist. &lt;/p&gt;
&lt;p&gt;So if the file is present, you 
invoke the &lt;code&gt;includepdf&lt;/code&gt; command from the &lt;code&gt;pdfpages&lt;/code&gt; package, specifying
that it should include all pages (&lt;code&gt;pages=-&lt;/code&gt;), it should add a title in the table
of contents at the subsection level with the  text &lt;code&gt;title&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;If the file 
does not exist, it will put a paragraph in your document with a big red 
&amp;ldquo;MISSING FILE&amp;rdquo; warning.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;summary&#34;&gt;Summary&lt;/h4&gt;
&lt;p&gt;You can include HTML in your LaTeX document as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;convert your HTML to PDF with &lt;code&gt;wkhtmltopdf&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;if you need internal links&lt;/em&gt;, use PAXMaker to write a helper &lt;code&gt;pax&lt;/code&gt; file
  and make sure you use the &lt;code&gt;pax&lt;/code&gt; package in your LaTeX document&lt;/li&gt;
&lt;li&gt;use the &lt;code&gt;pdfpages&lt;/code&gt; LaTeX package to include the PDF rendering of the HTML file.&lt;/li&gt;
&lt;li&gt;compile your final LaTeX document&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is a powerful method to include possibly many HTML pages into a LaTeX document.
You can use CSS with the HTML to make the included pages match your document.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Testing PDFs with Python</title>
    <link href="https://reachtim.com/articles/testing-pdfs-with-python.html" rel="alternate" />
    <id>https://reachtim.com/articles/testing-pdfs-with-python.html</id>
    <published>2015-01-26T00:00:00Z</published>
    <updated>2015-01-26T00:00:00Z</updated>
    <category term="Python" />
    <summary type="text">Methods for testing PDFs using Python.</summary>
    <content type="html">&lt;h2 id=&#34;overview&#34;&gt;Overview&lt;/h2&gt;
&lt;p&gt;When you generate PDFs, you need a way to test their integrity&amp;ndash;not only must they be valid, but they should behave correctly and display consistently, even on different platforms. This article describes how you can use the PyPDF2 library to test your PDF files for broken links (both internal and external), and how to find fonts that are not embedded in the PDF.&lt;/p&gt;
&lt;p&gt;Note that some PDF readers are &amp;lsquo;smart&amp;rsquo; and will create a live hyperlink from a string of text that looks like a URL, even though the text is not coded as a URL in the PDF file. The technique described in this article does not address this issue&amp;ndash;it only tests the actual URLs present in the PDF file.&lt;/p&gt;
&lt;p&gt;Code from this article is available as a &lt;a href=&#34;https://gist.github.com/tiarno/dea01f70a54cac52f6a6#file-pdf-testing-md&#34;&gt;gist&lt;/a&gt; on GitHub. Get a copy and play along.&lt;/p&gt;
&lt;h2 id=&#34;testing-internal-and-external-links&#34;&gt;Testing Internal and External Links&lt;/h2&gt;
&lt;p&gt;To test the links, we&amp;rsquo;ll create a function to check the urls found inside the PDF file. This function uses the &lt;code&gt;requests&lt;/code&gt; library, which you can install with &lt;code&gt;pip&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;There may be some &lt;code&gt;ftp&lt;/code&gt; links, which are not directly supported by the &lt;code&gt;requests&lt;/code&gt; library. You can either use the &lt;code&gt;urllib&lt;/code&gt; package as in the following code, or you can use the &lt;code&gt;requests-ftp&lt;/code&gt; package, available on &lt;code&gt;pypi&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;PyPDF2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pprint&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pprint&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;requests&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;sys&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;urllib&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;check_ftp&lt;/code&gt; function checks a given url for a response. If it fails, or if the response is empty, it returns &lt;code&gt;False&lt;/code&gt;, along with the reason; otherwise it returns &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;def check_ftp(url):
    try:
        response = urllib.urlopen(url)
    except IOError as e:
        result, reason = False, e
    else:
        if response.read():
            result, reason = True, &#39;okay&#39;
        else:
            result, reason = False, &#39;Empty Page&#39;
    return result, reason
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;check_url&lt;/code&gt; function is also simple: If the url starts with &lt;code&gt;ftp&lt;/code&gt;, it delegates to the &lt;code&gt;check_ftp&lt;/code&gt; function. Otherwise, it attempts to &lt;code&gt;get&lt;/code&gt; the url with some timeout value using typical header values. The the function returns the response along with the reason it succeeded or failed.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;def check_url(url, auth=None):
    headers = {&#39;User-Agent&#39;: &#39;Mozilla/5.0&#39;, &#39;Accept&#39;: &#39;*/*&#39;}
    if url.startswith(&#39;ftp://&#39;):
        result, reason = check_ftp(url)
    else:
        try:
            response = requests.get(url, timeout=6, auth=auth, headers=headers)
        except (requests.ConnectionError,
                requests.HTTPError,
                requests.Timeout) as e:
            result, reason = False, e
        else:
            if response.text:
                result, reason = response.status_code, response.reason
            else:
                result, reason = False, &#39;Empty Page&#39;

    return result, reason
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that we have this utility, we can check the PDF file. We will create four lists:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;links&lt;/strong&gt; The internal PDF links in the file; for example, a reference to a section or figure.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;badlinks&lt;/strong&gt; Of the internal links in the file, these are links that target a missing destination (broken link).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;urls&lt;/strong&gt; The links from the PDF to an external location; for example, a hyperlink to a web site.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;badurls&lt;/strong&gt; Of the external links in the file, these are the urls that target a missing destination (broken url)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now for the PyPDF2 goodies. The following &lt;code&gt;check_pdf&lt;/code&gt; function loops over the pages in the PDF file object. For each page, it walks through the &lt;code&gt;Annots&lt;/code&gt; dictionary. If that dictionary has an action (&lt;code&gt;\A&lt;/code&gt;) with a key of &lt;code&gt;\D&lt;/code&gt; (destination?), that is an internal link, so update the &lt;code&gt;links&lt;/code&gt; list with the destination.&lt;/p&gt;
&lt;p&gt;If the dictionary has an action with a key of &lt;code&gt;\URI&lt;/code&gt;, it is an external link. Check the external links with the &lt;code&gt;check_url&lt;/code&gt; function and update the &lt;code&gt;urls&lt;/code&gt; and &lt;code&gt;bad_urls&lt;/code&gt; lists.&lt;/p&gt;
&lt;p&gt;After checking each page, get a list of all the anchors in the PDF with the &lt;code&gt;getNamedDestinations&lt;/code&gt; attribute; compare that list of all known anchors to the list of internal links we just created. If there is a link with no matching anchor, that link belongs in the &lt;code&gt;badlinks&lt;/code&gt; list.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;check_pdf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pdf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;links&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;urls&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;badurls&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;page&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pdf&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;page&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;annot&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/Annots&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[])]:&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;dst&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;annot&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/A&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/D&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;url&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;annot&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/A&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/URI&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
            &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;dst&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
                &lt;span class=&#34;n&#34;&gt;links&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dst&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
            &lt;span class=&#34;k&#34;&gt;elif&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
                &lt;span class=&#34;n&#34;&gt;urls&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
                &lt;span class=&#34;n&#34;&gt;result&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;reason&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;check_url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
                &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;not&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;result&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
                    &lt;span class=&#34;n&#34;&gt;badurls&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;url&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;reason&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;%r&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;%&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;reason&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;anchors&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pdf&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;namedDestinations&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;keys&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;badlinks&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;links&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;not&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;anchors&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;links&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;badlinks&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;urls&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;badurls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally, make the code into a callable script that takes a single argument, the path to the PDF file. Then print the results of the &lt;code&gt;check_pdf&lt;/code&gt; function on &lt;code&gt;stdout&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;vm&#34;&gt;__name__&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;__main__&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;fname&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sys&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;argv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;Checking &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;%&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fname&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;pdf&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fname&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;links&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;badlinks&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;urls&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;badurls&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;check_pdf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pdf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;urls: &#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;urls&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;bad links: &#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;badlinks&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;bad urls: &#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;badurls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&#34;test-for-embedded-fonts&#34;&gt;Test for Embedded Fonts&lt;/h2&gt;
&lt;p&gt;Test to make sure that the fonts used in the PDF file are embedded. If a font is not embedded, your PDF file may display differently on different machines, even if it is a font that is putatively &amp;ldquo;standard&amp;rdquo;, like &lt;code&gt;Times Roman&lt;/code&gt; or &lt;code&gt;Helvetica&lt;/code&gt;. To insure that your PDF displays as intended on any machine, all fonts must be embedded.&lt;/p&gt;
&lt;p&gt;In the following code, the &lt;code&gt;walk&lt;/code&gt; function is a recursive function that takes a dictionary-like object (&lt;code&gt;obj&lt;/code&gt;)  and two sets (&lt;code&gt;fnt&lt;/code&gt; and &lt;code&gt;emb&lt;/code&gt;). It walks the given dictionary object: for every key in the given dictionary, the function calls itself on the corresponding value (if that value is a nested dictionary).&lt;/p&gt;
&lt;p&gt;If the dictionary has a key called &lt;code&gt;BaseFont&lt;/code&gt;, the value corresponding to that key is the name of a font used in the PDF; add that font name to the &lt;code&gt;fnt&lt;/code&gt; set of &lt;strong&gt;fonts used&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If the dictionary has a key called &lt;code&gt;FontName&lt;/code&gt;, the dictionary is a descriptor for that font, so check for another key in the same font descriptor dictionary that begins with &lt;code&gt;FontFile&lt;/code&gt; (the key could be &lt;code&gt;FontFile&lt;/code&gt;, &lt;code&gt;FontFile2&lt;/code&gt;, or &lt;code&gt;FontFile3&lt;/code&gt;). If that key exists, the font is embedded; add that font name to the set of &lt;strong&gt;fonts embedded&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If the two sets are not identical, there are unembedded fonts in the PDF.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;n&#34;&gt;fontkeys&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/FontFile&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;/FontFile2&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;/FontFile3&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;walk&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fnt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;emb&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;/BaseFont&#39;&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;fnt&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;add&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/BaseFont&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;elif&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;/FontName&#39;&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;and&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fontkeys&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;intersection&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)):&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;emb&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;add&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/FontName&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;k&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;hasattr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;k&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;keys&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;walk&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;k&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fnt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;emb&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fnt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;emb&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally, make the code into a callable script that takes a single argument, the path to the PDF file. &lt;/p&gt;
&lt;p&gt;Start with two empty sets, &lt;code&gt;fonts&lt;/code&gt; and &lt;code&gt;embedded&lt;/code&gt;. Open the file with PyPDF2. The library gives us access to the internal structure of the PDF. We loop over each page in the PDF, passing the page&amp;rsquo;s &lt;code&gt;Resources&lt;/code&gt; dictionary to the &lt;code&gt;walk&lt;/code&gt; function, described above. Add the corresponding results to the two sets and calculate the unembedded fonts by differencing the sets.&lt;/p&gt;
&lt;p&gt;Print the fonts used in the PDF file and if there are unembedded fonts, print their names as well. Of course here you can do anything you want with the information such as save it to test database, print a report, and so on.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;vm&#34;&gt;__name__&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;__main__&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;fname&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sys&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;argv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;pdf&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fname&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;fonts&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;embedded&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;page&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pdf&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;page&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;walk&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;obj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/Resources&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fonts&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;embedded&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;fonts&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fonts&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;union&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;embedded&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;embedded&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;union&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;unembedded&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fonts&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;embedded&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;Font List&#39;&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;pprint&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;sorted&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fonts&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)))&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;unembedded&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;se&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;Unembedded Fonts&#39;&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;pprint&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;unembedded&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&#34;using-pypdf2-methods&#34;&gt;Using PyPDF2 Methods&lt;/h2&gt;
&lt;p&gt;Obviously, the more you can specify about the PDFs you produce, the more you can test. For example, you may know that your PDF should have specific metadata, should be encrypted, contain a certain number of pages, and so on.&lt;/p&gt;
&lt;p&gt;You can test for those conditions with the built-in tools that the PDFFileReader in pyPDF2 provides. If you have a PDFFileReader instance, you can use the following properties for testing:&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;documentInfo&lt;/dt&gt;
&lt;dd&gt;returns the document metadata such as author, creator, producer, subject, and title.&lt;/dd&gt;
&lt;dt&gt;isEncrypted&lt;/dt&gt;
&lt;dd&gt;returns boolean value specifiying whether the document is encrypted&lt;/dd&gt;
&lt;dt&gt;numPages&lt;/dt&gt;
&lt;dd&gt;returns the number of pages in the document&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;
&lt;p&gt;If you produce PDF documents, you need to test them. The more you can specify about your PDFs, the more you can test. This article describes how you can test that the links (internal and external) are valid and that the fonts used in the document are embedded.&lt;/p&gt;
&lt;p&gt;Get a copy of this code and play:
Code from this article is available as a &lt;a href=&#34;https://gist.github.com/tiarno/dea01f70a54cac52f6a6#file-pdf-testing-md&#34;&gt;gist&lt;/a&gt; on GitHub.&lt;/p&gt;
&lt;p&gt;Do you test other things in your own PDF documents? Leave a comment!&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Manipulate PDFs with Python</title>
    <link href="https://reachtim.com/articles/manipulate-pdfs-with-python.html" rel="alternate" />
    <id>https://reachtim.com/articles/manipulate-pdfs-with-python.html</id>
    <published>2015-01-12T00:00:00Z</published>
    <updated>2015-01-12T00:00:00Z</updated>
    <category term="Python" />
    <summary type="text">Methods for manipulating and extracting information from PDF documents using Python.</summary>
    <content type="html">&lt;p&gt;This article was originally published on &lt;a href=&#34;https://www.binpress.com/tutorial/manipulating-pdfs-with-python/167&#34;&gt;BinPress.com&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;overview&#34;&gt;Overview&lt;/h2&gt;
&lt;p&gt;PDF documents are beautiful things, but that beauty is often only skin deep. Inside, they might have any number of structures that are difficult to understand and exasperating to get at. The PDF reference specification (ISO 32000-1) provides rules, but it is programmers who follow them, and they, like all programmers, are a creative bunch.&lt;/p&gt;
&lt;p&gt;That means that in the end, a beautiful PDF document is really meant to be read and its internals are not to be messed with. Well, we are programmers too, and we are a creative bunch, so we will see how we can get at those internals.&lt;/p&gt;
&lt;p&gt;Still, the best advice if you have to extract or add information to a PDF is: &lt;strong&gt;don&amp;rsquo;t do it&lt;/strong&gt;. Well, don&amp;rsquo;t do it if there is any way you can get access to the information further upstream. If you want to scrape that spreadsheet data in a PDF, see if you can get access to it before it became part of the PDF. Chances are, now that is is inside the PDF, it is just a bunch of lines and numbers  with no connection to its former structure of cells, formats, and headings. &lt;/p&gt;
&lt;p&gt;If you cannot get access to the information further upstream, this tutorial will show you some of the ways you can get inside the PDF using Python.&lt;/p&gt;
&lt;h2 id=&#34;survey-of-tools&#34;&gt;Survey of Tools&lt;/h2&gt;
&lt;p&gt;There are several Python packages that can help. The following list displays some of the most popular ones although undoubtedly I&amp;rsquo;ve omitted some tools.&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;pdfrw&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Last update: 2012. Read and write PDF files; watermarking, copying images from one PDF to another. Includes sample code. Python 2.5&amp;ndash;2.7. MIT License. &lt;a href=&#34;https://code.google.com/p/pdfrw/&#34;&gt;repo&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;slate&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Active development. Simplifies extracting text from PDF files. Wrapper around &lt;code&gt;PDFMiner&lt;/code&gt;. Includes documentation on GitHub and PyPI. Python 2.6. GPL License.  &lt;a href=&#34;https://github.com/timClicks/slate&#34;&gt;repo&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;PDFQuery&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Active development. PDF scraping with Jquery or XPath syntax. Requires &lt;code&gt;PDFMiner&lt;/code&gt;, &lt;code&gt;pyquery&lt;/code&gt; and &lt;code&gt;lxml&lt;/code&gt; libraries. Includes sample code, documentation. Seems to be Python 2.x. MIT License. &lt;a href=&#34;https://github.com/jcushman/pdfquery&#34;&gt;repo&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;PDFMiner&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Active development. Extracting text, images, object coordinates, metadata from PDF files. Pure Python. Includes sample code and command line interface; Google group and documentation. Python 2.x only. MIT License. &lt;a href=&#34;https://github.com/euske/pdfminer/&#34;&gt;repo&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;PyPDF2&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Active development. Split, merge, crop, etc. of PDF files. Pure Python. Includes sample code and command line interface, documentation. Python 2 and 3. BSD License. &lt;a href=&#34;https://github.com/mstamy2/PyPDF2&#34;&gt;repo&lt;/a&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;h3 id=&#34;related-tools&#34;&gt;Related Tools&lt;/h3&gt;
&lt;p&gt;This article focuses on extracting information with PDFMiner and manipulating PDFs with PyPDF2.&lt;/p&gt;
&lt;p&gt;There are other Python projects for creating PDFs, and several non-Python tools available for manipulating PDF. If none of the Python solutions described here fit your situation, see the section [Other Tools][] for more information.&lt;/p&gt;
&lt;h2 id=&#34;extracting-pdfminer&#34;&gt;Extracting: PDFMiner&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;PDFMiner&lt;/strong&gt; library excels at extracting data and coordinates from a PDF. In most cases, you can use the included command-line scripts to extract text and images (&lt;code&gt;pdf2txt.py&lt;/code&gt;) or find objects and their coordinates (&lt;code&gt;dumppdf.py&lt;/code&gt;). If you&amp;rsquo;re dealing with a particularly nasty PDF and you need to get more detailed , you can import the package and use it as library. Install with &lt;code&gt;pip&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;the-pdf2txtpy-command&#34;&gt;The &lt;code&gt;pdf2txt.py&lt;/code&gt; command&lt;/h3&gt;
&lt;p&gt;The package includes the &lt;code&gt;pdf2txt.py&lt;/code&gt; command-line command, which you can use to extract text and images. The command supports many options and is very flexible. Some popular options are shown below. See the usage information for complete details.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;pdf2txt.py&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;options&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;filename.pdf
Options:
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-o&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;output&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;file&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;name
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-p&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;comma-separated&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;list&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;page&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;numbers&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;to&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;extract
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-t&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;output&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;format&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;text/html/xml/tag&lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Tagged&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;PDFs&lt;span class=&#34;o&#34;&gt;])&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-O&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;dirname&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;triggers&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;extraction&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;images&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;from&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;PDF&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;into&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;directory&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-P&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;password
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That&amp;rsquo;s right, you can even use the command to convert PDF to HTML or XML!
For example, say you want the HTML version of the first and third pages of your PDF, including images.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;pdf2txt.py&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-O&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;myoutput&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-o&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;myoutput/myfile.html&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-t&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;html&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-p&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;,3&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;myfile.pdf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that the package cannot recognize text drawn as images because that would require optical character recognition. It does extract the corresponding locations, font names, font sizes, etc., for each bit of text. Often this is good enough&amp;ndash;you can extract the text and use typical Python patterns for text processing to get the text or data into a usable form.&lt;/p&gt;
&lt;h3 id=&#34;the-dumppdfpy-command&#34;&gt;The &lt;code&gt;dumppdf.py&lt;/code&gt; command&lt;/h3&gt;
&lt;p&gt;The package also includes the &lt;code&gt;dumppdf.py&lt;/code&gt; command-line command, which you can use to find the objects and their coordinates inside a PDF file.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;dumppdf.py&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;options&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;filename.pdf
Options:
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-a&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;dump&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;all&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;objects
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-p&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;comma-separated&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;list&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;page&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;numbers&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;to&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;extract
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-i&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;object&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;id
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-E&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;dirname&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;extract&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;embedded&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;files&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;from&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;the&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;PDF&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;into&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;directory&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-T&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;dump&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;the&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;table&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;contents&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;bookmark&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;outlines&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;-p&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;password
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is very useful when you have a problematic PDF and you want to know the exact object IDs that it contains. For example, you might need to know the object ID corresponding to an image in the PDF so you can extract only that image. Here is an example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;dumppdf.py&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-a&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;filename.pdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;more
&lt;span class=&#34;c1&#34;&gt;# search for the string &#39;Image&#39; and find the ID; &#39;33&#39; for example.&lt;/span&gt;
dumppdf.py&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-i&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;33&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-r&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;filename.pdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&amp;gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;myimage.jpg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You run the command with &lt;code&gt;-a&lt;/code&gt; option first so you can review the objects and their IDs, find the object you want (images have a &lt;code&gt;SubType&lt;/code&gt; of &lt;code&gt;Image&lt;/code&gt;), then re-run the command with the &lt;code&gt;-i&lt;/code&gt; option to extract only that object.&lt;/p&gt;
&lt;h3 id=&#34;use-as-a-library&#34;&gt;Use as a Library&lt;/h3&gt;
&lt;p&gt;If you are writing Python code and you don&amp;rsquo;t want to shell out to the command line with &lt;code&gt;os.system&lt;/code&gt; or &lt;code&gt;subprocess&lt;/code&gt;, you use the package as a library. For example, to extract text from a PDF:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;cStringIO&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;StringIO&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pdfminer.pdfinterp&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFResourceManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFPageInterpreter&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pdfminer.converter&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TextConverter&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pdfminer.layout&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;LAParams&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pdfminer.pdfpage&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFPage&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;convert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fname&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pages&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;None&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;not&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;pagenums&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;pagenums&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;output&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;StringIO&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;manager&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFResourceManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;converter&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TextConverter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;manager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;output&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;laparams&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;LAParams&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;())&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;interpreter&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFPageInterpreter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;manager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;converter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;file&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fname&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;page&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFPage&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;get_pages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pagenums&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;interpreter&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;process_page&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;page&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;close&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;converter&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;close&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;text&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;output&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getvalue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;output&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;close&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;convert&lt;/code&gt; function is called with the name of the PDF file to convert, and optionally, a list of pages to convert. By default, all pages are converted to text. The function returns a string containing the text. To retrieve the text extracted from &lt;code&gt;myfile.pdf&lt;/code&gt; on pages 6 and 8, call the function as follows (internal page numbering starts at zero).&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;n&#34;&gt;convert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;myfile.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pages&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;PDFMiner provides functions to access the document&amp;rsquo;s table of contents. If your PDF has bookmarks or &amp;ldquo;Outlines&amp;rdquo;, you can extract the table of contents with this code snippet:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;table class=&#34;highlighttable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;linenos&#34;&gt;&lt;div class=&#34;linenodiv&#34;&gt;&lt;pre&gt;&lt;span class=&#34;normal&#34;&gt; 1&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 2&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 3&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 4&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 5&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 6&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 7&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 8&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 9&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;10&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;11&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;12&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;13&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#34;code&#34;&gt;&lt;div&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pdfminer.pdfparser&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFParser&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pdfminer.pdfdocument&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFDocument&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;get_toc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pdf_path&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pdf_path&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;parser&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFParser&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;document&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PDFDocument&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;parser&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;toc&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;level&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;structelem&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;document&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;get_outlines&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;():&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;toc&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;((&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;level&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;toc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you change line 11 to read &lt;code&gt;toc.append((level, title, a.resolve()))&lt;/code&gt;, the resulting list will contain the actual internal destinations for bookmarks. A typical entry looks like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;(1, u&#39;Other Commands&#39;, {&#39;S&#39;: /GoTo, &#39;D&#39;: &#39;section.8&#39;})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;where the section &amp;ldquo;Other Commands&amp;rdquo; is a first level section and it is the 8th section in the PDF. You can use this information to construct an HTML URL that opens the PDF at that destination. In this example, the URL would look like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&amp;lt;a href=&#34;path/to/myfile.pdf#section.8&#34;&amp;gt;link text&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When you follow the URL, the browser opens the PDF at the location of the section titled &amp;ldquo;Other Commands&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;How do you know how to piece everything together, what with the manager, the parser, the document, etc.? The documentation for the package is helpful, but in addition, the source code for the command-line commands is straightforward and shows how you can configure your own code. The source for the scripts reside in a subdirectory called &lt;code&gt;tools&lt;/code&gt;. In fact, the entire source code is readable and provides good, Pythonic, examples.&lt;/p&gt;
&lt;h2 id=&#34;manipulating-pypdf2&#34;&gt;Manipulating: PyPDF2&lt;/h2&gt;
&lt;p&gt;You can manipulate PDF files in a variety of ways using the pure-Python PyPDF2 toolkit. The original &lt;strong&gt;pyPDF&lt;/strong&gt; library is officially no longer being developed but the &lt;strong&gt;pyPDF2&lt;/strong&gt; library has taken up the project under the new name and continues to develop and enhance the library. The development team is dedicated to keeping the project backward compatible. Install with &lt;code&gt;pip&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Want to merge two PDFs? Merge can mean a couple of things&amp;ndash;you can merge, in the sense of inserting the contents of one PDF &lt;em&gt;after&lt;/em&gt; the content of another, or you can merge by applying one PDF page &lt;em&gt;on top of&lt;/em&gt; another. &lt;/p&gt;
&lt;h3 id=&#34;merge-layer&#34;&gt;Merge (layer)&lt;/h3&gt;
&lt;p&gt;For an example of the latter case, if you have a one-page PDF containing a watermark, you can layer it onto each page of another PDF.
Say you&amp;rsquo;ve created a PDF with transparent watermark text (using Photoshop, Gimp, or LaTeX). If this PDF is named &lt;code&gt;wmark.pdf&lt;/code&gt;, the following python code will stamp each page of the target PDF with the watermark.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;PyPDF2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileWriter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;output&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileWriter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;ipdf&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;sample2e.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;wpdf&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;wmark.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;watermark&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;wpdf&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;xrange&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ipdf&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getNumPages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;page&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ipdf&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;page&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mergePage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;watermark&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;output&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;page&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;newfile.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;wb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
   &lt;span class=&#34;n&#34;&gt;output&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If your watermark PDF is not transparent it will hide the underlying text. In that case, make sure the content of the watermark displays on the header or margin so that when it is merged, no text is masked. For example, you can use this technique to stamp a logo or letterhead on each page of the target PDF.&lt;/p&gt;
&lt;h3 id=&#34;merge-append&#34;&gt;Merge (append)&lt;/h3&gt;
&lt;p&gt;This snippet takes all the PDFs in a subdirectory named &lt;code&gt;mypdfs&lt;/code&gt; and puts them in name-sorted order into a new PDF called &lt;code&gt;output.pdf&lt;/code&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;PyPDF2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileMerger&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;os&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;merger&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileMerger&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;files&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;os&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;listdir&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;mypdfs&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;endswith&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)]&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fname&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;sorted&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;files&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;merger&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;os&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;path&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;join&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;mypdfs&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fname&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)))&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;merger&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;output.pdf&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&#34;delete&#34;&gt;Delete&lt;/h3&gt;
&lt;p&gt;If you want to delete pages, iterate over the pages in your source PDF and skip over the pages to be deleted as you write your new PDF. For example, if you want to get rid of blank pages in &lt;code&gt;source.pdf&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;PyPDF2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileWriter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;source.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;output&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileWriter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;xrange&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getNumPages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;p&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getContents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;():&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;# getContents is None if  page is blank&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;output&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;p&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;newfile.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;wb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
   &lt;span class=&#34;n&#34;&gt;output&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Since blank pages are not added to the output file, the result is that the new output file is a copy of the source but with no blank pages.&lt;/p&gt;
&lt;h3 id=&#34;split&#34;&gt;Split&lt;/h3&gt;
&lt;p&gt;You want to split one PDF into separate one-page PDFs. Create a new file for each page and write it to disk as you iterate through the source PDF.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;PyPDF2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileWriter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt; 
&lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;source.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;xrange&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getNumPages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;infile&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;outfile&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileWriter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;outfile&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;p&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;page-&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;%02d&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;.pdf&#39;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;%&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;wb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;outfile&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&#34;slices&#34;&gt;Slices&lt;/h3&gt;
&lt;p&gt;You can even operate on slices of a source PDF, where each item in the slice is a page.&lt;/p&gt;
&lt;p&gt;Say you have two PDFs resulting from scanning odd and even pages of a source document. You can merge them together, interleaving the pages as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;table class=&#34;highlighttable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;linenos&#34;&gt;&lt;div class=&#34;linenodiv&#34;&gt;&lt;pre&gt;&lt;span class=&#34;normal&#34;&gt; 1&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 2&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 3&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 4&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 5&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 6&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 7&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 8&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt; 9&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;10&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;11&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;12&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;13&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;14&lt;/span&gt;
&lt;span class=&#34;normal&#34;&gt;15&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#34;code&#34;&gt;&lt;div&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;PyPDF2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileWriter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt; 
&lt;span class=&#34;n&#34;&gt;even&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;even.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;odd&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileReader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;odd.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;all&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileWriter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;all&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addBlankPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;612&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;792&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;y&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;zip&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;odd&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;even&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;all&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;all&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;y&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;while&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;all&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getNumPages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;%&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;all&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addBlankPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;all.pdf&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;wb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;all&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The first &lt;code&gt;addBlankPage&lt;/code&gt; (line 5) insures that the output PDF begins with a blank page so that the first content page is on the right-hand side. &lt;br&gt;
Note that, when you add a blank page, the default page dimensions are set to the previous page. In this case, because there is no previous page, you must set the dimensions explicitly; 8.5 inches is 612 points, 11 inches is 792 points).
Alternatively, you might add a title page as the first page. In any case, putting the first content page on the right-hand side (odd numbered) is useful when your PDFs are laid out for a two-page (book-like) spread.&lt;/p&gt;
&lt;p&gt;The last &lt;code&gt;addBlankPage&lt;/code&gt; sequence (lines 11-12) insures there is an even number of pages in the final PDF. Like the first &lt;code&gt;addBlankPage&lt;/code&gt;, this is an optional step, but could be important depending on how the final PDF will be used (for example, a print shop will appreciate that your PDFs do not end on an odd page).&lt;/p&gt;
&lt;h3 id=&#34;metadata&#34;&gt;Metadata&lt;/h3&gt;
&lt;p&gt;You can set your own metadata in a PDF:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;PyPDF2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileMerger&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;add_metadata&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;merger&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PdfFileMerger&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;.pdf&#39;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;%&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;f0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;merger&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;f0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;merger&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addMetadata&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;_update.pdf&#39;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;%&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;wb&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;f1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;merger&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;f1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;metadata&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;sa&#34;&gt;u&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/hey&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;sa&#34;&gt;u&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;there&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;sa&#34;&gt;u&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/la&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;deedah&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;add_metadata&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;myfile&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;metadata&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The function &lt;code&gt;add_metadata&lt;/code&gt; opens the source PDF file and appends it to a new PdfFileMerger instance, resulting in a copy of the original PDF. Then it adds a special dictionary of key-value pairs into the new PDF metadata dictionary and writes the new PDF out to disk.  The dictionary keys and values are unicode strings; also, the &lt;code&gt;key&lt;/code&gt; string begins with a forward slash.&lt;/p&gt;
&lt;p&gt;Why would you want to add metadata to a PDF? One situation is when you are processing PDFs as part of a larger workflow in which each PDF file must be processed exactly once. In that case, when you open a file for processing, you first check for the custom metadata key; if it is not present, you add the key and continuing processing the file. If it is already present, the file has already been processed, so you skip it and continue with the next file.&lt;/p&gt;
&lt;h3 id=&#34;other-operations&#34;&gt;Other Operations&lt;/h3&gt;
&lt;p&gt;With the &lt;strong&gt;PyPDF2&lt;/strong&gt; package, you can also crop pages, add Javascript code, encrypt, rotate, scale, and transform pages. The documentation covers these uses and the sample code that comes with the package shows you how to do each operation.&lt;/p&gt;
&lt;p&gt;The distribution contains a subdirectory called &lt;code&gt;Sample_Code&lt;/code&gt; with several scripts that show you how. The script &lt;code&gt;pdfcat&lt;/code&gt; is particularly interesting as it enables you to concatenate specified pages from multiple PDF files into one single PDF. For example (from the &lt;code&gt;pdfcat&lt;/code&gt; documentation):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;pdfcat&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-o&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;output.pdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;head.pdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;content.pdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;:6&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;7&lt;/span&gt;:&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;tail.pdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-1
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;Concatenate&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;all&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;head.pdf,&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;all&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;but&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;page&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;seven&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;content.pdf,&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;and&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;the&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;last&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;page&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;of&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;tail.pdf,&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;producing&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;output.pdf.

pdfcat&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;chapter*.pdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&amp;gt;book.pdf
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;You&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;can&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;specify&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;the&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;output&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;file&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;by&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;redirection.

pdfcat&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;chapter?.pdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;chapter10.pdf&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&amp;gt;book.pdf
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;In&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;case&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;you&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;don&lt;span class=&#34;err&#34;&gt;&#39;&lt;/span&gt;t&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;want&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;chapter&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;before&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;chapter&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&#34;other-tools&#34;&gt;Other Tools&lt;/h2&gt;
&lt;p&gt;If you commonly deal with PDFs, you need a deep toolbox to turn to. In addition to the tools Python provides for manipulating PDFs, the following libraries, packages, and programs enable you to do other types of tasks.&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;reportlab&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Python package. Create PDF documents as well as vector and bitmap images. &lt;a href=&#34;http://www.reportlab.com/opensource/&#34;&gt;info&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;pdftk&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;GUI and command line. Merge, split PDF files, and more. &lt;a href=&#34;https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/&#34;&gt;info&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;fdfgen&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Python package. Generates an &lt;code&gt;FDF&lt;/code&gt; file containing form data that can be used with &lt;code&gt;pdftk&lt;/code&gt; to populate a PDF form. &lt;a href=&#34;https://github.com/ccnmtl/fdfgen/&#34;&gt;repo&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;qpdf&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;C++ library and program suite. Transforms PDF files. Useful for linearizing/optimizing uncompressing, and encryption. &lt;a href=&#34;http://qpdf.sourceforge.net/&#34;&gt;repo&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;ghostscript&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Interpreter for Postscript and PDF. &lt;a href=&#34;http://www.ghostscript.com/&#34;&gt;info&lt;/a&gt;. &lt;a href=&#34;http://www.ghostscript.com/doc/9.15/Use.htm&#34;&gt;Command-line info&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;XPDF&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Open source project. Contains several useful tools such as &lt;code&gt;pdffonts&lt;/code&gt; and &lt;code&gt;pdfinfo&lt;/code&gt;. &lt;a href=&#34;http://www.foolabs.com/xpdf/&#34;&gt;repo&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;pdffonts&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;lists fonts used in a PDF file including information on font type, whether the font is embedded, etc. Part of the open-source &lt;code&gt;Xpdf&lt;/code&gt; project. Licensed under GPL v2.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;pdffonts&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;filename.pdf
name&lt;span class=&#34;w&#34;&gt;                            &lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;          &lt;/span&gt;emb&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;sub&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;uni&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;object&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;ID
-------------------------------&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;-------------&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;---&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;---&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;---&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;---------
LFIGJB+ArialMT&lt;span class=&#34;w&#34;&gt;                  &lt;/span&gt;Type&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;1C&lt;span class=&#34;w&#34;&gt;       &lt;/span&gt;yes&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;yes&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;no&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;39&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;
LFIGLB+Arial-BoldMT&lt;span class=&#34;w&#34;&gt;             &lt;/span&gt;Type&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;1C&lt;span class=&#34;w&#34;&gt;       &lt;/span&gt;yes&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;yes&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;no&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;41&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;pdfinfo&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;extracts contents of &lt;code&gt;Info&lt;/code&gt; dictionary in a PDF file. Another part of the &lt;code&gt;Xpdf&lt;/code&gt; project.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;pdfinfo&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;filename.pdf
Title:&lt;span class=&#34;w&#34;&gt;          &lt;/span&gt;HILs.pdf
Subject:
Keywords:
Author:&lt;span class=&#34;w&#34;&gt;         &lt;/span&gt;
Creator:&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;Acrobat&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;PDFMaker&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;10&lt;/span&gt;.0&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Word
Producer:&lt;span class=&#34;w&#34;&gt;       &lt;/span&gt;Acrobat&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Distiller&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;9&lt;/span&gt;.3.0&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;Windows&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
CreationDate:&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;Mon&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Jun&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;11&lt;/span&gt;:16:53&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2014&lt;/span&gt;
ModDate:&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;Mon&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;Jun&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;11&lt;/span&gt;:16:53&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;2014&lt;/span&gt;
Tagged:&lt;span class=&#34;w&#34;&gt;         &lt;/span&gt;no
Pages:&lt;span class=&#34;w&#34;&gt;          &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;3&lt;/span&gt;
Encrypted:&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;no
Page&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;size:&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;612&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;x&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;792&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;pts&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;letter&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
File&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;size:&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;39177&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;bytes
Optimized:&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;yes
PDF&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;version:&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt;.5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;
&lt;p&gt;There is some overlap in capability between &lt;code&gt;PDFMiner&lt;/code&gt; and &lt;code&gt;PyPDF2&lt;/code&gt; and that means you have options in which one you want to use and learn. Both libraries are in active development and the developers are dedicated to providing good code. Browsing the source is good way to learn about both Python and PDF internals.&lt;/p&gt;
&lt;p&gt;There are some nasty PDFs out there. But there are several tools you can use to get what you need from them and Python enables you to get inside and scrape, split, merge, delete, and crop just about whatever you find.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>SAS and LaTeX Together: The StatRep Package</title>
    <link href="https://reachtim.com/articles/sas-latex-statrep.html" rel="alternate" />
    <id>https://reachtim.com/articles/sas-latex-statrep.html</id>
    <published>2014-11-23T00:00:00Z</published>
    <updated>2014-11-23T00:00:00Z</updated>
    <category term="LaTeX" />
    <summary type="text">How to use StatRep with SAS-generated LaTeX output for reproducible research.</summary>
    <content type="html">&lt;h4 class=&#34;article-title&#34; id=&#34;overview&#34;&gt;Overview&lt;/h4&gt;
&lt;p&gt;This short article shows how you can use &lt;strong&gt;StatRep&lt;/strong&gt; to capture and display SAS-generated output to create dynamic documents. With the most recent release of the &lt;strong&gt;StatRep&lt;/strong&gt; package, you can now create and display SAS-generated LaTeX output in your LaTeX documents. &lt;/p&gt;
&lt;p class=&#34;callout&#34;&gt;The &lt;strong&gt;StatRep&lt;/strong&gt; system for reproducible research was introduced at the 2012 SAS Global Forum, in this &lt;a href=&#34;http://support.sas.com/resources/papers/proceedings12/324-2012.pdf&#34;&gt;paper&lt;/a&gt;. You can download the package from the links given at the end of this article.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;StatRep&lt;/strong&gt; package enables you to create dynamic documents using SAS and LaTeX. It is composed of one LaTeX package (&lt;code&gt;statrep.sty&lt;/code&gt;) and a suite of SAS macros, (&lt;code&gt;statrep_macros.sas&lt;/code&gt;) that work together to capture and display SAS output. This enables you to write all of your data, code, and discussion in a single source file (your LaTeX document); that file can be shared with others and you can be sure that the output others see is generated from the code you used to create the analysis. No more copying and pasting of code blocks!&lt;/p&gt;
&lt;p&gt;Your document is processed in four steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Write your LaTeX document using custom markup from the &lt;strong&gt;StatRep&lt;/strong&gt; package.&lt;/li&gt;
&lt;li&gt;Compile the document with pdfLaTeX; this step generates a SAS program to capture the output needed in your document.&lt;/li&gt;
&lt;li&gt;Run that generated SAS program to produce the output.&lt;/li&gt;
&lt;li&gt;Recompile the document with pdfLaTeX; now your code and output appear in the final PDF document.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;configuration&#34;&gt;Configuration&lt;/h4&gt;
&lt;p&gt;There are several &amp;ldquo;hooks&amp;rdquo; in the &lt;strong&gt;StatRep&lt;/strong&gt; configuration that you can set in order to make the package work with your preferences. These hooks are explained in detail in the &lt;em&gt;User&amp;rsquo;s Guide&lt;/em&gt;; see the section on customization and the installation instructions. &lt;/p&gt;
&lt;p&gt;While the defaults have been carefully chosen, there are two settings that deserve some special consideration.&lt;/p&gt;
&lt;h5 class=&#34;article-title&#34; id=&#34;the-statrep_macrossas-file&#34;&gt;The &lt;code&gt;statrep_macros.sas&lt;/code&gt; File&lt;/h5&gt;
&lt;p&gt;The location of the file &lt;code&gt;statrep_macros.sas&lt;/code&gt; must be set so that the generated SAS program will function correctly. Set the location with the &lt;code&gt;\SRmacropath&lt;/code&gt; variable in your &lt;code&gt;statrep.cfg&lt;/code&gt; file. &lt;/p&gt;
&lt;p&gt;If you do not set this variable, SAS will expect the macros to be in the current directory (the one that contains the program and your LaTeX document). Nothing wrong with that, just make sure the file is in that directory when you run the generated SAS program.&lt;/p&gt;
&lt;h5 class=&#34;article-title&#34; id=&#34;the-sas-ods-output-destination&#34;&gt;The SAS ODS Output Destination&lt;/h5&gt;
&lt;p&gt;When you download and install the &lt;strong&gt;StatRep&lt;/strong&gt; package, the default ODS destination is the ODS Listing; (because the default value of the &lt;code&gt;\SRdefaultdests&lt;/code&gt; configuration variable is &lt;code&gt;listing&lt;/code&gt;). SAS-generated output is displayed as it appears in the listing window. You can alternatively have SAS create output as LaTeX tables.  There are two ways to select SAS-generated LaTeX output.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;You can modify the configuration in the &lt;code&gt;statrep.cfg&lt;/code&gt; file to make the default the ODS &lt;strong&gt;StatRep&lt;/strong&gt; LaTeX tagset by changing the value of &lt;code&gt;\SRdefaultdests&lt;/code&gt; to &lt;code&gt;latex&lt;/code&gt;. &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Alternatively, you can leave the &lt;code&gt;\SRdefaultdests&lt;/code&gt; configuration variable as it is and modify your &lt;code&gt;\Listing&lt;/code&gt; tag in your LaTeX document. Specify the ODS destination on the &lt;code&gt;\Listing&lt;/code&gt; tag itself, as in the example below, which sets the &lt;code&gt;dest=latex&lt;/code&gt; option. &lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p class=&#34;callout&#34;&gt;If the global default &lt;code&gt;\SRdefaultdests&lt;/code&gt; is set to &lt;code&gt;latex&lt;/code&gt;, there is no need to specify it again on the &lt;code&gt;\Listing&lt;/code&gt; tag.&lt;/p&gt;
&lt;p&gt;In either case, whether you change the global configuration variable or add the &lt;code&gt;dest=&lt;/code&gt; option to your &lt;code&gt;\Listing&lt;/code&gt; tags, SAS will need access to the &lt;strong&gt;StatRep&lt;/strong&gt; tagset in order to generate the output. &lt;/p&gt;
&lt;p&gt;Bundled in the &lt;strong&gt;StatRep&lt;/strong&gt; package is a SAS program that creates a new tagset base on the ODS LaTeX tagset. You create the tagset by running the included SAS program, &lt;code&gt;statrep_tagset.sas&lt;/code&gt;. &lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;the-statrep-tagset-and-style&#34;&gt;The StatRep Tagset and Style&lt;/h4&gt;
&lt;p&gt;If you want your output to be generated in LaTeX format rather than the listing format, you first must create a special tagset for SAS to use. This is easily done&amp;ndash;just run the included SAS program &lt;code&gt;statrep_tagset.sas&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;User&amp;rsquo;s Guide&lt;/em&gt; provides complete details on how to create SAS-generated LaTeX output. The main points are that you must create the &lt;code&gt;statrep.tagset&lt;/code&gt; with the included SAS program, &lt;code&gt;statrep_tagset.sas&lt;/code&gt;, and you must tell SAS to use the tagset when creating the output. You can run the tagset generation code each time you use &lt;strong&gt;StatRep&lt;/strong&gt; or you can save the tagset. By default, the tagset is saved to the SASUSER.TEMPLAT item store. Full details on installing tagsets are available in this &lt;a href=&#34;http://support.sas.com/kb/32/394.html&#34;&gt;Usage Note&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you want to run the tagset generation code each time, you can change the way &lt;strong&gt;StatRep&lt;/strong&gt; includes the macros. Your file &lt;code&gt;statrep.cfg&lt;/code&gt; will change so the definition for &lt;code&gt;\SRmacroinclude&lt;/code&gt; appears as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\SRmacroinclude&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;\@percentchar&lt;/span&gt; include &#34;statrep&lt;span class=&#34;nb&#34;&gt;_&lt;/span&gt;tagset.sas&#34; /nosource;
  &lt;span class=&#34;k&#34;&gt;\@percentchar&lt;/span&gt; include &#34;&lt;span class=&#34;k&#34;&gt;\SRmacropath&lt;/span&gt;&#34; /nosource;
&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With this definition, the generated SAS program first creates the &lt;strong&gt;StatRep&lt;/strong&gt; tagset before creating the output for your document. &lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;examples&#34;&gt;Examples&lt;/h4&gt;
&lt;p&gt;In the examples that follow, the following document preamble, data step and proc step are used. Make sure you have already created the &lt;strong&gt;StatRep&lt;/strong&gt; tagset, described above.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\documentclass&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;article&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\usepackage&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;[margin=1in]&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;geometry&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\usepackage&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;statrep&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\begin&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;document&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;

The SAS datastep and analysis ...

&lt;span class=&#34;k&#34;&gt;\begin&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;Datastep&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
data time;
   input time @@;
   datalines;
 43  90  84  87  116   95  86   99   93  92
121  71  66  98   79  102  60  112  105  98
;
&lt;span class=&#34;k&#34;&gt;\end&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;Datastep&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;

... discussion of the analysis ...

&lt;span class=&#34;k&#34;&gt;\begin&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;Sascode&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;[store=myoutput]
proc ttest h0=80 plots(showh0) sides=u alpha=0.1;
   var time;
run;
&lt;span class=&#34;k&#34;&gt;\end&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;Sascode&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;

... discussion of the results ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The final PDF output from the preceding code looks like this:
&lt;img alt=&#34;pdfpage&#34; class=&#34;center_border&#34; src=&#34;/static/img/blog/saslatex1.png&#34;&gt;&lt;/p&gt;
&lt;p&gt;In the document snippet above, the code creates a SAS data set &lt;code&gt;time&lt;/code&gt; and invokes the TTEST procedure.  The result is that all output created by the TTEST procedure is contained in an ODS document named &lt;code&gt;myoutput&lt;/code&gt; as specified by the option &lt;code&gt;store=myoutput&lt;/code&gt;. &lt;/p&gt;
&lt;p class=&#34;callout&#34;&gt;So far, no output is displayed&amp;ndash;the output that is displayed is determined by the &lt;code&gt;\Listing&lt;/code&gt; and &lt;code&gt;\Graphic&lt;/code&gt; tags that follow. This is how &lt;strong&gt;StatRep&lt;/strong&gt; works: the code block generates all the output at one time and stores it in the ODS document so it can be used at any point in the document.&lt;/p&gt;
&lt;p&gt;In the sections that follow, the &lt;code&gt;\Listing&lt;/code&gt; and &lt;code&gt;\Graphic&lt;/code&gt; tags request the ODS document to replay output objects into external files (on disk). When the final document is recompiled, pdfLaTeX embeds and displays those outputs.&lt;/p&gt;
&lt;h5 class=&#34;article-title&#34; id=&#34;capture-all-outputs-as-a-single-stream&#34;&gt;Capture All Outputs as a Single Stream&lt;/h5&gt;
&lt;p&gt;In this example, the output tags make no request for specific outputs. When you make no explicit output selection, the &lt;code&gt;Listing&lt;/code&gt; tag selects all tables and notes. &lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\Listing&lt;/span&gt;[store=myoutput, dest=latex, 
         caption=&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;Court Case Duration Analysis&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;]&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;myouta&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
text about table
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;\Listing&lt;/code&gt; tag specifies:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;that output should come from the ODS document &lt;code&gt;myoutput&lt;/code&gt; which matches the &lt;code&gt;store=&lt;/code&gt; option on the &lt;code&gt;Sascode&lt;/code&gt; block&lt;/li&gt;
&lt;li&gt;the output destination is &lt;code&gt;latex&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;caption for the output&lt;/li&gt;
&lt;li&gt;filename that contains the output is &lt;code&gt;lst/myouta.lst&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The output from the above code looks like this:
&lt;img alt=&#34;pdfpage&#34; class=&#34;center_border&#34; src=&#34;/static/img/blog/saslatex2.png&#34;&gt;&lt;/p&gt;
&lt;p&gt;When you run the generated SAS program, the SAS Log displays the ODS objects that are selected for replay. These outputs are displayed in your document as a continuous stream of output with page breaks handled automatically. That is, the output can go over a page break, with breaks allowed only between ODS objects. The SAS Log displays the following table that corresponds to the &lt;code&gt;\Listing&lt;/code&gt; tag:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Objects                   Type  Status   Group

Ttest.time.PT             Note  Selected   1
Ttest.time.Statistics     Table Selected   1
Ttest.time.ConfLimits     Table Selected   2
Ttest.time.TTests         Table Selected   3
Ttest.time.SummaryPanel   Graph            .
Ttest.time.QQPlot         Graph            .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next, add some graphical output to your document with the &lt;code&gt;\Graphic&lt;/code&gt; tag&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\Graphic&lt;/span&gt;[store=myoutput, 
         caption=&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;Court Case Duration&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;]&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;myoutb&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;\Graphic&lt;/code&gt; tag specifies&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;that output should come from the ODS document &lt;code&gt;myoutput&lt;/code&gt; which matches the &lt;code&gt;store=&lt;/code&gt; option on the &lt;code&gt;Sascode&lt;/code&gt; block&lt;/li&gt;
&lt;li&gt;caption for the output&lt;/li&gt;
&lt;li&gt;filename that contains the output is &lt;code&gt;png/myoutb.png&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;Graphic&lt;/code&gt; tag selects all graphs by default. The SAS Log displays the following table that corresponds to the &lt;code&gt;\Graphic&lt;/code&gt; tag:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Objects                   Type  Status   Group

Ttest.time.PT             Note             .
Ttest.time.Statistics     Table            .
Ttest.time.ConfLimits     Table            .
Ttest.time.TTests         Table            .
Ttest.time.SummaryPanel   Graph Selected   1
Ttest.time.QQPlot         Graph Selected   2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The final PDF document, which specifies the &lt;code&gt;latex&lt;/code&gt; destination and uses the &lt;strong&gt;StatRep&lt;/strong&gt; tagset, looks like this:&lt;/p&gt;
&lt;p&gt;&lt;img alt=&#34;pdfpage&#34; class=&#34;border&#34; src=&#34;/static/img/blog/saslatex3a.png&#34;&gt;&lt;img alt=&#34;pdfpage&#34; class=&#34;border&#34; src=&#34;/static/img/blog/saslatex3b.png&#34;&gt;
&lt;img alt=&#34;pdfpage&#34; class=&#34;border&#34; src=&#34;/static/img/blog/saslatex3c.png&#34;&gt;&lt;/p&gt;
&lt;p&gt;For comparison, the following screenshots show the final PDF document if you use the default &lt;code&gt;listing&lt;/code&gt; destination. Only the tabular output has changed; the graphs are identical.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&#34;pdfpage&#34; class=&#34;center_border&#34; src=&#34;/static/img/blog/saslatex4a.png&#34;&gt;&lt;/p&gt;
&lt;h5 class=&#34;article-title&#34; id=&#34;capture-specific-outputs&#34;&gt;Capture Specific Outputs&lt;/h5&gt;
&lt;p&gt;You don&amp;rsquo;t always want the output to displayed as a continuous, uninterrupted stream, so &lt;strong&gt;StatRep&lt;/strong&gt; provides a flexible way to select ODS objects. In the following &lt;code&gt;\Listing&lt;/code&gt; tag, the &lt;code&gt;firstobj=&lt;/code&gt; option specifies that the output stream should begin with the &lt;code&gt;ConfLimits&lt;/code&gt; object; the &lt;code&gt;lastobj=&lt;/code&gt; option specifies that the output stream should end with the &lt;code&gt;TTests&lt;/code&gt; object. &lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\Listing&lt;/span&gt;[store=myoutput, dest=latex, 
         firstobj=conflimits, lastobj=ttests,
         caption=&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;Court Case Duration Analysis&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;]&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;myoutc&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Suppose there were several objects in the ODS document between those two specified objects; they would all be included with this one &lt;code&gt;\Listing&lt;/code&gt; tag.
&lt;strong&gt;StatRep&lt;/strong&gt; provides many other methods of ODS object selection; see the &lt;em&gt;User&amp;rsquo;s Guide&lt;/em&gt; for details.&lt;/p&gt;
&lt;p class=&#34;callout&#34;&gt;By specifying the same ODS document in different &lt;code&gt;\Listing&lt;/code&gt; tags, you can show the outputs in any order or break them up into parts so you can discuss particular outputs. Your SAS code is only run one time&amp;ndash;when the ODS document is created. You can replay the output from that ODS document in any order you like.&lt;/p&gt;
&lt;p&gt;When you run the generated SAS program, the resulting SAS Log shows that the output objects &lt;code&gt;ConfLimits&lt;/code&gt; and &lt;code&gt;TTests&lt;/code&gt; are selected.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Objects                   Type  Status   Group

Ttest.time.PT             Note             .
Ttest.time.Statistics     Table            .
Ttest.time.ConfLimits     Table Selected   1
Ttest.time.TTests         Table Selected   2
Ttest.time.SummaryPanel   Graph            .
Ttest.time.QQPlot         Graph            .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;\Graphic&lt;/code&gt; tag uses the &lt;code&gt;objects=&lt;/code&gt; option to specify that a single graph is to be captured and displayed, the &lt;code&gt;qqplot&lt;/code&gt; object. Typically, the &lt;code&gt;objects=&lt;/code&gt; option contains a space-separated list of ODS objects; in this case however, only one plot is selected.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt; &lt;span class=&#34;k&#34;&gt;\Graphic&lt;/span&gt;[store=myoutput, 
          objects=qqplot,
          caption=&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;Court Case Duration&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;]&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;myoutd&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The log shows that only the &lt;code&gt;QQPlot&lt;/code&gt; object is selected.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Objects                   Type  Status   Group

Ttest.time.PT             Note             .
Ttest.time.Statistics     Table            .
Ttest.time.ConfLimits     Table            .
Ttest.time.TTests         Table            .
Ttest.time.SummaryPanel   Graph            .
Ttest.time.QQPlot         Graph Selected   1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4 class=&#34;article-title&#34; id=&#34;summary&#34;&gt;Summary&lt;/h4&gt;
&lt;p&gt;This article provides a couple of simple examples that show you how to get started with &lt;strong&gt;StatRep&lt;/strong&gt; and generate LaTeX output with SAS. The &lt;em&gt;User&amp;rsquo;s Guide&lt;/em&gt; also provides simple examples, but includes more in-depth, advanced usage.&lt;/p&gt;
&lt;p&gt;You can download the package from:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://support.sas.com/rnd/app/papers/statrep.html&#34;&gt;support.SAS.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.ctan.org/tex-archive/macros/latex/contrib/statrep&#34;&gt;CTAN.org&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Do you have an example you&amp;rsquo;d like to see or a question about &lt;strong&gt;StatRep&lt;/strong&gt;? Leave a comment!&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Create Keywords Metadata From Index Terms</title>
    <link href="https://reachtim.com/articles/keywords-metadata-index-terms.html" rel="alternate" />
    <id>https://reachtim.com/articles/keywords-metadata-index-terms.html</id>
    <published>2014-10-03T00:00:00Z</published>
    <updated>2014-10-03T00:00:00Z</updated>
    <category term="XML" />
    <summary type="text">How to harvest index terms in DocBook XML to create keywords for HTML.</summary>
    <content type="html">&lt;p&gt;In an HTML page, the keywords can be listed in the metadata. An example might look like this&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;meta&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;keywords&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;content&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;DocBook, XML, XSLT, index terms&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This metadata is hardly ever used these days, but at one time, those words may have helped search engines find the page. However, it doesn&amp;rsquo;t work that way anymore. For the past several years, most search engines don&amp;rsquo;t use the metadata and most people never use them. In some situations they can still be useful, say if you are generating pages for a corporation using a Google Search Appliance that has been explicitly configured to include the keywords meta tag.&lt;/p&gt;
&lt;p&gt;If you are in such a situation where keyword metadata is actually used and you are converting from DocBook XML to HTML, this article can help. It shows how, using XSLT1.0, you can harvest the DocBook primary index terms and use them as keyword metadata in the HTML generated by the DocBook XSL stylesheets.&lt;/p&gt;
&lt;p&gt;We start by adding a stylesheet to our DocBook customization layer. You can add it to your &lt;code&gt;user.head.content&lt;/code&gt; template or create one from scratch. That template is automatically called from the DocBook stylesheet as a hook so you can add your own content in the &lt;code&gt;head&lt;/code&gt; element in the output HTML files. You can find the details in the excellent &lt;a href=&#34;http://www.sagehill.net/docbookxsl/HTMLHeaders.html&#34;&gt;DocBook XSL: The Complete Guide&lt;/a&gt;. Here&amp;rsquo;s an example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;cp&#34;&gt;&amp;lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&amp;gt;&lt;/span&gt;
&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:stylesheet&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;xmlns:xsl=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;http://www.w3.org/1999/XSL/Transform&#34;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;xmlns:exsl=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;http://exslt.org/common&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;extension-element-prefixes=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;exsl&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;version=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;1.0&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:template&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;user.head.content&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:call-template&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;keywordset&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;head&lt;/code&gt; element will have content from the following template named &lt;code&gt;keywordset&lt;/code&gt;. First, define a variable &lt;code&gt;section_level&lt;/code&gt; that describes the depth of the current section; we only want keywords that are actually in the HTML page, not necessarily all the terms in the section. That depends on the chunking depth, so we need to know how deep we are.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:template&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;keywordset&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:variable&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;section_level&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:number&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;value=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;count(ancestor-or-self::d:section)&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:variable&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the following code block, if we are at the start of a part or a book, the template does nothing. If we are not on a part or book, and we have index terms, create a new variable called &lt;code&gt;indexterms&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If we&amp;rsquo;re at the top level (a chapter), do nothing. The only thing that gets created at that level is a table of contents so we don&amp;rsquo;t want any keywords there.&lt;/p&gt;
&lt;p&gt;If we&amp;rsquo;re not at the bottom (that is, the current section level is less than the level we are chunking to, as set in the parameter &lt;code&gt;chunk.section.depth&lt;/code&gt;), get all index primary terms that are immediate children and all index primary terms that are children of non-section elements (for example, index terms inside itemized lists). The main issue is that we do not want to gather the terms inside subsections&amp;ndash;they will be on a different HTML page because of the chunking depth.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:if&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;test=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;not(self::part) and not(self::book) and .//d:indexterm&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:variable&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;indexterms&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:choose&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:when&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;test=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;$section_level = 0&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:when&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;test=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;$section_level &amp;amp;lt; $chunk.section.depth&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;            &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:copy-of&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;select=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;./d:indexterm/d:primary|./*[not(self::d:section)]//d:indexterm/d:primary&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:when&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Otherwise, we are in a section at a depth greater than or equal to the chunking depth. So get &lt;em&gt;all&lt;/em&gt; of the descendant primary index terms.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:otherwise&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:copy-of&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;select=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;.//d:indexterm/d:primary&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:otherwise&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:choose&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:variable&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So now we have a node-set (we used &lt;code&gt;copy-of&lt;/code&gt;) of all the index terms that are germane to the current chunked section and that set is contained in the variable &lt;code&gt;indexterms&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;Of course it&amp;rsquo;s likely we have duplicate terms, so let&amp;rsquo;s get all the terms that are unique and put them into the variable &lt;code&gt;indexterms-unique&lt;/code&gt;. The following technique is one of many recipes that you might find useful in &lt;a href=&#34;http://shop.oreilly.com/product/9780596009748.do&#34;&gt;XSLT CookBook&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Use &lt;code&gt;xsl:for-each&lt;/code&gt; to loop through the primary terms we&amp;rsquo;ve just gathered, and filter them so that we skip any that are already in the new unique list. That is, skip the term if it matches a &lt;code&gt;preceding-sibling&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;Then we use &lt;code&gt;normalize-space&lt;/code&gt; to get the string value of the element, and append a comma (&lt;code&gt;,&lt;/code&gt;) if this term isn&amp;rsquo;t the last one of the list.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:variable&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;indexterms-unique&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:for-each&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;select=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;exsl:node-set($indexterms)/*[not(. = preceding-sibling::*)]&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:value-of&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;select=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;normalize-space(.)&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:if&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;test=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;not(position() =  last())&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;,&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:if&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:for-each&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:variable&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally, insert the unique, comma-separated strings into the &lt;code&gt;meta&lt;/code&gt; tag as the &lt;code&gt;content&lt;/code&gt; attribute string.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;  :::xml
  &amp;lt;meta name=&#34;keywords&#34;&amp;gt;
  &amp;lt;xsl:attribute name=&#34;content&#34;&amp;gt;
    &amp;lt;xsl:value-of select=&#34;$indexterms-unique&#34; /&amp;gt;
  &amp;lt;/xsl:attribute&amp;gt;
&amp;lt;/meta&amp;gt;
&amp;lt;/xsl:if&amp;gt;
&amp;lt;/xsl:template&amp;gt;
&amp;lt;/xsl:stylesheet&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;While keyword metadata isn&amp;rsquo;t useful for general search engines, you still run into situations where it is used. Harvesting index primary terms to use as keywords populate that metadata with exactly what the information needed.&lt;/p&gt;
&lt;p&gt;For convenience, here is the stylesheet in one block:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;cp&#34;&gt;&amp;lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&amp;gt;&lt;/span&gt;
&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:stylesheet&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;xmlns:xsl=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;http://www.w3.org/1999/XSL/Transform&#34;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;xmlns:exsl=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;http://exslt.org/common&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;extension-element-prefixes=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;exsl&#34;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;version=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;1.0&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:template&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;user.head.content&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:call-template&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;keywordset&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:template&amp;gt;&lt;/span&gt;

&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:template&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;keywordset&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:variable&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;section_level&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:number&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;value=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;count(ancestor-or-self::d:section)&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:variable&amp;gt;&lt;/span&gt;

&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:if&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;test=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;not(self::part) and not(self::book) and .//d:indexterm&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:variable&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;indexterms&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:choose&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:when&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;test=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;$section_level = 0&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:when&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;test=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;$section_level &amp;amp;lt; $chunk.section.depth&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;          &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:copy-of&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;select=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;./d:indexterm/d:primary|./*[not(self::d:section)]//d:indexterm/d:primary&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:when&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:otherwise&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;          &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:copy-of&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;select=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;.//d:indexterm/d:primary&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:otherwise&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:choose&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:variable&amp;gt;&lt;/span&gt;

&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:variable&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;indexterms-unique&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:for-each&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;select=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;exsl:node-set($indexterms)/*[not(. = preceding-sibling::*)]&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:value-of&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;select=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;normalize-space(.)&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:if&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;test=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;not(position() =  last())&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;,&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:if&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:for-each&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:variable&amp;gt;&lt;/span&gt;

&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;meta&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;keywords&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:attribute&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;name=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;content&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;xsl:value-of&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;na&#34;&gt;select=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;$indexterms-unique&#34;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/meta&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:if&amp;gt;&lt;/span&gt;

&lt;span class=&#34;nt&#34;&gt;&amp;lt;/xsl:template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/xsl:stylesheet&gt;&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>BAM! A Web Framework &#34;Short Stack&#34;</title>
    <link href="https://reachtim.com/articles/bam-web-framework.html" rel="alternate" />
    <id>https://reachtim.com/articles/bam-web-framework.html</id>
    <published>2014-09-29T00:00:00Z</published>
    <updated>2014-09-29T00:00:00Z</updated>
    <category term="Python" />
    <summary type="text">Use BAM (Bottle, Apache, and MongoDB) to create a quick website.</summary>
    <content type="html">&lt;p&gt;This how-to article describes how you can use Bottle, Apache, and MongoDB to create a simple, understandable, and fast website. In real life applications that support CRUD operations, it will most likely also use JavaScript, but BAMJ doesn&amp;rsquo;t sound as cool. If you want to play with the code in this example, it is available on &lt;a href=&#34;https://github.com/tiarno/bam_example&#34;&gt;GitHub&lt;/a&gt;. The example is kept very simple to show the general workflow. &lt;/p&gt;
&lt;p&gt;Here are the tools:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://bottlepy.org/docs/dev/index.html&#34;&gt;Bottle&lt;/a&gt; Fast WSGI micro web-framework for Python&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://httpd.apache.org/&#34;&gt;Apache Web Server&lt;/a&gt; Popular, secure, open-source HTTP server&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.mongodb.com/mongodb-overview&#34;&gt;MongoDB&lt;/a&gt; Leading open-source NOSQL database (BSON document based)&lt;/li&gt;
&lt;/ul&gt;
&lt;p class=&#34;callout&#34;&gt;&lt;span class=&#34;note&#34;&gt;Note: &lt;/span&gt;
Be warned if you want to use a NFS file system to house your web files, logs, or database. NFS is nice if you need to share directories with clients over a network. However, file locking is slow and that can cause problems if a program finds it cannot get a lock. It is not recommended to use NFS for logging or for MongoDB.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;overview&#34;&gt;Overview&lt;/h4&gt;
&lt;p&gt;This is what we&amp;rsquo;re going to do:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a simple MongoDB database.&lt;/li&gt;
&lt;li&gt;Set up the Bottle web framework.&lt;/li&gt;
&lt;li&gt;Set up Apache and WSGI to enable Bottle applications.&lt;/li&gt;
&lt;li&gt;Create a Bottle application that can connect (using GET/POST) to the MongoDB database.&lt;/li&gt;
&lt;li&gt;Create HTML to view the data. With some final notes on how to handle CRUD operations on the data.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I&amp;rsquo;m assuming you have installed Bottle, the Apache HTTP server with &lt;code&gt;mod_wsgi&lt;/code&gt;, and  MongoDB.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;http://bottlepy.org/docs/dev/index.html&#34;&gt;Bottle&lt;/a&gt; 
     This is the smallest and  simplest Python web framework I have found. 
     When I first started out, I used Django, which supports just about everything you could ever want in a framework. I found that it was great to get a lot accomplished in a short time; it is called &lt;em&gt;the web framework for perfectionists with deadlines&lt;/em&gt; for a reason. But I always felt uneasy about using it because I never fully understood what was going on under the covers.&lt;/p&gt;
&lt;p&gt;Bottle is on the other end of the spectrum.  While I have to program the stuff I need, that gives me confidence because there&amp;rsquo;s no magic. If it doesn&amp;rsquo;t work, it&amp;rsquo;s my code that broke it and I know where to look.  On the other hand, my needs are simple. You might find this &lt;a href=&#34;http://stackoverflow.com/questions/4941145/python-flask-vs-bottle&#34;&gt;question&lt;/a&gt; on &lt;code&gt;stackoverflow&lt;/code&gt; helpful if you&amp;rsquo;re trying to decide on a web framework.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;http://httpd.apache.org/&#34;&gt;Apache&lt;/a&gt; Over time Apache has lost some of its cool factor I guess, and Node/Express, Lighttpd, and Nginx, etc. are hot now. But Apache is a hardy, stable, and very popular tool. It is easy to install and not too hard to configure. You also need to follow the &lt;code&gt;modwsgi&lt;/code&gt; installation &lt;a href=&#34;https://code.google.com/p/modwsgi/wiki/QuickInstallationGuide&#34;&gt;guide&lt;/a&gt; since that is the bridge between Apache and Bottle.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;http://www.mongodb.org/&#34;&gt;MongoDB&lt;/a&gt; I really like the JSON-like structure of MongoDB documents (records). For my own needs (configuration database, build reports, and system monitoring) that structure fits much better than an SQL database. &lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;create-the-database&#34;&gt;Create the Database&lt;/h4&gt;
&lt;p&gt;First, create a toy &lt;code&gt;people&lt;/code&gt; database in the mongo shell. We&amp;rsquo;ll use a collection called &lt;code&gt;test&lt;/code&gt;. Here is what it looks like from a terminal window:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;mongo&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;servername&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;MongoDB&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;shell&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;version&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;2.4.9&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;connecting&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;servername&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;/&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;test&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;rs1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;PRIMARY&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;people&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;Alphonse&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;15&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;height&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;67&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;weight&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;116.5&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;Janice&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;12&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;height&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;54.3&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;weight&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;60.5&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;Sam&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;12&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;height&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;64.3&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;weight&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;123.25&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;Ursula&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;14&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;height&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;62.8&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;weight&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;104.0&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;

&lt;span class=&#34;nx&#34;&gt;rs1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;PRIMARY&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;people&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;insert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;people&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;rs1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;PRIMARY&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;people&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;find&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;_id&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ObjectId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;...&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;Alphonse&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;age&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;15&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;height&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;67&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;weight&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;116.5&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;_id&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ObjectId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;...&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;Janice&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;age&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;12&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;height&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;54.3&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;weight&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;60.5&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;_id&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ObjectId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;...&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;Sam&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;age&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;12&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;height&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;64.3&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;weight&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;123.25&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;_id&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ObjectId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;...&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;Ursula&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;age&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;14&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;height&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;62.8&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;weight&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;104.0&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The data is a set of height and weight measurements on children of various ages. That&amp;rsquo;s the data we&amp;rsquo;ll connect to via Bottle/Apache.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;bottle-directory-structure&#34;&gt;Bottle Directory Structure&lt;/h4&gt;
&lt;p&gt;In &lt;code&gt;/var/www/bottle&lt;/code&gt;, the directory structure looks like this. This layout makes it easy to add new applications as you need them. We have the main &lt;code&gt;wsgi&lt;/code&gt; script (&lt;code&gt;bottle_adapter.wsgi&lt;/code&gt;), and a single application, &lt;code&gt;people&lt;/code&gt;. For every application you write, you may also have a &lt;code&gt;css&lt;/code&gt; file, a &lt;code&gt;javascript&lt;/code&gt; file and templates in the &lt;code&gt;views&lt;/code&gt; subdirectory.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;bottle_adapter.wsgi
people.py

css/
    person_form.css

js/
    person.js

views/
    people/
        people.tpl
        person.tpl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To add a new application:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;create the application code file in the root directory, like &lt;code&gt;people.py&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;mount the application in the &lt;code&gt;bottle_adapter.wsgi&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;add the template path in the &lt;code&gt;bottle_adapter.wsgi&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;add &lt;code&gt;css&lt;/code&gt;, &lt;code&gt;js&lt;/code&gt; files if needed&lt;/li&gt;
&lt;li&gt;add a &lt;code&gt;view&lt;/code&gt; subdirectory to contain the application templates. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In this example we won&amp;rsquo;t use any javascript or even css, but in real life you will probably want to add that in. This directory layout keeps things simple and separated, and it&amp;rsquo;s easy to extend.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;apache-configuration&#34;&gt;Apache Configuration&lt;/h4&gt;
&lt;p&gt;In the Apache &lt;code&gt;httpd.conf&lt;/code&gt; file, set the &lt;code&gt;WSGIScriptAlias&lt;/code&gt; and &lt;code&gt;WSGIDaemonProcess&lt;/code&gt;. 
For details on &lt;code&gt;wsgi&lt;/code&gt; configuration, see the &lt;a href=&#34;https://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide&#34;&gt;guide&lt;/a&gt;.
When a request comes in to our server with a url that begins with &lt;code&gt;service&lt;/code&gt;, the request is passed on to our bottle application.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;LoadModule&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;wsgi_module&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;/usr/lib/apache2/modules/mod_wsgi.so
WSGIScriptAlias&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;/service&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;/var/www/bottle/bottle_adapter.wsgi
WSGIDaemonProcess&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;example02&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;processes&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;m&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;threads&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;m&#34;&gt;25&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You may need to set other parameters depending on your platform. I am on a FreeBSD machine and needed to set &lt;code&gt;WSGIAcceptMutex posixsem&lt;/code&gt; in addition to the settings above. The &lt;code&gt;wsgi&lt;/code&gt; guide is well written and indispensable.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;the-wsgi-adapter&#34;&gt;The &lt;code&gt;wsgi&lt;/code&gt; Adapter&lt;/h4&gt;
&lt;p&gt;When the Apache server gets a request that begins with &lt;code&gt;service&lt;/code&gt;, the &lt;code&gt;httpd&lt;/code&gt; daemon calls the bottle application adapter script, &lt;code&gt;bottle_adapter.wsgi&lt;/code&gt;. As you can see below, the script imports the application code, adds to the template path and &amp;ldquo;mounts&amp;rdquo; the application on a specific URL path. &lt;/p&gt;
&lt;p&gt;Now that the &lt;code&gt;people&lt;/code&gt; application is mounted on the URL path &lt;code&gt;/people&lt;/code&gt;, the application will get called for any URL that starts with &lt;code&gt;/service/people&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;The debug attribute is set to &lt;code&gt;True&lt;/code&gt; until we&amp;rsquo;re ready to go production. Bottle provides its own development server when you&amp;rsquo;re getting started; I&amp;rsquo;ve commented out that line but it is helpful to use it when debugging your application code.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;sys&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;bottle&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;sys&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;path&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/var/www/bottle&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;people&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;bottle&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;TEMPLATE_PATH&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;/var/www/bottle/views/people&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;application&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;bottle&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;default_app&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;application&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mount&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/people&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;people&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;app&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;bottle&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;debug&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;True&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;#bottle.run(host=&#39;example02&#39;, port=8090, debug=True)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4 class=&#34;article-title&#34; id=&#34;the-skeleton-of-an-app&#34;&gt;The Skeleton of an App&lt;/h4&gt;
&lt;p&gt;Every application will have a similar skeleton; all my apps have the same things at the beginning. The following code block displays the beginning of  the &lt;code&gt;people&lt;/code&gt; application. First, we make the necessary imports, get a connection to the MongoDB database &lt;code&gt;test&lt;/code&gt; collection and instantiate our application, &lt;code&gt;app&lt;/code&gt;, which is known as &lt;code&gt;people.app&lt;/code&gt; to the &lt;code&gt;wsgi&lt;/code&gt; adapter.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;bottle&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bottle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;view&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;request&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;bson.objectid&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ObjectId&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pymongo&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;conn&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pymongo&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MongoReplicaSetClient&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
    &lt;span class=&#34;s1&#34;&gt;&#39;example02.unx.sas.com, example01.unx.sas.com&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;replicaSet&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;rs1&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;db&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;conn&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;test&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;app&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bottle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And there we have it&amp;ndash;our &lt;code&gt;people.app&lt;/code&gt; Bottle application, all wired up but not able to do anything yet. Let&amp;rsquo;s add some abilities so we can respond to URL requests. For example, when a &lt;code&gt;GET&lt;/code&gt; request comes in on &lt;code&gt;service/people&lt;/code&gt;, we&amp;rsquo;ll send back a response with a list of the people in the database. &lt;/p&gt;
&lt;p&gt;Most capabilities you code will have three parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a URL route that matches and responds to an HTPP request&lt;/li&gt;
&lt;li&gt;a view (template) to render as the response&lt;/li&gt;
&lt;li&gt;a function to get the data that the view expects.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the following code block, the &lt;code&gt;@app.get&lt;/code&gt; decorator corresponds to a URL &lt;em&gt;route&lt;/em&gt;; it responds to a &lt;code&gt;GET&lt;/code&gt; request with no further arguments (&lt;code&gt;&#39;/&#39;&lt;/code&gt;); however, by the time the request makes it to this point, the URL is actually &lt;code&gt;service/people&lt;/code&gt;; that is what the root URL looks like to the &lt;code&gt;people&lt;/code&gt; app.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;@view&lt;/code&gt; decorator specifies the template to use to display the data in the response (that is, the template &lt;code&gt;views/people/people.tpl&lt;/code&gt; ) &lt;/p&gt;
&lt;p&gt;The &lt;code&gt;people&lt;/code&gt; function creates a cursor which gets all the records (&lt;em&gt;documents&lt;/em&gt; in the MongoDB world) from the database, sorts those records on the &lt;code&gt;name&lt;/code&gt; and returns them as a list under the key named &lt;code&gt;results&lt;/code&gt;. &lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;nd&#34;&gt;@app&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;nd&#34;&gt;@view&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;people&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;people&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;():&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;people&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;find&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sort&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;name&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)])&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;results&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;p&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&#34;callout&#34;&gt;&lt;span class=&#34;note&#34;&gt;Note: &lt;/span&gt;
The &lt;code&gt;sort&lt;/code&gt; function in &lt;code&gt;pymongo&lt;/code&gt; operates differently from the &lt;code&gt;mongo&lt;/code&gt; shell; it uses a list of tuples to sort on instead of a document. This tripped me up at first. The &lt;a href=&#34;http://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.sort&#34;&gt;documentation&lt;/a&gt; explains it well.&lt;/p&gt;
&lt;p&gt;Next, here is a route using the &lt;code&gt;@app.get&lt;/code&gt; decorator which responds to a GET request of the form &lt;code&gt;service/people/somename&lt;/code&gt;. It executes the &lt;code&gt;person&lt;/code&gt; function and renders the resulting data using the &lt;code&gt;person&lt;/code&gt; template (&lt;code&gt;views/people/person.tpl&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;person&lt;/code&gt; function gets the single (or first) document with a matching &lt;code&gt;name&lt;/code&gt;, converts the &lt;code&gt;_id&lt;/code&gt; value to a string and returns the data under the key named &lt;code&gt;person&lt;/code&gt;. &lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;@app.get(&#39;/&amp;lt;name&amp;gt;&#39;)
@view(&#39;person&#39;)
def person(name):
    person = db[&#39;people&#39;].find_one({&#39;name&#39;:name})
    person[&#39;_id&#39;] = str(person[&#39;_id&#39;])
    return {&#39;person&#39;: person}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Why convert the &lt;code&gt;_id&lt;/code&gt; from an ObjectID (as it exists in the MongDB database) to a string? If all you want to do is display the record, you could just delete the key, but if you want to interact with the record (possibly updating or deleting it through the client), you have to keep track of that &lt;code&gt;_id&lt;/code&gt;. Otherwise there&amp;rsquo;s no way to map back to the record in the database.  So we transform it to a string on the way out to the client and as you&amp;rsquo;ll see in the next route function, we transform it back to an ObjectID on the way back in to the database.&lt;/p&gt;
&lt;p&gt;The last route in this example uses the &lt;code&gt;app.post&lt;/code&gt; decorator to respond to a POST request. It creates a new dictionary populated with values from the request, converts the string &lt;code&gt;_id&lt;/code&gt; value back to an ObjectID, and saves the record back to the database. It returns a bare string (&amp;ldquo;Thanks&amp;hellip;&amp;rdquo;) so the user knows the data was updated.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;@app.post(&#39;/&amp;lt;name&amp;gt;&#39;)
def update_person(name):
    person = {&#39;name&#39;: name}
    person[&#39;age&#39;] = request.POST.get(&#39;age&#39;)
    person[&#39;weight&#39;] = request.POST.get(&#39;weight&#39;)
    person[&#39;height&#39;] = request.POST.get(&#39;height&#39;)
    person[&#39;_id&#39;] = ObjectId(request.POST.get(&#39;_id&#39;))

    db[&#39;people&#39;].save(person)
    return &#39;Thanks for your data.&#39;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&#34;callout&#34;&gt;Keep in mind that this is just a simple toy example; if you have a bazillion records or a gazillion requests per second, you&amp;rsquo;ll want to do a lot of reading on MongoDB index creation, aggregation and filtering techniques.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;summary-what-we-have-so-far&#34;&gt;Summary: What We Have So Far&lt;/h4&gt;
&lt;p&gt;When a GET request comes in with this url:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;http://example01/service/people
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;that url is routed from Apache to the Bottle &lt;code&gt;wsgi&lt;/code&gt; script and on to the &lt;code&gt;people.app&lt;/code&gt; Bottle application and finally to the &lt;code&gt;/people&lt;/code&gt; route. That route, in turn, invokes the &lt;code&gt;people&lt;/code&gt; function, which retrieves all the MongoDB documents in the &lt;code&gt;people&lt;/code&gt; database. The JSON data is returned in the response under the key named &lt;code&gt;results&lt;/code&gt; and rendered by the &lt;code&gt;people&lt;/code&gt; template.&lt;/p&gt;
&lt;p&gt;When a request comes in with this url:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;http://example01/service/people/Sam
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If the request is a &lt;code&gt;GET&lt;/code&gt;, the document with &lt;code&gt;name&lt;/code&gt; = &lt;code&gt;Sam&lt;/code&gt; is retrieved, the &lt;code&gt;_id&lt;/code&gt; is converted to a string instead of an object (it is saved as an ObjectId in the MongoDB database). The JSON data is returned in the response under the key named &lt;code&gt;results&lt;/code&gt; and rendered by the &lt;code&gt;person&lt;/code&gt; template.&lt;/p&gt;
&lt;p&gt;If the request is a &lt;code&gt;POST&lt;/code&gt;, the data is read from the request form, the &lt;code&gt;_id&lt;/code&gt; is turned back into an &lt;code&gt;ObjectId&lt;/code&gt; and the document is saved to the database.&lt;/p&gt;
&lt;p class=&#34;callout&#34;&gt;&lt;span class=&#34;note&#34;&gt;Note: &lt;/span&gt;
With this smidgen of code and a rational directory structure, before even talking about the HTML side, we have an simple, easy-to-understand API that can send and receive JSON documents, interacting with a MongoDB backend database.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;a-sample-template-file&#34;&gt;A Sample Template File&lt;/h4&gt;
&lt;p&gt;You can use plain HTML to display and update records if your underlying data has a simple structure, as our example data does.&lt;/p&gt;
&lt;p&gt;This is the view-only template &lt;code&gt;people.tpl&lt;/code&gt; which displays the data on a GET request to &lt;code&gt;http://example02/service/people&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;cp&#34;&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;html&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;People&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;h1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;People&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;h1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;table&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Name&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Age&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
        %for person in results:
            &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
                &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;name&#39;]}}&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
                &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;age&#39;]}}&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
        %end for
    &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;table&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;html&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And this is the (unstyled) result:&lt;/p&gt;
&lt;p&gt;&lt;img alt=&#34;peoplelist&#34; src=&#34;/static/img/blog/peoplelist.png&#34;&gt;&lt;/p&gt;
&lt;p&gt;To view a particular person, the logic is similar, except we get back a single record: Here is the template &lt;code&gt;person.tpl&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;cp&#34;&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;html&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;name&#39;]}} Stats&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;h1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;name&#39;]}} Stats&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;h1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;table&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Age&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;age&#39;]}}&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Height&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;height&#39;]}}&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Weight&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;weight&#39;]}}&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
   &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;table&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;html&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If the url is &lt;code&gt;http://example01/service/people/Alphonse&lt;/code&gt;, here is the result:&lt;/p&gt;
&lt;p&gt;&lt;img alt=&#34;person&#34; src=&#34;/static/img/blog/person.png&#34;&gt;&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;crud-operations&#34;&gt;CRUD Operations&lt;/h4&gt;
&lt;p&gt;Performing updates, deletes, or creating records can get tricky, depending on the complexity of your data. If you have nested JSON data, you will need some Javascript to serialize the user-input data so the result has the correct structure when it gets back to MongoDB.  More on that later. For now let&amp;rsquo;s write a form that enables us to update a simple record and save it. &lt;/p&gt;
&lt;h5 class=&#34;article-title&#34; id=&#34;simple-case&#34;&gt;Simple Case&lt;/h5&gt;
&lt;p&gt;First we need to view the data in a prepopulated form and then we need to update the database record with the changes made in the HTML client.&lt;/p&gt;
&lt;p&gt;We already have the Python code (the &lt;code&gt;person&lt;/code&gt; and &lt;code&gt;update_person&lt;/code&gt; functions), so we just need to write the HTML page. You might call the page &lt;code&gt;person_update.tpl&lt;/code&gt;, and change the &lt;code&gt;@app.get(/&amp;lt;name&amp;gt;)&lt;/code&gt; route to use that instead of the view-only template &lt;code&gt;person.tpl&lt;/code&gt; from before.&lt;/p&gt;
&lt;p&gt;This page displays the data but now inside an HTML form. We keep the &lt;code&gt;_id&lt;/code&gt; value because we must have it in order to update the database. If we don&amp;rsquo;t include the &lt;code&gt;_id&lt;/code&gt; on the way back in to the database, our changed data will be added to the database as a new record instead of updating the original record.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;cp&#34;&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;html&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;name&#39;]}} Stats&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;h1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;name&#39;]}} Stats&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;h1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;form&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;persondata&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;person&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;method&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;post&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;action&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;http://example02/service/people/{{person[&#39;name&#39;]}}&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;_id&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;_id&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;display: none;&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;name&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;name&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;display: none;&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;table&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Age&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;age&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;age&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Height&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;height&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;height&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Weight&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;weight&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;weight&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;button&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;submit&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Save&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;button&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
   &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;table&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;form&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;html&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h5 class=&#34;article-title&#34; id=&#34;complex-case&#34;&gt;Complex Case&lt;/h5&gt;
&lt;p&gt;If you have nested JSON data you will probably have to use Javascript. There are so many solutions that I&amp;rsquo;ll just point out some links. The library I use (and I have deeply nested JSON) is &lt;a href=&#34;https://github.com/maxatwork/form2js&#34;&gt;form2js&lt;/a&gt;. It is a Javascript library for collecting form data and converting it to a Javascript object.&lt;/p&gt;
&lt;p&gt;In no particular order, here are some similar libraries that address the same situation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/Krinkle/jquery-json&#34;&gt;jquery-json&lt;/a&gt; jQuery plugin for conversion to and from JSON.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/DubFriend/forminator&#34;&gt;forminator&lt;/a&gt; uses class names and data attributes to build ajax interfaces.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.domajax.com/&#34;&gt;domajax&lt;/a&gt; jQuery plugin, enables ajax calls without javascript.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/rdiazv/jquery.form.serializer&#34;&gt;jquery.form.serializer&lt;/a&gt; jQuery extension to serialize HTML forms to JSON.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The following is an example of using the &lt;code&gt;form2js&lt;/code&gt; library and jQuery. You can use the library with arbitrarily nested JSON documents. However, you need to make a couple of changes to your Bottle app. This example template and the changed app code are in the GitHub files &lt;code&gt;alt_people.py&lt;/code&gt; and &lt;code&gt;alt_person_update.tpl&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The main change to the &lt;code&gt;people&lt;/code&gt; app is the &lt;code&gt;update_person&lt;/code&gt; function, which now looks like the following. It reads the JSON string from the request body and converts it. The &lt;code&gt;loads&lt;/code&gt; function is included in the &lt;code&gt;pymongo&lt;/code&gt; package; it provides conversion from a string instance to a BSON document. Finally, our function converts the &lt;code&gt;_id&lt;/code&gt; to an ObjectId and saves the record back to the database.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;nd&#34;&gt;@app&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;post&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;update_person&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;():&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;request&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;read&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;person&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;loads&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;person&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;_id&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ObjectId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;person&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;_id&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;people&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;person&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the template, the form itself is identical but the &lt;strong&gt;submit&lt;/strong&gt; button now calls a Javascript function.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;cp&#34;&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;name&#39;]}} Stats&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;h1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;{{person[&#39;name&#39;]}} Stats&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;h1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;form&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;persondata&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;person&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;_id&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;_id&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;display: none;&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;name&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;name&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;display: none;&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;table&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Age&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;age&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;age&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Height&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;height&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;height&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Weight&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;th&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;weight&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;{{person[&#39;weight&#39;]}}&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;submit&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;td&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;tr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;table&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;form&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When the user clicks the &lt;code&gt;submit&lt;/code&gt; button, the &lt;code&gt;save_data&lt;/code&gt; function is called.&lt;/p&gt;
&lt;p&gt;First we load jQuery and the &lt;code&gt;form2js&lt;/code&gt; library. Finally, we have two functions&amp;ndash;one that executes when the submit button is clicked, and the &lt;code&gt;save_data&lt;/code&gt; function.
The &lt;code&gt;save_data&lt;/code&gt; function reads the JSON data from the form into the variable &lt;code&gt;json_data&lt;/code&gt;, stringifies it to the variable &lt;code&gt;jsonstr&lt;/code&gt;, and fires an AJAX POST back to our URL route for updating a person (the only route in our app that accepts a POST request).&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;text/javascript&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;/js/jquery.min.js&#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;text/javascript&#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;/js/form2js.js&#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;text/javascript&#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;save_data&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;function&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;evt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;){&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;json_data&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;form2js&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;persondata&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;skipEmpty&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;jsonstr&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;JSON&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;stringify&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;json_data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;null&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;\t&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;$&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ajax&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;            &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;POST&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;            &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;http://example02/service/people&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;            &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;jsonstr&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;            &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;success&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;function&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(){&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;                &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;Configuration saved.&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;            &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;};&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;function&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;$&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;){&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;     &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;$&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;function&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(){&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;$&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;input[type=submit]&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;val&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;Submit&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;click&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;save_data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;})(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;jQuery&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;   &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When a request comes in at this URL:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;http://example01/service/people/Janice
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The result is displayed in a form:&lt;/p&gt;
&lt;p&gt;&lt;img alt=&#34;person_complex&#34; src=&#34;/static/img/blog/person_complex.png&#34;&gt;&lt;/p&gt;
&lt;p&gt;Check it out on &lt;a href=&#34;https://github.com/tiarno/bam_example&#34;&gt;GitHub&lt;/a&gt; if you want to play around with it. It is very easy to add applications and get CRUD operations going on any mongoDB database.&lt;/p&gt;
&lt;p&gt;Let me know if you have any questions or if something needs changing on the GitHub repo.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>How To Pad a PDF to an N Page Signature</title>
    <link href="https://reachtim.com/articles/pad-pdf-to-n-pages.html" rel="alternate" />
    <id>https://reachtim.com/articles/pad-pdf-to-n-pages.html</id>
    <published>2014-09-13T00:00:00Z</published>
    <updated>2014-09-13T00:00:00Z</updated>
    <category term="LaTeX" />
    <summary type="text">How to use LaTeX to force a PDF to have a multiple of n pages.</summary>
    <content type="html">&lt;p&gt;This how-to article describes how you can create a LaTeX command to pad a PDF with blank pages so that the total number of pages is an integer multiple (to satisify a printer&amp;rsquo;s request).&lt;/p&gt;
&lt;p&gt;Does your printer want the number of pages in your PDFs to be an integer multiple? Mine does. The number of pages in my documents must be a multiple of four. That isn&amp;rsquo;t exactly a &lt;strong&gt;signature&lt;/strong&gt;, but that&amp;rsquo;s the first word that comes to mind. Of course this is easy to do by hand when you have Adobe Acrobat or a similar tool, but this short article shows how to create and use a LaTeX command that will do it for you automatically.&lt;/p&gt;
&lt;p&gt;This example depends on the &lt;code&gt;ifthen&lt;/code&gt;, &lt;code&gt;calc&lt;/code&gt;, and &lt;code&gt;pageslts&lt;/code&gt; packages. &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://ctan.org/pkg/ifthen&#34;&gt;&lt;code&gt;ifthen&lt;/code&gt;&lt;/a&gt; provides the &lt;code&gt;ifthenelse&lt;/code&gt; and &lt;code&gt;whiledo&lt;/code&gt; commands.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://ctan.org/pkg/calc&#34;&gt;&lt;code&gt;calc&lt;/code&gt;&lt;/a&gt; enables us to do infix math to easily set a counter.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://ctan.org/pkg/pageslts&#34;&gt;&lt;code&gt;pageslts&lt;/code&gt;&lt;/a&gt; provides the counter &lt;code&gt;CurrentPage&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As usual with TeX and LaTeX there are multiple ways to solve this problem; this is just one. Other packages that are helpful in this situation but not used in this article are :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://www.ctan.org/pkg/intcalc&#34;&gt;&lt;code&gt;intcalc&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://ctan.org/pkg/fp&#34;&gt;&lt;code&gt;fp&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://ctan.org/pkg/pgf&#34;&gt;&lt;code&gt;pgf&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://ctan.org/pkg/calculator&#34;&gt;&lt;code&gt;calculator&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p class=&#34;callout&#34;&gt;What is a real &lt;strong&gt;signature&lt;/strong&gt;? It is group of pages printed on a large sheet in such a way that you can fold and cut to the finished page size. This &lt;a href=&#34;http://www.designersinsights.com/designer-resources/understanding-and-working-with-print&#34;&gt;article&lt;/a&gt; from &lt;em&gt;Designers Insights&lt;/em&gt; describes in detail what &lt;code&gt;signature&lt;/code&gt; means in the print world.&lt;/p&gt;
&lt;p&gt;What this article is about is not exactly creating a signature; we will create a LaTeX command to pad a PDF with blank pages until the total number of pages is a multiple of four. Usually the printer will set up the signature, but some, like mine, prefer that the number of pages is an integer multiple of the final signature. &lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;the-modulo-function&#34;&gt;The Modulo Function&lt;/h4&gt;
&lt;p&gt;Given the problem, it&amp;rsquo;s pretty clear we&amp;rsquo;re going to need a &lt;a href=&#34;http://en.wikipedia.org/wiki/Modulo_operation&#34;&gt;modulo&lt;/a&gt; function and LaTeX doesn&amp;rsquo;t come with one built in. We can program it since&lt;/p&gt;
&lt;p&gt;$$
 a\mod n = a - n\times(a//n)
$$&lt;/p&gt;
&lt;p&gt;where the $//$ is the &lt;code&gt;integer division&lt;/code&gt; operator. When TeX does division on integers, the fractional part is discarded, which is a form of integer division (&lt;em&gt;truncated division&lt;/em&gt;). Here is a little example you might like to play with: &lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\documentclass&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;article&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\usepackage&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;calc&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\usepackage&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;ifthen&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\newcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;dividend&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\newcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;divisor&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\newcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;answer&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;\begin&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;document&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\noindent&lt;/span&gt; Starting at 10:
&lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;dividend&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;10&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;divisor&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;10&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;\whiledo&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\thedivisor&lt;/span&gt;&amp;gt;0&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;answer&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\thedividend&lt;/span&gt;/&lt;span class=&#34;k&#34;&gt;\thedivisor&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;\noindent&lt;/span&gt; divisor: &lt;span class=&#34;k&#34;&gt;\thedivisor\&lt;/span&gt; answer: &lt;span class=&#34;k&#34;&gt;\theanswer\\&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;divisor&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\thedivisor&lt;/span&gt; - 1&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\end&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;document&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;img alt=&#34;moduloex&#34; src=&#34;/static/img/blog/modulo_example.png&#34;&gt;&lt;/p&gt;
&lt;p&gt;Additionally, this &lt;a href=&#34;http://tex.stackexchange.com/questions/34424/how-do-i-calculate-n-modulo-3-in-latex&#34;&gt;question&lt;/a&gt; on &lt;code&gt;tex.stackexchange.com&lt;/code&gt; has some useful and interesting answers about calculating &lt;code&gt;modulo&lt;/code&gt; with TeX and LaTeX.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;the-latex-macro-padpages&#34;&gt;The LaTeX Macro &lt;code&gt;padpages&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;Now for the macro. We&amp;rsquo;ll name it &lt;code&gt;padpages&lt;/code&gt;. First, create a new counter, &lt;code&gt;modpage&lt;/code&gt;.&lt;/p&gt;
&lt;p class=&#34;callout&#34;&gt;&lt;span class=&#34;note&#34;&gt;Note: &lt;/span&gt;If you use the the &lt;code&gt;memoir&lt;/code&gt; class, you can take advantage of the command &lt;code&gt;thesheetsequence&lt;/code&gt;, which provides the number of the current page; not necessarily the same as the page number that is printed on the page, but the actual number of the page as counted from the beginning of the document. In this example, we&amp;rsquo;ll use the &lt;a href=&#34;http://ctan.org/pkg/pageslts&#34;&gt;&lt;code&gt;pageslts&lt;/code&gt;&lt;/a&gt; package to get the same result (&lt;code&gt;theCurrentPage&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;For the blank pages we&amp;rsquo;re going to insert, we don&amp;rsquo;t want headings or page numbers, so set the page style to &lt;code&gt;empty&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\pagenumbering&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;arabic&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\newcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;modpage&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\newcommand\padpages&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;\pagestyle&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;empty&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Manipulate the &lt;code&gt;modpage&lt;/code&gt; counter to be the real page number &lt;em&gt;mod&lt;/em&gt; 4. Since &lt;/p&gt;
&lt;p&gt;$$
a \mod n = a - n \times(a//n)
$$&lt;/p&gt;
&lt;p&gt;or,&lt;/p&gt;
&lt;p&gt;$$
\text{pagenum} \mod 4 = \text{pagenum} - 4 \times (\text{pagenum}//4)
$$&lt;/p&gt;
&lt;p&gt;Use the &lt;code&gt;modpage&lt;/code&gt; counter to hold the modulo calculation. The &lt;code&gt;calc&lt;/code&gt; package enables us to write the arithmetic inside the argument that sets the counter:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;    &lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;modpage&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\theCurrentPage&lt;/span&gt; - 4*&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\theCurrentPage&lt;/span&gt;/4&lt;span class=&#34;nb&#34;&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;ifthenelse&lt;/code&gt; command takes three arguments. The first one sets the condtion to be tested.  If the condition is true, the second argument is executed; if it is false, the third argument is executed. We test to see if our &lt;code&gt;modpage&lt;/code&gt; counter is equal to zero. If &lt;code&gt;modpage&lt;/code&gt; is zero, we don&amp;rsquo;t need to anything since the page number is already a multiple of 4, so the second argument is simply &lt;code&gt;relax&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\ifthenelse&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\themodpage&lt;/span&gt;=0&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\relax&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Otherwise, we use our third argument, the one that does the work; we need to add $4 - (\text{pagenum}\mod 4)$ pages. We reuse the counter &lt;code&gt;modpage&lt;/code&gt; to now hold the number of pages to add:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;    &lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;modpage&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;4 - &lt;span class=&#34;k&#34;&gt;\themodpage&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;\whiledo&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\themodpage&lt;/span&gt;&amp;gt;0&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
            &lt;span class=&#34;k&#34;&gt;\mbox&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{}&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\clearpage\mbox&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
            &lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;modpage&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\themodpage&lt;/span&gt; - 1&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
        &lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;% end whiledo&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;% end ifthenelse&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;% end padpages&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;\mbox{}\clearpage\mbox{}&lt;/code&gt; forces a blank page to be added. We continue to force these blank pages to be inserted, decrementing the counter each time through the &lt;code&gt;whiledo&lt;/code&gt; loop until the counter is zero.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;\padpages&lt;/code&gt; macro should come at the very end of the document; depending on what other packages you use, you might find the &lt;a href=&#34;http://www.ctan.org/pkg/atveryend&#34;&gt;&lt;code&gt;atveryend&lt;/code&gt;&lt;/a&gt; package useful.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;the-complete-macro&#34;&gt;The Complete Macro&lt;/h4&gt;
&lt;p&gt;Finally, here is the entire code but a bit more general. Now it takes an argument&amp;ndash;the integer for which the total number of pages should be a multiple. For example, your printer might want a multiple of eight instead of four.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;\pagenumbering&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;arabic&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\newcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;modpage&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;\newcommand\padpages&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;[1]&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;\pagestyle&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;empty&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;modpage&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\theCurrentPage&lt;/span&gt; - #1*&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\theCurrentPage&lt;/span&gt;/#1&lt;span class=&#34;nb&#34;&gt;}}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;\ifthenelse&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\themodpage&lt;/span&gt;=0&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
        &lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\relax&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
        &lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;modpage&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;#1 - &lt;span class=&#34;k&#34;&gt;\themodpage&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
            &lt;span class=&#34;k&#34;&gt;\whiledo&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\themodpage&lt;/span&gt;&amp;gt;0&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
                &lt;span class=&#34;k&#34;&gt;\mbox&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{}&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\clearpage\mbox&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
                &lt;span class=&#34;k&#34;&gt;\setcounter&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;{&lt;/span&gt;modpage&lt;span class=&#34;nb&#34;&gt;}{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;\themodpage&lt;/span&gt; - 1&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;%&lt;/span&gt;
            &lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;% end whiledo&lt;/span&gt;
        &lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;% end ifthenelse&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;c&#34;&gt;% end padpages&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h5 class=&#34;article-title&#34; id=&#34;example&#34;&gt;Example&lt;/h5&gt;
&lt;p&gt;For example, say the current number of page is 121 and we need the final number of pages to be a multiple of 4. That is, $p \mod 4 = 1$, which is defined as&lt;/p&gt;
&lt;p&gt;$$
    \text{pages} \mod 4 = \text{pages} - 4\times(\text{pages}//4)
$$&lt;/p&gt;
&lt;p&gt;or, substituting ($121//4=30$):&lt;/p&gt;
&lt;p&gt;$$
  121 - 4\times(121//4) = 121-4*30 = 1
$$&lt;/p&gt;
&lt;p&gt;With 1 as the remainder (that is, 1 is the solution to the modulo operation), we now use it to calculate the number of pages to add. &lt;/p&gt;
&lt;p&gt;In the &lt;code&gt;setcounter&lt;/code&gt; just before the &lt;code&gt;whiledo&lt;/code&gt; loop begins, we subtract the remainder from the initial integer multiple: $4-1=3$. We add three blank pages to the document so it goes from 121 pages to 124 pages, a multiple of 4.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>psutil and MongoDB for System Monitoring</title>
    <link href="https://reachtim.com/articles/psutil-mongodb-monitoring.html" rel="alternate" />
    <id>https://reachtim.com/articles/psutil-mongodb-monitoring.html</id>
    <published>2014-09-03T00:00:00Z</published>
    <updated>2014-09-03T00:00:00Z</updated>
    <category term="Python" />
    <summary type="text">How to use psutil and MongoDB for monitoring system health.</summary>
    <content type="html">&lt;p&gt;This how-to article describes how you can create a set of charts for monitoring the load on one or more servers. It uses Python (psutil and bottle), MongoDb, and jquery. The general idea is the same no matter if you use a different database or web framework.&lt;/p&gt;
&lt;p&gt;At the end of the process, you will have a web page for each machine that displays charts showing cpu, memory, and disk usage.&lt;/p&gt;
&lt;p&gt;I need to watch a couple of FreeBSD machines to make sure they&amp;rsquo;re healthy and not running into memory or disk space issues. Their names are, for the purposes of the article, &lt;code&gt;example01&lt;/code&gt; and &lt;code&gt;example02&lt;/code&gt;.  &lt;/p&gt;
&lt;p class=&#34;callout&#34;&gt;&lt;span class=&#34;note&#34;&gt;Note: &lt;/span&gt;These happen to be the same machines that run the MongoDB replica set and one of them runs the web server. There is no reason they all have to be the same machines&amp;ndash;you can have MongoDB running on completely different machines than the ones you want to monitor. The same is true for the web server&amp;ndash;it can be on any machine, not necessarily one you&amp;rsquo;re monitoring.&lt;/p&gt;
&lt;p&gt;I don&amp;rsquo;t want to go to each machine and run &lt;code&gt;top&lt;/code&gt; or &lt;code&gt;ds&lt;/code&gt; to find out what&amp;rsquo;s going on, I want a web page with up-to-date charts that I can glance at, one page per machine.&lt;/p&gt;
&lt;p&gt;The overall workflow follows these three steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Get the system data into MongoDB (&lt;code&gt;psutil&lt;/code&gt; and &lt;code&gt;cron&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Set up a web server to query the MongoDB server (&lt;code&gt;bottle&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Write a short HTML page to display the data (&lt;code&gt;jqplot&lt;/code&gt; + AJAX).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt=&#34;psflow&#34; src=&#34;/static/img/blog/psflow.png&#34;&gt;&lt;/p&gt;
&lt;p&gt;You can get all the code in the GitHub project &lt;a href=&#34;https://github.com/tiarno/psmonitor&#34;&gt;psmonitor&lt;/a&gt;. I use snippets of that code in this article.&lt;/p&gt;
&lt;p&gt;In the first part, you use the &lt;code&gt;psutil&lt;/code&gt; Python package inside a &lt;code&gt;cron&lt;/code&gt; job to write system load information to a capped collection in MongoDB every 5 minutes. The 5 minutes is totally arbitrary&amp;ndash;you can pick whatever period you like. With the systems I&amp;rsquo;m looking at, a few minutes provides a fine enough granularity. This part puts the data into MongoDB.&lt;/p&gt;
&lt;p&gt;In the second part, the &lt;code&gt;bottle&lt;/code&gt; application makes a request to MongoDB and responds with the JSON data. This creates the broker between the client HTML page and the MongoDB data.&lt;/p&gt;
&lt;p&gt;In the third part, you have an HTML file corresponding to each machine you want to monitor. The file loads &lt;code&gt;jqplot&lt;/code&gt; and makes an AJAX request to the &lt;code&gt;bottle&lt;/code&gt; application. This part is the reason for the exercise&amp;ndash;we get the time-series chart of the load data that we&amp;rsquo;ve stored in MongoDB.&lt;/p&gt;
&lt;p&gt;This is an example of one of the charts we will create:
&lt;img alt=&#34;monitor01&#34; src=&#34;/static/img/blog/monitor01.png&#34;&gt;&lt;/p&gt;
&lt;p&gt;You can make charts for any data you want. 
The charts I use for each machine are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;cpu user percent&lt;/li&gt;
&lt;li&gt;cpu system percent&lt;/li&gt;
&lt;li&gt;cpu irq&lt;/li&gt;
&lt;li&gt;cpu nice percent&lt;/li&gt;
&lt;li&gt;disk space free&lt;/li&gt;
&lt;li&gt;memory free&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;part-0-get-the-tools&#34;&gt;Part 0: Get the Tools&lt;/h4&gt;
&lt;p&gt;The following tools are the ones to install for this implementation, but you can use a different database or web framework if you already have one.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;psutil&lt;/code&gt; Python package is a great cross-platform tool for system monitoring. Read more about it on its &lt;a href=&#34;http://pythonhosted.org/psutil/&#34;&gt;project page&lt;/a&gt;. You can install it with &lt;code&gt;pip&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The NOSQL database &lt;a href=&#34;http://www.mongodb.org/&#34;&gt;mongoDB&lt;/a&gt; is an open-source document database. If you have data that naturally fits a JSON-style structure, mongoDB is a great fit for data storage. It has become a natural tool for me anytime I find I need a JSON store. The funny thing is, the more I use it, the more I find new uses for it. The structure of a MongoDB &lt;em&gt;document&lt;/em&gt; is suprising useful and can provide a map for lots of information types.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;bottle&lt;/code&gt; web &lt;a href=&#34;http://bottlepy.org/docs/dev/index.html&#34;&gt;framework&lt;/a&gt; is a tiny framework written in Python with no other dependencies. You can install it with &lt;code&gt;pip&lt;/code&gt;. You can use the included development server to get things going and put it under a different backend server later. I put mine under my Apache server once I got things like I wanted them. I&amp;rsquo;ll write an article about that setup later on.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&#34;http://www.jqplot.com/&#34;&gt;jqplot&lt;/a&gt; &lt;code&gt;jquery&lt;/code&gt; plugin makes producing the charts easy, plus they look good and they are all uniform so it easy to compare charts. For example, it is easy to see on the chart when a process spins up because you can see the spike for cpu and memory at the same moment, which is the same x-axis location for both charts. There is a lot more you can do with this plugin, this exercise just scratches the surface.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;part-1-get-the-data&#34;&gt;Part 1: Get the Data&lt;/h4&gt;
&lt;p&gt;We want to create a data structure that follows this pattern. You can add or remove any data that psutil supports in this step. The end point is the user, who will be looking at a &lt;code&gt;jqplot&lt;/code&gt; chart, so it is good to keep that data structure in the back of your mind. What &lt;code&gt;jqplot&lt;/code&gt; will want is a list of two-element lists, one of which is a datetime, something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;[[datetime1, y-value1], [datetime2, y-value2], and so on.]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That structure isn&amp;rsquo;t the most efficient way to gather the data, but it&amp;rsquo;s always important to remember where you&amp;rsquo;re going. We&amp;rsquo;ll gather the data and then change the structure to give &lt;code&gt;jqplot&lt;/code&gt; the structure it needs.&lt;/p&gt;
&lt;p&gt;As my first step I wasn&amp;rsquo;t sure of what I needed and would use and I had a few more measurements. I figured that as time went by and I saw the actual needs of the machine this would change, and it could be easily changed. &lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;s1&#34;&gt;&#39;server&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;servername&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
  &lt;span class=&#34;s1&#34;&gt;&#39;datetime&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;datetime&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;now&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(),&lt;/span&gt;
  &lt;span class=&#34;s1&#34;&gt;&#39;disk_root&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
  &lt;span class=&#34;s1&#34;&gt;&#39;phymem&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:,&lt;/span&gt;
  &lt;span class=&#34;s1&#34;&gt;&#39;cpu&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;user&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;nice&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;system&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;idle&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;irq&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:,},&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What follows is the python code for getting data from the system (using &lt;code&gt;psutil&lt;/code&gt;) into the MongoDb database. I created a collection in MongoDb for each machine to be monitored. &lt;/p&gt;
&lt;p&gt;You could create a single document in MongoDb that contains data for each machine; it depends on your needs. Since I want a separate page for each machine, I divided the data in the same way. You might want the data for all machines in one document if you want to render one page with charts for all the machines.&lt;/p&gt;
&lt;h5 id=&#34;about-the-mongodb-collection&#34;&gt;About the MongoDB Collection.&lt;/h5&gt;
&lt;p&gt;I have a three-member MongoDB replica set named &lt;code&gt;rs1&lt;/code&gt;. The machines that hold the data happen to be the same ones I am monitoring, &lt;code&gt;example01&lt;/code&gt; and &lt;code&gt;example02&lt;/code&gt;.
The third member is an arbiter and doesn&amp;rsquo;t keep the data. These mongoDB servers don&amp;rsquo;t have to be the ones that are monitored, they could be anywhere.&lt;/p&gt;
&lt;p&gt;I have a database called &lt;code&gt;reports&lt;/code&gt; and it is in that database we will put the new collections. For each machine we will have one collection to contain its load data:
With 1440 minutes per day, sampling every 5 minutes, and keeping two days worth of data, we&amp;rsquo;ll need 576 records (documents)&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;(1440/5)*2 = 576 records per server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I wasn&amp;rsquo;t sure how much data I would eventually use, so I estimated 2k per document. I estimated a generous size for the documents because this is just a start and I may want to gather more data later on. Turns out that 2k is extremely generous. The average size of a record is around 200 bytes but I haven&amp;rsquo;t included any networking data (and disk space is cheap). &lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;576 documents @ 2048 bytes per doc = 1,179,648 bytes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For each machine to be monitored, I created a capped collection with a maximum size of 1179648 and a maximum number of 576 documents:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;nx&#34;&gt;use&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;reports&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;createCollection&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;example01&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;capped&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;size&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;1179648&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;max&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;576&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;createCollection&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;example02&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;capped&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;size&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;1179648&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;max&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;576&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By using a capped collection, we are guaranteed that the data will be preserved in insertion order, and old documents are automatically removed as time goes by so we always have the latest 48 hours of data.&lt;/p&gt;
&lt;h5 id=&#34;the-data-gathering-code&#34;&gt;The Data Gathering Code&lt;/h5&gt;
&lt;p&gt;First, do the necessary imports and make the connection to the MongoDb instance.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;datetime&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;datetime&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;psutil&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pymongo&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;socket&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;conn&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pymongo&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MongoReplicaSetClient&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
    &lt;span class=&#34;s1&#34;&gt;&#39;example01.com, example02.com&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;replicaSet&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;rs1&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;db&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;conn&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;reports&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now call &lt;code&gt;psutil&lt;/code&gt; for every piece of data you want.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;main&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;():&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;cpu&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psutil&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;cpu_times_percent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;disk_root&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psutil&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;disk_usage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;phymem&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psutil&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;phymem_usage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Create a dictionary to contain the data in the time-series structure you need.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;    &lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;dict&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;server&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;socket&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gethostname&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;date&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;datetime&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;now&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;disk_root&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;disk_root&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;free&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; 
    &lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;phymem&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;phymem&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;free&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;cpu&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;s1&#34;&gt;&#39;user&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;user&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; 
        &lt;span class=&#34;s1&#34;&gt;&#39;nice&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;nice&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
        &lt;span class=&#34;s1&#34;&gt;&#39;system&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;system&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; 
        &lt;span class=&#34;s1&#34;&gt;&#39;idle&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;idle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
        &lt;span class=&#34;s1&#34;&gt;&#39;irq&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;irq&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally, add that dictionary as a document into the corresponding MongoDb collection. It is converted to a BSON document when it is inserted into the database, but the structure is the same.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;server&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;example01.com&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;example01&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;elif&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;server&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;example02&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;example02&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;doc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There you have the code to get the data and the database collections in which to store it. All that&amp;rsquo;s left to do for this part is to automatically run the code:&lt;/p&gt;
&lt;p&gt;Set up a cron job to run the script every 5 minutes on each server you want to monitor:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;*/5&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;*&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;*&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;*&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;*&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;/path/to/psutil_script
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Each mongoDB collection contains 48 hours of system performance data about the corresponding server; you can set it and forget it.&lt;/p&gt;
&lt;h4 class=&#34;article-title&#34; id=&#34;part-2-set-up-the-bottle-server&#34;&gt;Part 2: Set up the bottle Server&lt;/h4&gt;
&lt;p&gt;Create a &lt;code&gt;bottle&lt;/code&gt; application to query the mongoDb collection.&lt;/p&gt;
&lt;p&gt;Connect to MongoDb. On receipt of a request for server data, return the formatted data for the appropriate server in the response.  &lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;kn&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;bottle&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bottle&lt;/span&gt;
&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nn&#34;&gt;pymongo&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;load&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bottle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;conn&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pymongo&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MongoReplicaSetClient&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
    &lt;span class=&#34;s1&#34;&gt;&#39;example01.com, example02.com&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;replicaSet&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;rs1&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;db&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;conn&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;reports&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a route, a url connection. When a request comes in, &lt;em&gt;get&lt;/em&gt; the servername from the url (&lt;code&gt;&amp;lt;server&amp;gt;&lt;/code&gt;) and create and return the proper data structure (the structure that &lt;code&gt;jqplot&lt;/code&gt; will need).&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;nd&#34;&gt;@load&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;/&amp;lt;server&amp;gt;&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;get_loaddata&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;server&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;data_cursor&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;server&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;example02&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;data_cursor&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;example02&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;find&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;elif&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;server&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&#39;example01&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;data_cursor&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;db&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;example01&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;find&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;

    &lt;span class=&#34;n&#34;&gt;disk_root_free&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;phymem_free&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;cpu_user&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;cpu_nice&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;cpu_system&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;cpu_idle&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;cpu_irq&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data_cursor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;date&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;date&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;disk_root_free&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;disk_root&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;phymem_free&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;phymem&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;cpu_user&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;cpu&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;user&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]])&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;cpu_nice&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;cpu&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;nice&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]])&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;cpu_system&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;cpu&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;system&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]])&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;cpu_idle&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;cpu&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;idle&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]])&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;cpu_irq&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;cpu&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;irq&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]])&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
            &lt;span class=&#34;s1&#34;&gt;&#39;disk_root_free&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;disk_root_free&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
            &lt;span class=&#34;s1&#34;&gt;&#39;phymem_free&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;phymem_free&lt;/span&gt;
            &lt;span class=&#34;s1&#34;&gt;&#39;cpu_user&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu_user&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
            &lt;span class=&#34;s1&#34;&gt;&#39;cpu_irq&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu_irq&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
            &lt;span class=&#34;s1&#34;&gt;&#39;cpu_system&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu_system&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
            &lt;span class=&#34;s1&#34;&gt;&#39;cpu_nice&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu_nice&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
            &lt;span class=&#34;s1&#34;&gt;&#39;cpu_idle&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cpu_idle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
            &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4 class=&#34;article-title&#34; id=&#34;part-3-display-the-data-with-jqplot&#34;&gt;Part 3: Display the Data with jqplot&lt;/h4&gt;
&lt;h5 id=&#34;the-html-page&#34;&gt;The HTML Page&lt;/h5&gt;
&lt;p&gt;The HTML is very simple. &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Read in the stylesheet.&lt;/li&gt;
&lt;li&gt;Write the &lt;code&gt;div&lt;/code&gt; to hold each chart. In the example below I display only the &lt;code&gt;cpu_user&lt;/code&gt; data. The pattern is the same for all the variables.&lt;/li&gt;
&lt;li&gt;Load the javascript.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You can put the javascript from &lt;code&gt;psmonitor.js&lt;/code&gt; directly into the page or call it as a file (as shown in the example).&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;cp&#34;&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;html&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;meta&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;charset&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;utf-8&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Load Monitoring&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;link&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;rel&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;stylesheet&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text/css&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;href&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;/css/jquery.jqplot.css&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;div&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;margin&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;auto&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;}&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;

    &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;div&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;cpu_user&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;height:400px;width:800px; &#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;div&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;http://code.jquery.com/jquery-latest.min.js&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text/javascript&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;/js/jquery.jqplot.js&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text/javascript&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;/js/jqplot.json2.js&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text/javascript&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;/js/jqplot.dateAxisRenderer.js&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text/javascript&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;/js/jqplot.highlighter.js&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text/javascript&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;/js/jqplot.cursor.js&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&#34;cm&#34;&gt;&amp;lt;!--[if lt IE 9]&amp;gt;&lt;/span&gt;
&lt;span class=&#34;cm&#34;&gt;  &amp;lt;script language=&#34;javascript&#34; type=&#34;text/javascript&#34; src=&#34;excanvas.js&#34;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&#34;cm&#34;&gt;&amp;lt;![endif]--&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;script&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;text/javascript&#34;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;src&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&#34;/js/psmonitor.js&#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;html&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h5 id=&#34;the-javascript-jqplot-code&#34;&gt;The JavaScript (jqplot) Code&lt;/h5&gt;
&lt;p&gt;The complete code is in the GitHub project, but here is a snippet of how &lt;code&gt;jqplot&lt;/code&gt; is set up to show the data. The machine &lt;code&gt;example01&lt;/code&gt; runs the web server that will return the json load data. Again, the web server could be on any machine; in my example, it happens to be one of the machines being monitored. &lt;/p&gt;
&lt;p&gt;The code follows the same pattern for each plot:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Make the AJAX call to the server that is running the &lt;code&gt;bottle&lt;/code&gt; app.&lt;/li&gt;
&lt;li&gt;Put the data you want to chart into a variable.&lt;/li&gt;
&lt;li&gt;Pass the variable to &lt;code&gt;jqplot&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The &lt;code&gt;url&lt;/code&gt; in the code contains the string &amp;lsquo;example01&amp;rsquo;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;url: &#34;http://example01/load/example01&#34;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The first instance of &lt;code&gt;example01&lt;/code&gt; is addressing the web server since that is the machine running the &lt;code&gt;bottle&lt;/code&gt; app. The second instance is the name of the machine we want the data for. That is the server name (&lt;code&gt;&amp;lt;server&amp;gt;&lt;/code&gt;) that is passed to the &lt;code&gt;bottle&lt;/code&gt; route (&lt;code&gt;get_loaddata&lt;/code&gt;) for retrieving the MongoDB records (documents).&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#34;nx&#34;&gt;$&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;document&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ready&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;function&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(){&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;jsonData&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;$&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ajax&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;async&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;http://example01/load/example01&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;dataType&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;json&#34;&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;

&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;cpu_user&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;jsonData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;responseJSON&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;cpu_user&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]];&lt;/span&gt;

&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;$&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;jqplot&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&#39;cpu_user&#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;cpu_user&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;CPU User Percent: EXAMPLE01&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;highlighter&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;show&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;sizeAdjust&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;7.5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;cursor&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;show&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;axes&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;xaxis&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;renderer&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;$&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;jqplot&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;DateAxisRenderer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;              &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;tickOptions&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;formatString&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&#34;%a %H:%M&#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}}},&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;        &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;series&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;lineWidth&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;showMarker&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}]&lt;/span&gt;
&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This javascript plots the CPU user percent; you can add the other plots in exactly the same way, changing only the variable name and the title.&lt;/p&gt;</content>
  </entry>
</feed>