<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Create Blog &#187; Python</title>
	<atom:link href="http://create.tpsitulsa.com/blog/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://create.tpsitulsa.com/blog</link>
	<description>the create framework blog</description>
	<lastBuildDate>Sat, 30 Jan 2010 20:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Roots Deliver Nutrients</title>
		<link>http://create.tpsitulsa.com/blog/2009/12/07/roots-deliver-nutrients/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/12/07/roots-deliver-nutrients/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 21:14:06 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Comet]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SproutCore]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=245</guid>
		<description><![CDATA[Push is complex. I tried to figure it out with ActiveMQ, Orbited, and friends awhile back, but it just seemed too complicated. It made me feel like I was not very smart—obviously, some people understand this thing, but I don&#8217;t. I am a bit too prideful, and do not like feeling unintelligent. But I need [...]]]></description>
			<content:encoded><![CDATA[<p>Push is complex. I tried to figure it out with ActiveMQ, Orbited, and friends awhile back, but it just seemed too complicated. It made me feel like I was not very smart—obviously, some people understand this thing, but I don&#8217;t. I am a bit too prideful, and do not like feeling unintelligent.</p>

<p>But I need messaging.</p>

<p>I don&#8217;t care about queuing or persistence. For web applications (what I work on) messages should be delivered as quickly as possible. Any &#8220;queues&#8221; can be in-memory, and any message should not remain in a queue for more than a minute—if the client was disconnected for any duration, it would likely be better off re-downloading that receiving incremental updates.</p>

<p>So, whatever these <em>systems</em> do, they seem too complicated (I could be wrong—as I said, I have trouble understanding them).</p>

<p>My requirements:</p>

<ul>
    <li><strong>Normal Authentication</strong>. Clients should go through the &#8220;normal&#8221; web app to subscribe to events.</li>
    <li><strong>Easy</strong>. It should be very simple and easy to understand.</li>
    <li><strong>Quick</strong>. I should be able to add Comet to an app in ten to fifteen minutes, maximum.</li>
</ul>

<p>I found out that it is not only possible to write messaging servers on one&#8217;s own—it is relatively easy. So I wrote one.</p>

<p>I&#8217;ll stop the backstory here, though there is plenty more—tell me if you want any of the boring details about Dolores and the rest.</p>

<h3>Roots</h3>

<p>Roots—codenamed Dobby after the Harry Potter character—is a messaging server that has a built-in long-polling server (among several other things).</p>

<p>Roots is made up of many small message-passing pieces. They plug together, just like Lego pieces. Each piece is simple, and the connection between the pieces are even simpler: a single function, called &#8220;update.&#8221;</p>

<p>If you are interested, I&#8217;ll go over that more thoroughly in &#8220;Implementation.&#8221;</p>

<h3>How Roots is Used</h3>

<p><strong>Above All, Roots is Easy.</strong></p>

<p>It takes me about fifteen minutes to use Roots to add push to a SproutCore application (starting with a fresh clone of Roots—which, by the way, does not need any configuration for testing). It <em>should</em> only take me five minutes, but I am often a bit slow of a coder.</p>

<p><strong>There is a brief semi-tutorial <a href="http://github.com/ialexi/dobby">on GitHub</a>.</strong> The following is based on that tutorial, but has some extra background information.</p>

<p>So, a bird&#8217;s-eye view: unless you implement the server-side <em>in</em> Roots itself, you will have three components in any application: <strong>back-end</strong> (Rails, Django, etc.), <strong>front-end</strong> (SproutCore), and Roots.</p>

<ul>
    <li>Client—such as Pomona, the SproutCore framework for Roots) connects to a Roots server.</li>
    <li>Roots sends the client (Pomona) an id.</li>
    <li>
        The client calls predefined URL to connect to events.
        <ul>
            <li>The URLs are part of the back-end (Django, etc.)</li>
            <li>The ID is sent with these requests.</li>
            <li>Clients (Pomona) usually will keep track of what server-side events you are listening to and can reconnect automatically if the Dobby connection is interrupted—you just provide the client with the URLs and the events to listen to.</li>
        </ul>
    </li>
    <li>Back-end tells Roots to connect client to events—and can perform any authorization it wishes.</li>
    <li>Back-end sends updates to Roots.</li>
    <li>Client receives notifications—everyone&#8217;s Happy!!!</li>
</ul>

<h3>What You Do (for SproutCore)</h3>

<p>You don&#8217;t have to do much:</p>

<ol>
    <li>Add <a href="http://github.com/ialexi/pomona">Pomona</a> to your SproutCore project.</li>
    <li>Add a Roots connector to your server-side project:
        <ul>
        <li><a href="http://github.com/ialexi/cornelius">Cornelius for Python</a></li>
        <li>Others as I add them (I plan Ruby and PHP)</li>
        <li>Implement your own (the Python version of the better protocol—Dudley—is 45 lines counting whitespace and comments).</li>
        </ul>
    </li>
    <li>&#8220;Update&#8221; Roots (call update(path, message) from your back-end—Django has handy model post-save signals you can use).</li>
    <li>Add some URLs to your back-end that connect clients.
        <ul>
        <li>URLs accept client ID in them (/my/connect/AN_ID_HERE or /my/connect.src?id=AN_ID_HERE)</li>
        <li>URLs should accept POST of a simple JSON array of path/event names.</li>
        <li>For each path/event/whatever you want to call it, call connect(id, path) on your Roots connector.</li>
    </li>
    <li>Initialize Pomona and connect to Roots in your SproutCore Data Source.</li>
    <li>Accept incoming data!</li>
</ol>

<p>A more detailed tutorial is <a href="http://github.com/ialexi/dobby">here on GitHub</a> with the rest of Dobby (Roots).</p>

<p><del datetime="2009-12-16T22:53:44+00:00"><strong>Open Question</strong>: Would anyone like a tutorial on setting up a Django back-end for the SproutCore <a href="http://wiki.sproutcore.com/Todos%C2%A0Intro">Todos</a> tutorial, with an extra step of adding Comet at the end?</del> <a href="http://create.tpsitulsa.com/blog/2009/12/11/sc-todos-django-comet/">Done!</a></p>

<h4>Implementation</h4>

<p>Roots/Dobby is currently written in Python. I am working on a rewrite in <a href="http://nodejs.org/">node.js</a>, because node.js is <em>super cool</em>—and, secondarily, it means that some of the same code used in the server (for instance, the message dispatch code) could be used on the client.</p>

<p>Roots has many small parts that hook together in a very simple way. At Roots&#8217;s core is Thestral, a simple interface that requires implementors to do two things: have an id and implement a single method: update(sender, path, message)—where sender is an originator, path is an event name, and message is some data to send.</p>

<p>Here are some of the components:</p>

<ul>
<li><strong>Dolores</strong>: This is the main controller. It assigns IDs to all Thestral instances. It itself is a Thestral instance, and sends its updates to &#8220;delegates&#8221;.</li>
<li><strong>Pig</strong>: An &#8220;Owl,&#8221; or, really, a dispatcher; it knows who receives what message. It is often one of Dolores&#8217;s delegates. Specially-formed messages, tell it to connect certain Thestral ids with certain events—for instance, the event &#8220;::connect&#8221; with message &#8220;12345->my/data&#8221; would connect Thestral &#8220;12345&#8243; to &#8220;my/data&#8221;.</li>
<li><strong>Imperio</strong>. A somewhat pathetic text-based protocol, with accompanying receiver server (a bit like Dudley, which should be used instead). In addition to reading from a server, Imperio supports writing to anything with a write() method, and as such, is used for logging as a delegate of Dolores.</li>
<li><strong>Dudley</strong>. Dudley is not a delegate of Dolores. Rather, Dolores can be its &#8220;delegate&#8221; (or, in this case, &#8220;receiver&#8221;). Dudley is an HTTP server, and receives messages over HTTP and funnels them to a receivers (Thestral implementation), and Dolores is its default receiver.</li>
<li><strong>Firenze.</strong> Firenze, like Dudley, is an HTTP server; but instead of being a receiving server, it is a long-polling push server. It is not usually Dolores&#8217;s delegate—you don&#8217;t want every client to get every message. Instead, Firenze merely registers itself with Dolores to get an ID, and lets others register it (for instance, a client could send Dudley a message, which, through Dolores, would be received by Pig to make the connection).</li>
</ul>

<p>Above all, it is simple. The longest of these pieces is Firenze, at 280 lines of code (50 of which are intro comments I used to explain it to myself and spec it while planning).</p>

<h3>That&#8217;s All!</h3>

<p>I hope you find this helpful. I am excited about porting Roots to node.js; progress is somewhat slow, as I am quite busy (I am a student, and finals for all four classes tomorrow!)</p>

<p>What I am most looking forward to is how Pig (the dispatcher) could use the same code in both the client (Pomona) and the server—the routing is quite similar; in one case, you want to route to handlers/callbacks, in the other, to update functions on Thestral instances—pretty similar; even identical.</p>

<p>I am also looking forward to experimenting with two-way push, where all data is transferred through push—completely eradicating the expectation of a response to a request, and providing only a single receive/send code path on both client and server.</p>

<p>Anyway, I think this is all super cool. :)</p>

<p><strong><em>Thanks to Endash for the nice title. :)</em></strong>
<strong>Roots (called Dobby in development) is <a href="http://github.com/ialexi/dobby">available on GitHub.</a></strong></p>

<p><strong>P.S. I like the name Dobby, but Roots is better P.R.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/12/07/roots-deliver-nutrients/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Sprouting—Automated Spriting for SproutCore</title>
		<link>http://create.tpsitulsa.com/blog/2009/10/17/sprouting%e2%80%94automated-spriting-for-sproutcore/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/10/17/sprouting%e2%80%94automated-spriting-for-sproutcore/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 23:09:09 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SproutCore]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[spriting]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=203</guid>
		<description><![CDATA[Well. Spriting. Do you know what it is? While I&#8217;ve included a very brief summary, you may want to just skip that part if you already know about it, or find a better description of the technique. Also, while this article is specifically aimed at SproutCore, the concepts (and the script download) can easily be [...]]]></description>
			<content:encoded><![CDATA[<p>Well. Spriting. Do you know what it is? While I&#8217;ve included a <em>very</em> brief summary, you may want to <a href="#what-is-it">just skip that part</a> if you already know about it, or find <a href="http://www.alistapart.com/articles/sprites">a better description</a> of the technique. Also, while this article is specifically aimed at SproutCore, the concepts (and the script download) can easily be applied elsewhere.</p>

<h3>Well, here are your options:</h3>

<ol>
    <li><strong>Read the whole article.</strong> Could be amusing, if I’m having a bad writing day.</li>
    <li><a href="#skip-to-it">Skip to the download and “how to use.”</a> No, I’m not going to explain the code. Well, not here, at any rate. I’ll explain it in the code itself. Yes, of course there are comments.</li>
</ol>

<h3 id = "what-is-it">￼What is Spriting?</h3>

<p><div id="attachment_211" class="wp-caption alignleft" style="width: 110px"><img src="http://create.tpsitulsa.com/blog/wp-content/uploads/2009/10/sprites-100x300.png" alt="Example Sprited Images" title="Sprites" width="100" height="300" class="size-medium wp-image-211" /><p class="wp-caption-text">Example Sprited Images</p></div>Spriting is a technique which combines multiple small images into one large image. This can be very beneficial, because it means that, instead of downloading, say, <em>100</em> small images for all the commonly used icons in your web application, the web browser downloads <em>one</em> bigger image (or, perhaps, two or three, but you get the picture).</p>

<p>The goal of spriting is to reduce the effect of latency on your web app’s performance.</p>

<p>To your left is an example set of sprited images.</p>

<h3>Using a Script</h3>

<p>You may thing that combining several images into one larger image might be a bit time-consuming and, overall, a tad challenging, especially if you are often updating the individual images.</p>

<p>You are not alone. I feel the same way.</p>

<p>However, I do know a little about writing scripts—even if I’d like to know a lot more; I’m still only learning Python. So, there is a simple answer:</p>

<p>Write a darn script!</p>

<h3 id = "skip-to-it">The Product</h3>

<p>If you just want the script and how to use it, you’re in luck. Because there are, in fact, some comments in the script, I’ll leave the explanation of how the code works for there.</p>

<p>You can <a href="http://github.com/ialexi/Spriter">download the code</a> from GitHub.</p>

<p>The script is written in Python. It requires:
Either: Python 2.6+ or Python 2.5 with simplejson installed
PIL (Python Imaging Library)</p>

<p>Very briefly, here is what it does:</p>

<ul>
    <li>Takes a “sets” folder, which should have sub-folder <em>sets</em> of icons to be sprited.</li>
    <li>Takes an output folder, under which it will create new set folders containing CSS and image files.</li>
    <li>Combines images under a maximum size (128&#215;128 by default—see Configuration) into a sprites.png file in the output set folder.</li>
    <li>Creates CSS referencing these images using a URL template (SproutCore oriented at first—see URL)</li>
</ul>

<h3>Sets</h3>

<p>You often might have more than one set of icons. For instance, you could have a main set, and then an alternate set that you only want to load on demand. You still want that additional set to be sprited.</p>

<p>Or, perhaps, you may have images meant for repeating backgrounds, but that you still want to sprite.</p>

<p>To facilitate this, the spriting script takes, as its first argument, the location to a sets folder. You put your sets as subfolders into this folder. For instance, this could be your folder layout:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">my-sets
	common
		delete-32.png
		delete-64.png
		refresh-32.png
		refresh-64.png
		refresh-512.png
	repeat-x
		config.js       — see Configuration
		toolbar.png
		footer.png
		header.png
	odd-feature
		some-icon.png
		other-icon.png</pre></div></div>


<h3>URLs</h3>

<p>The CSS has to reference the images. Only problem: it doesn’t know where those images are.</p>

<p>Usually, you’d just be able to use paths relative to the StyleSheet. However, if you are using SproutCore, this won’t work because it moves around all the CSS and JavaScript.</p>

<p>With SproutCore, you need something more like:</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">static_url<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/set/sprites.png'</span><span style="color: #00AA00;">&#41;</span></pre></div></div>


<p>This is what the script does by default. Just like above. It uses a URL template of</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">static_url<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/{set}/{image}'</span><span style="color: #00AA00;">&#41;</span></pre></div></div>


<p>It is a string which will be passed through the Python <tt>format</tt> function, with <em>set</em> and <em>image</em> as arguments.</p>

<h3>Configuration</h3>

<p>There are a few things that are configurable. Configuration is done per-set.</p>

<p>Configuration files are JSON files, and are very simple. They are optional, and take at most four parameters:</p>

<ul>
    <li><strong>max-size</strong>: Default: 128.<br />The maximum size for sprited images. Images above this size will be put as separate images. Useful if you have some really high-res icons you don’t want to sprite, but want to keep with the set. </li>
    <li><strong>repeat</strong>: Default: “none”; can also be “x” or “y”.<br /> Determines the repeat mode. If repeat is “x,” the sprites will be laid out in rows; if “y,” the sprites will be laid out in columns. The generated CSS will include repeat-x or repeat-y, respectively.  Note that all images in a repeat-x set must be the same width, and all images in a repeat-y set must be the same height. The width or height of these images should be set using repeat-width and repeat height, which are discussed below. </li>
    <li><strong>repeat-width</strong>: Default: 32.<br />If repeat=“x,” the width of the repeat images. If your repetition does not require a pattern, you could set this to 1. </li>
    <li><strong>repeat-height</strong>: See that lovely summary of repeat-width above? Well, this is the same, but for y/height.</li>
</ul>

<h3>The CSS</h3>

<p>As you may know, SproutCore automatically includes any CSS files. So, since this generates CSS files (and, if you provide the right argument, puts them directly into a SproutCore-watched folder), the CSS is automatically added. So, how do you use it?</p>

<p>Very simply. The script generates CSS rules in this format:</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.set-name</span> <span style="color: #6666ff;">.icon-name</span><span style="color: #6666ff;">.icon</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.set-name</span> <span style="color: #6666ff;">.icon-name</span><span style="color: #6666ff;">.icon</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/* CSS */</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>


<p>For example, to show &#8220;refresh-64&#8243; icon in set &#8220;common&#8221;, you&#8217;d do something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ImageView.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  value<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;common refresh-64 icon&quot;</span> <span style="color: #006600; font-style: italic;">// using SproutCore's built-in className support for spriting</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>Or, as you may have noticed from the rule above, you can accomplish some basic theming very easily by treating set names as themes:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">View.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span><span style="color: #CC0000;">256</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">256</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;common&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// the theme</span>
  childViews<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;styledView&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  styledView<span style="color: #339933;">:</span> ImageView.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    value<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;refresh-64 icon&quot;</span> <span style="color: #006600; font-style: italic;">// using SproutCore's built-in className support for spriting</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>


<p>Nice and simple, eh?</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/10/17/sprouting%e2%80%94automated-spriting-for-sproutcore/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Running Orbited (or other Twisted Services) as a Windows Service</title>
		<link>http://create.tpsitulsa.com/blog/2009/08/04/running-orbited-or-other-twisted-services-as-a-service/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/08/04/running-orbited-or-other-twisted-services-as-a-service/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 14:47:15 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Comet]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=185</guid>
		<description><![CDATA[It took me awhile to figure out how to run Orbited and Dolores as Windows Services. Unfortunately, while I love Mac, almost every other computer at my workplace is Windows, including the servers. When deploying my web application, I can&#8217;t really just leave instructions like: &#8220;When you restart the server, please run these two scripts.&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>It took me awhile to figure out how to run <a href="http://orbited.org">Orbited</a> and <a href="http://create.tpsitulsa.com/blog/2009/08/04/dolores/">Dolores</a> as Windows Services. Unfortunately, while I love Mac, almost every other computer at my workplace is Windows, including the servers. When deploying my web application, I can&#8217;t really just leave instructions like: &#8220;When you restart the server, please run these two scripts.&#8221;</p>

<p>They need to be Windows Services.</p>

<p>Thankfully, it isn&#8217;t actually too hard. You just need a couple of Python packages (which I actually don&#8217;t remember installing, so they may have come with Twisted or Python), and then all you have to do is write a couple of very simple scripts. Here are mine:</p>

<p><strong>Orbited Service</strong></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> OrbitedManager <span style="color: #808080; font-style: italic;"># a copy of orbited.start with some very minor modifications, which I'll include after the break</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Service Utilities</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32serviceutil
<span style="color: #ff7700;font-weight:bold;">import</span> win32service
<span style="color: #ff7700;font-weight:bold;">import</span> win32event
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> WindowsService<span style="color: black;">&#40;</span>win32serviceutil.<span style="color: black;">ServiceFramework</span><span style="color: black;">&#41;</span>:
	_svc_name_ = <span style="color: #483d8b;">&quot;Orbited&quot;</span>
	_svc_display_name_ = <span style="color: #483d8b;">&quot;Orbited Server&quot;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, args<span style="color: black;">&#41;</span>:
		win32serviceutil.<span style="color: black;">ServiceFramework</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, args<span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span> = win32event.<span style="color: black;">CreateEvent</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> SvcStop<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">ReportServiceStatus</span><span style="color: black;">&#40;</span>win32service.<span style="color: black;">SERVICE_STOP_PENDING</span><span style="color: black;">&#41;</span>
		win32event.<span style="color: black;">SetEvent</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> SvcDoRun<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">import</span> servicemanager
		OrbitedManager.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
			retval = win32event.<span style="color: black;">WaitForSingleObject</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> retval == win32event.<span style="color: black;">WAIT_TIMEOUT</span>:
				OrbitedManager.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">break</span>
				<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5.0</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">'__main__'</span>:
	win32serviceutil.<span style="color: black;">HandleCommandLine</span><span style="color: black;">&#40;</span>WindowsService<span style="color: black;">&#41;</span></pre></div></div>


<p><strong>Dolores Service</strong></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Dolores
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Service Utilities</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32serviceutil
<span style="color: #ff7700;font-weight:bold;">import</span> win32service
<span style="color: #ff7700;font-weight:bold;">import</span> win32event
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> WindowsService<span style="color: black;">&#40;</span>win32serviceutil.<span style="color: black;">ServiceFramework</span><span style="color: black;">&#41;</span>:
	_svc_name_ = <span style="color: #483d8b;">&quot;Dolores&quot;</span>
	_svc_display_name_ = <span style="color: #483d8b;">&quot;Dolores Server&quot;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, args<span style="color: black;">&#41;</span>:
		win32serviceutil.<span style="color: black;">ServiceFramework</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, args<span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span> = win32event.<span style="color: black;">CreateEvent</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> SvcStop<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">ReportServiceStatus</span><span style="color: black;">&#40;</span>win32service.<span style="color: black;">SERVICE_STOP_PENDING</span><span style="color: black;">&#41;</span>
		win32event.<span style="color: black;">SetEvent</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> SvcDoRun<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">import</span> servicemanager
		Dolores.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
			retval = win32event.<span style="color: black;">WaitForSingleObject</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> retval == win32event.<span style="color: black;">WAIT_TIMEOUT</span>:
				Dolores.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">break</span>
				<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5.0</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">'__main__'</span>:
	win32serviceutil.<span style="color: black;">HandleCommandLine</span><span style="color: black;">&#40;</span>WindowsService<span style="color: black;">&#41;</span></pre></div></div>


<p>That&#8217;s the basics, but as you may have noticed, I also changed Orbited&#8217;s start script. I did this because the original did not play nicely with threads.</p>

<p>Instead of changing it in its original location in the filesystem, I created a copy of it in the local directory (as you can see, I&#8217;m <em>not</em> importing Orbited.OrbitedManager).</p>

<p><span id="more-185"></span>
Before overwhelming you with a wall of code, I&#8217;ll explain what I changed. It is actually quite simple:</p>

<ol>
    <li>First, some imports at the top:


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> orbited
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">threading</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span></pre></div></div>


</li>
    <li>Since the file is no longer &#8220;start.py&#8221; in the Orbited package directory, the static files path needs changing (just find the line and change it):


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">static_files = static.<span style="color: black;">File</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>orbited.__path__<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'orbited/static'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>



</li>
<li>And, since we&#8217;re going to start the reactor manually, we need to <strong>remove</strong> the auto-start portion:


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># REMOVE THIS PART:</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> options.<span style="color: #dc143c;">profile</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">hotshot</span>
        prof = <span style="color: #dc143c;">hotshot</span>.<span style="color: black;">Profile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;orbited.profile&quot;</span><span style="color: black;">&#41;</span>
        logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;running Orbited in profile mode&quot;</span><span style="color: black;">&#41;</span>
        logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;for information on profiler, see http://orbited.org/wiki/Profiler&quot;</span><span style="color: black;">&#41;</span>
        prof.<span style="color: black;">runcall</span><span style="color: black;">&#40;</span>reactor.<span style="color: black;">run</span><span style="color: black;">&#41;</span>
        prof.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>



</li>

    <li>Finally, I added a bit to the bottom, replacing the lines that call &#8220;main()&#8221;. It arranges to run Orbited (or any Twisted service) in a separate thread:


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> thread_start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># initializes the reactor</span>
	<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
	reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span>installSignalHandlers=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Crashed into Earth's Surface.&quot;</span>
&nbsp;
orbited_thread = <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Starting Orbited...&quot;</span>
	orbited_thread = <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#40;</span>target=thread_start<span style="color: black;">&#41;</span>
	orbited_thread.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;In Orbit.&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop_reactor<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">callFromThread</span><span style="color: black;">&#40;</span>stop_reactor<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
	start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	run = <span style="color: #008000;">True</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> run:
		<span style="color: #ff7700;font-weight:bold;">try</span>:
			<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyboardInterrupt</span>:
			<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
			stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
			run = <span style="color: #008000;">False</span></pre></div></div>



</li></ol>

<p><strong>The Raw OrbitedManager.py File</strong></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urlparse</span>
<span style="color: #ff7700;font-weight:bold;">from</span> orbited <span style="color: #ff7700;font-weight:bold;">import</span> __version__ <span style="color: #ff7700;font-weight:bold;">as</span> version
<span style="color: #ff7700;font-weight:bold;">from</span> orbited <span style="color: #ff7700;font-weight:bold;">import</span> config
<span style="color: #ff7700;font-weight:bold;">from</span> orbited <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> orbited
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">threading</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># NB: this is set after we load the configuration at &quot;main&quot;.</span>
logger = <span style="color: #008000;">None</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _import<span style="color: black;">&#40;</span>name<span style="color: black;">&#41;</span>:
    module_import = name.<span style="color: black;">rsplit</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #008000;">getattr</span>, name.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>, <span style="color: #008000;">__import__</span><span style="color: black;">&#40;</span>module_import<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _setup_protocols<span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
    protocols = <span style="color: black;">&#91;</span>
        <span style="color: #808080; font-style: italic;">#child_path config_key  port_class_import,              factory_class_import</span>
        <span style="color: black;">&#40;</span><span style="color: #483d8b;">'tcp'</span>,     <span style="color: #483d8b;">'proxy'</span>,    <span style="color: #483d8b;">'orbited.cometsession.Port'</span>,    <span style="color: #483d8b;">'orbited.proxy.ProxyFactory'</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> child_path, config_key, port_class_import, factory_class_import <span style="color: #ff7700;font-weight:bold;">in</span> protocols:
        <span style="color: #ff7700;font-weight:bold;">if</span> config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[global]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s.enabled'</span> <span style="color: #66cc66;">%</span> config_key<span style="color: black;">&#41;</span> == <span style="color: #483d8b;">'1'</span>:
            port_class = _import<span style="color: black;">&#40;</span>port_class_import<span style="color: black;">&#41;</span>
            factory_class = _import<span style="color: black;">&#40;</span>factory_class_import<span style="color: black;">&#41;</span>
            reactor.<span style="color: black;">listenWith</span><span style="color: black;">&#40;</span>port_class, factory=factory_class<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #dc143c;">resource</span>=root, childName=child_path<span style="color: black;">&#41;</span>
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s protocol active'</span> <span style="color: #66cc66;">%</span> config_key<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _setup_static<span style="color: black;">&#40;</span>root, config<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> static
    <span style="color: #ff7700;font-weight:bold;">for</span> key, val <span style="color: #ff7700;font-weight:bold;">in</span> config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'[static]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> key == <span style="color: #483d8b;">'INDEX'</span>:
            key = <span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> root.<span style="color: black;">getStaticEntity</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>:
            logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cannot mount static directory with reserved name %s&quot;</span> <span style="color: #66cc66;">%</span> key<span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        root.<span style="color: black;">putChild</span><span style="color: black;">&#40;</span>key, static.<span style="color: black;">File</span><span style="color: black;">&#40;</span>val<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> twisted
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Orbited requires Twisted, which is not installed. See http://twistedmatrix.com/trac/ for installation instructions.&quot;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#################</span>
    <span style="color: #808080; font-style: italic;"># This corrects a bug in Twisted 8.2.0 for certain Python 2.6 builds on Windows</span>
    <span style="color: #808080; font-style: italic;">#   Twisted ticket: http://twistedmatrix.com/trac/ticket/3868</span>
    <span style="color: #808080; font-style: italic;">#     -mario</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">python</span> <span style="color: #ff7700;font-weight:bold;">import</span> lockfile
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
        <span style="color: #ff7700;font-weight:bold;">from</span> orbited <span style="color: #ff7700;font-weight:bold;">import</span> __path__ <span style="color: #ff7700;font-weight:bold;">as</span> orbited_path
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>orbited_path<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,<span style="color: #483d8b;">&quot;hotfixes&quot;</span>,<span style="color: #483d8b;">&quot;win32api&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">python</span> <span style="color: #ff7700;font-weight:bold;">import</span> lockfile
        lockfile.<span style="color: black;">kill</span> = <span style="color: #008000;">None</span>
    <span style="color: #808080; font-style: italic;">#################</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser
    <span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">&quot;-c&quot;</span>,
        <span style="color: #483d8b;">&quot;--config&quot;</span>,
        dest=<span style="color: #483d8b;">&quot;config&quot;</span>,
        default=<span style="color: #008000;">None</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;path to configuration file&quot;</span>
    <span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">&quot;-v&quot;</span>,
        <span style="color: #483d8b;">&quot;--version&quot;</span>,
        dest=<span style="color: #483d8b;">&quot;version&quot;</span>,
        action=<span style="color: #483d8b;">&quot;store_true&quot;</span>,
        default=<span style="color: #008000;">False</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;print Orbited version&quot;</span>
    <span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">&quot;-p&quot;</span>,
        <span style="color: #483d8b;">&quot;--profile&quot;</span>,
        dest=<span style="color: #483d8b;">&quot;profile&quot;</span>,
        action=<span style="color: #483d8b;">&quot;store_true&quot;</span>,
        default=<span style="color: #008000;">False</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;run Orbited with a profiler&quot;</span>
    <span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">&quot;-q&quot;</span>,
        <span style="color: #483d8b;">&quot;--quickstart&quot;</span>,
        dest=<span style="color: #483d8b;">&quot;quickstart&quot;</span>,
        action=<span style="color: #483d8b;">&quot;store_true&quot;</span>,
        default=<span style="color: #008000;">False</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;run Orbited on port 8000 and MorbidQ on port 61613&quot;</span>
    <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> args:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'the &quot;orbited&quot; command does not accept positional arguments. type &quot;orbited -h&quot; for options.'</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> options.<span style="color: black;">version</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Orbited version: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>version,<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> options.<span style="color: black;">quickstart</span>:
        config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[listen]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://:8000'</span><span style="color: black;">&#41;</span>
        config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[listen]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'stomp://:61613'</span><span style="color: black;">&#41;</span>
        config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[access]'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'localhost'</span>,<span style="color: #ff4500;">61613</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'*'</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Quickstarting Orbited&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #808080; font-style: italic;"># load configuration from configuration</span>
        <span style="color: #808080; font-style: italic;"># file and from command line arguments.</span>
        config.<span style="color: black;">setup</span><span style="color: black;">&#40;</span>options=options<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #dc143c;">logging</span>.<span style="color: black;">setup</span><span style="color: black;">&#40;</span>config.<span style="color: #008000;">map</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># we can now safely get loggers.</span>
    <span style="color: #ff7700;font-weight:bold;">global</span> logger<span style="color: #66cc66;">;</span> logger = <span style="color: #dc143c;">logging</span>.<span style="color: black;">get_logger</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'orbited.start'</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;"># NB: we need to install the reactor before using twisted.</span>
    reactor_name = config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[global]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'reactor'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> reactor_name:
        install = _import<span style="color: black;">&#40;</span><span style="color: #483d8b;">'twisted.internet.%sreactor.install'</span> <span style="color: #66cc66;">%</span> reactor_name<span style="color: black;">&#41;</span>
        install<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'using %s reactor'</span> <span style="color: #66cc66;">%</span> reactor_name<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">############</span>
    <span style="color: #808080; font-style: italic;"># This crude garbage corrects a bug in twisted</span>
    <span style="color: #808080; font-style: italic;">#   Orbited ticket: http://orbited.org/ticket/111</span>
    <span style="color: #808080; font-style: italic;">#   Twisted ticket: http://twistedmatrix.com/trac/ticket/2447</span>
    <span style="color: #ff7700;font-weight:bold;">import</span> twisted.<span style="color: black;">web</span>.<span style="color: black;">http</span>
    twisted.<span style="color: black;">web</span>.<span style="color: black;">http</span>.<span style="color: black;">HTTPChannel</span>.<span style="color: black;">setTimeout</span> = <span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">self</span>, arg: <span style="color: #008000;">None</span>
    twisted.<span style="color: black;">web</span>.<span style="color: black;">http</span>.<span style="color: black;">HTTPChannel</span>.<span style="color: black;">resetTimeout</span> = <span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">self</span>: <span style="color: #008000;">None</span>
    <span style="color: #808080; font-style: italic;">############</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">resource</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> server
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> static
    <span style="color: #ff7700;font-weight:bold;">import</span> orbited.<span style="color: black;">system</span>
&nbsp;
    root = <span style="color: #dc143c;">resource</span>.<span style="color: black;">Resource</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    static_files = static.<span style="color: black;">File</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>orbited.__path__<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'orbited/static'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    root.<span style="color: black;">putChild</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'static'</span>, static_files<span style="color: black;">&#41;</span>
    root.<span style="color: black;">putChild</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'system'</span>, orbited.<span style="color: black;">system</span>.<span style="color: black;">SystemResource</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[test]'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'stompdispatcher.enabled'</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'1'</span>:
        logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'stompdispatcher enabled'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#static_files.putChild('orbited.swf', static.File(os.path.join(os.path.dirname(__file__), 'flash', 'orbited.swf')))</span>
    <span style="color: #dc143c;">site</span> = server.<span style="color: black;">Site</span><span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>
&nbsp;
    _setup_protocols<span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>
    _setup_static<span style="color: black;">&#40;</span>root, config.<span style="color: #008000;">map</span><span style="color: black;">&#41;</span>
    start_listening<span style="color: black;">&#40;</span><span style="color: #dc143c;">site</span>, config.<span style="color: #008000;">map</span>, logger<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># switch uid and gid to configured user and group.</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">name</span> == <span style="color: #483d8b;">'posix'</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">getuid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
        <span style="color: #dc143c;">user</span> = config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[global]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'user'</span><span style="color: black;">&#41;</span>
        group = config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[global]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'group'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">user</span>:
            <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">pwd</span>
            <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">grp</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                pw = <span style="color: #dc143c;">pwd</span>.<span style="color: black;">getpwnam</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span><span style="color: black;">&#41;</span>
                uid = pw.<span style="color: black;">pw_uid</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> group:
                    gr = <span style="color: #dc143c;">grp</span>.<span style="color: black;">getgrnam</span><span style="color: black;">&#40;</span>group<span style="color: black;">&#41;</span>
                    gid = gr.<span style="color: black;">gr_gid</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    gid = pw.<span style="color: black;">pw_gid</span>
                    gr = <span style="color: #dc143c;">grp</span>.<span style="color: black;">getgrgid</span><span style="color: black;">&#40;</span>gid<span style="color: black;">&#41;</span>
                    group = gr.<span style="color: black;">gr_name</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
                logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Aborting; Unknown user or group: %s'</span> <span style="color: #66cc66;">%</span> e<span style="color: black;">&#41;</span>
                <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'switching to user %s (uid=%d) and group %s (gid=%d)'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>, uid, group, gid<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">os</span>.<span style="color: black;">setgid</span><span style="color: black;">&#40;</span>gid<span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">os</span>.<span style="color: black;">setuid</span><span style="color: black;">&#40;</span>uid<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Aborting; You must define a user (and optionally a group) in the configuration file.'</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> start_listening<span style="color: black;">&#40;</span><span style="color: #dc143c;">site</span>, config, logger<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> protocol <span style="color: #ff7700;font-weight:bold;">as</span> protocol_module
    <span style="color: #808080; font-style: italic;"># allow stomp:// URIs to be parsed by urlparse</span>
    <span style="color: #dc143c;">urlparse</span>.<span style="color: black;">uses_netloc</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;stomp&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># allow test server URIs to be parsed by urlparse</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> orbited.<span style="color: black;">servers</span> <span style="color: #ff7700;font-weight:bold;">import</span> test_servers
    <span style="color: #ff7700;font-weight:bold;">for</span> protocol <span style="color: #ff7700;font-weight:bold;">in</span> test_servers:
        <span style="color: #dc143c;">urlparse</span>.<span style="color: black;">uses_netloc</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>protocol<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> addr <span style="color: #ff7700;font-weight:bold;">in</span> config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'[listen]'</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> addr.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;stomp&quot;</span><span style="color: black;">&#41;</span>:
            stompConfig = <span style="color: #483d8b;">&quot;&quot;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">&quot; &quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> addr:
                addr, stompConfig = addr.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; &quot;</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        url = <span style="color: #dc143c;">urlparse</span>.<span style="color: #dc143c;">urlparse</span><span style="color: black;">&#40;</span>addr<span style="color: black;">&#41;</span>
        hostname = url.<span style="color: black;">hostname</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> url.<span style="color: black;">scheme</span> == <span style="color: #483d8b;">'stomp'</span>:
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Listening stomp@%s'</span> <span style="color: #66cc66;">%</span> url.<span style="color: black;">port</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">from</span> morbid <span style="color: #ff7700;font-weight:bold;">import</span> get_stomp_factory
            morbid_instance = get_stomp_factory<span style="color: black;">&#40;</span>stompConfig<span style="color: black;">&#41;</span>
            config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'morbid_instance'</span><span style="color: black;">&#93;</span> = morbid_instance
            reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, morbid_instance, interface=hostname<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> url.<span style="color: black;">scheme</span> == <span style="color: #483d8b;">'http'</span>:
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Listening http@%s'</span> <span style="color: #66cc66;">%</span> url.<span style="color: black;">port</span><span style="color: black;">&#41;</span>
            reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, <span style="color: #dc143c;">site</span>, interface=hostname<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> url.<span style="color: black;">scheme</span> == <span style="color: #483d8b;">'https'</span>:
            <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> ssl
            crt = config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'[ssl]'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'crt'</span><span style="color: black;">&#93;</span>
            key = config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'[ssl]'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'key'</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                ssl_context = ssl.<span style="color: black;">DefaultOpenSSLContextFactory</span><span style="color: black;">&#40;</span>key, crt<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
                <span style="color: #ff7700;font-weight:bold;">raise</span>
            <span style="color: #ff7700;font-weight:bold;">except</span>:
                logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Error opening key or crt file: %s, %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>key, crt<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Listening https@%s (%s, %s)'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, key, crt<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            reactor.<span style="color: black;">listenSSL</span><span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, <span style="color: #dc143c;">site</span>, ssl_context, interface=hostname<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> url.<span style="color: black;">scheme</span> <span style="color: #ff7700;font-weight:bold;">in</span> test_servers:
            test_factory = protocol_module.<span style="color: black;">ServerFactory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            test_factory.<span style="color: black;">protocol</span> = test_servers<span style="color: black;">&#91;</span>url.<span style="color: black;">scheme</span><span style="color: black;">&#93;</span>
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Listening %s@%s&quot;</span><span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span>url.<span style="color: black;">scheme</span>, url.<span style="color: black;">port</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, test_factory<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> url.<span style="color: black;">scheme</span> == <span style="color: #483d8b;">'monitor'</span>:
                config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'globalVars'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'monitoring'</span><span style="color: black;">&#93;</span> = url.<span style="color: black;">port</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Invalid Listen URI: %s&quot;</span> <span style="color: #66cc66;">%</span> addr<span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> thread_start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
	reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span>installSignalHandlers=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Crashed into Earth's Surface.&quot;</span>
&nbsp;
&nbsp;
orbited_thread = <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Starting Orbited...&quot;</span>
	dolores_thread = <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#40;</span>target=thread_start<span style="color: black;">&#41;</span>
	dolores_thread.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;In Orbit.&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop_reactor<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">callFromThread</span><span style="color: black;">&#40;</span>stop_reactor<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
	start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	run = <span style="color: #008000;">True</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> run:
		<span style="color: #ff7700;font-weight:bold;">try</span>:
			<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyboardInterrupt</span>:
			<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
			stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
			run = <span style="color: #008000;">False</span></pre></div></div>


<h3>So, What Do I Do?</h3>

<p>You should just need to put the windows service script and the OrbitedManager.py script in the same directory, and then run the windows service script from the command line. If you run it with no arguments, I <em>believe</em> it will give you an overview of available options, including the option to install.</p>

<p>I hope someone can find this helpful.</p>

<p><strong>Update: </strong> I added some clarification in the middle, discussing high-level about the replacement of Orbited&#8217;s <em>start</em> module, and a bit at the end about <em>what to do</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/08/04/running-orbited-or-other-twisted-services-as-a-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python, Windows, and Network</title>
		<link>http://create.tpsitulsa.com/blog/2009/04/16/python-windows-and-network/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/04/16/python-windows-and-network/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 19:31:00 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[USPS]]></category>
		<category><![CDATA[Barcode]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=123</guid>
		<description><![CDATA[We have several machines. All of them (except for mine, a MacBook Pro which I use for almost everything) run Windows. I&#8217;ve been learning Python. It is a really useful language. It runs on the command line, and that is really important. If I could, I&#8217;d use JavaScript instead, but some things &#8212; such as [...]]]></description>
			<content:encoded><![CDATA[<p>We have several machines. All of them (except for mine, a MacBook Pro which I use for almost everything) run Windows.</p>

<p>I&#8217;ve been learning Python. It is a really useful language. It runs on the command line, and that is really important. If I could, I&#8217;d use JavaScript instead, but some things &mdash; such as connecting to MySQL servers &mdash; are a bit much for me to write JavaScript shells for at the moment (especially because it has been a year or three since I&#8217;ve dealt with the C++ MySQL APIs).</p>

<p>So, for a recent diversion which required MySQL database access, I decided to use Python. If I can remember, I&#8217;ll write about the project later. It was a simple script to keep track of sequence numbers for the new <a href="http://en.wikipedia.org/wiki/Intelligent_Mail_Barcode">Intelligent Mail Barcode</a>, so that we could ensure each piece had a unique number in any 45-day period.</p>

<p>Windows does not come with Python pre-installed. This is not usually a problem, as Python is not at all difficult to install. But in addition to having to install Python, I&#8217;d have to install the Python package for MySQL. This Python script could be used from several computers, so I&#8217;d have to do in on each computer. Even if I posted an article on our intra-office wiki (which it <em>still</em> seems like nobody reads), I would probably have to end up helping everyone install it.</p>

<p>And, what if I make a mistake? Having so many installations (at least 3 or 4, possibly more, and prone to change whenever we replace or add a computer) could make me very busy with things I don&#8217;t really want to be busy with.</p>

<p>What would be best is if I could install Python on a central server. Well, I couldn&#8217;t do that, but I could install Python on my Windows computer (which, alas, I still have to use sometimes), and figure out what I needed to copy to the server.</p>

<p>It is really quite simple in principle: just install Python on Windows. Copy <tt>C:\Python25</tt> (or whatever your Python install directory is) to your central server.</p>

<p>Except&#8230; <strong>it still won&#8217;t run on the other computers.</strong></p>

<p>It complains about missing a Python25.dll. It would seem that the Python installer was stupid enough to not install its DLLs in the install folder along with Python itself, but instead litter the already-overpopulated <tt>C:\Windows\System32</tt> directory with them.</p>

<p>So, I found all of the DLLs that looked like they belonged to Python 2.5, and copied them into the Python25 directory on the central server:</p>

<ul>
<li>python25.dll</li>
<li>pythoncom25.dll</li>
<li>pywintypes25.dll</li>
</ul>

<p>Could I have missed some? Perhaps. But everything appeared to work.</p>

<p>I still had one complication: it would be nice if scripts could run without the command needing to say <tt>r:\path\to\Python25\python</tt>. For this, each computer would need to have Python&#8217;s path in their PATH system variable.</p>

<p>Instead of adding yet another item to PATH, however, I decided to make a simple command file that would &#8220;initialize&#8221; Python by adding it to the instance&#8217;s PATH. This would only last the duration of the Command window.</p>

<p>Here is the script, changed only slightly:</p>


<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> off
&nbsp;
<span style="color: #00b100; font-weight: bold;">IF</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">INITIALIZED_PYTHON_25</span><span style="color: #33cc33;">%</span>&quot;==&quot;YES&quot; <span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">END</span>
&nbsp;
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PYTHONPATH</span>=R:\path\to\python\python25\
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PATH</span>=R:\path\to\python\python25\;<span style="color: #33cc33;">%</span><span style="color: #448888;">PATH</span><span style="color: #33cc33;">%</span>
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">INITIALIZED_PYTHON_25</span>=YES
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">END</span></pre></div></div>


<p>And that&#8217;s it. As the script I was working on (and likely any others in future) would be run from batch files anyway, they&#8217;d just need an extra line at the beginning to call this command. And that&#8217;s it.</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/04/16/python-windows-and-network/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
