<?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>swag.dk &#187; Programming</title>
	<atom:link href="http://swag.dk/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://swag.dk/blog</link>
	<description></description>
	<lastBuildDate>Wed, 21 Jul 2010 12:34:40 +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>Managing multiple AWS identities</title>
		<link>http://swag.dk/blog/2009/07/22/managing-multiple-aws-identities/</link>
		<comments>http://swag.dk/blog/2009/07/22/managing-multiple-aws-identities/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 14:50:12 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=167</guid>
		<description><![CDATA[I&#8217;m running multiple different project on AWS which was so much of a pain to use, as I often find myself having to use the identity of project-a together with the official amazon ec2 tools. To help myself manage the &#8230; <a href="http://swag.dk/blog/2009/07/22/managing-multiple-aws-identities/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m running multiple different project on <a href="http://aws.amazon.com/">AWS</a> which was so much of a pain to use, as I often find myself having to use the identity of project-a together with the official amazon ec2 tools.</p>

<p>To help myself manage the multiple identities, I wote a set of bash functions, called:</p>

<ul>
<li><code>aws_load &lt;config-name&gt;</code> &#8211; loads configuration from config-name</li>
<li><code>ec2ssh &lt;instance-number-in-ec2din-list&gt;</code> &#8211; ssh&#8217;s into a given instance, with the root key</li>
<li><code>ec2scp</code> &#8211; a shorthand for scp -i &lt;keyfile&gt;
<!-- more --></li>
</ul>

<p>I keep the configuration files in the directory <code>~/amazon/conf/name.sh</code> and keypairs in <code>~/amazon/keypairs/</code> but that should be obvious to change.</p>

<p>To change or load an identity, one simply calls the function from a shell prompt like so:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mads<span style="color: #000000; font-weight: bold;">@</span>workmads ~ <span style="color: #000000; font-weight: bold;">%</span> aws_load some-identity
loaded certificate ...
loaded <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>mads<span style="color: #000000; font-weight: bold;">/</span>amazon<span style="color: #000000; font-weight: bold;">/</span>conf<span style="color: #000000; font-weight: bold;">/</span>some-identity.sh <span style="color: #7a0874; font-weight: bold;">&#40;</span>...<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>


<p>I hope someone finds this as useful as I do.</p>

<p>Functions (could be placed in <code>.bashrc</code> or <code>.zshrc</code>).</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> aws_load <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
		<span style="color: #007800;">ec2_configurations</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/amazon/conf&quot;</span>
		<span style="color: #007800;">ec2_keys</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/amazon/keypairs&quot;</span>
		<span style="color: #007800;">conf</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$ec2_configurations</span>/$1.sh&quot;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-x</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$conf</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
			<span style="color: #7a0874; font-weight: bold;">unset</span> AMAZON_ID AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_CERT EC2_PRIVATE_KEY EC2_CERT AWS_KEYPAIR_NAME
&nbsp;
			<span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #007800;">$conf</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$AWS_KEYPAIR_NAME</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
				<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_SSH_KEY</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$ec2_keys</span>/id_rsa_<span style="color: #007800;">${AWS_KEYPAIR_NAME}</span>-keypair&quot;</span>
			<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$AWS_CERT</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
				<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">EC2_PRIVATE_KEY</span>=~<span style="color: #000000; font-weight: bold;">/</span>.ec2<span style="color: #000000; font-weight: bold;">/</span>pk-<span style="color: #007800;">$AWS_CERT</span>.pem
				<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">EC2_CERT</span>=~<span style="color: #000000; font-weight: bold;">/</span>.ec2<span style="color: #000000; font-weight: bold;">/</span>cert-<span style="color: #007800;">$AWS_CERT</span>.pem
&nbsp;
				<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;loaded certificate <span style="color: #007800;">$AWS_CERT</span>&quot;</span>
			<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
			<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;loaded <span style="color: #007800;">$conf</span> (<span style="color: #007800;">$AMAZON_ID</span>)&quot;</span>
		<span style="color: #000000; font-weight: bold;">else</span>
			<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;configuration <span style="color: #007800;">$conf</span> not found (or not executable)&quot;</span>
		<span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;usage: aws_load &lt;configuration name&gt;&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> ec2ssh <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #007800;">HOST</span>=<span style="color: #ff0000;">&quot;<span style="color: #780078;">`ec2din | awk '/i-/ {print $4}' | tail +$1 | head -n 1`</span>&quot;</span>
        <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-i</span> <span style="color: #007800;">$AWS_SSH_KEY</span> <span style="color: #660033;">-l</span> root <span style="color: #800000;">${HOST}</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Please write a number&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> ec2scp <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #c20cb9; font-weight: bold;">scp</span> <span style="color: #660033;">-i</span> <span style="color: #007800;">$AWS_SSH_KEY</span> $<span style="color: #000000; font-weight: bold;">@</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>


<p>Configuration &#8220;file&#8221; template to be placed in <code>~/amazon/conf/&lt;config-name&gt;.sh</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AMAZON_ID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_ACCESS_KEY_ID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_SECRET_ACCESS_KEY</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_CERT</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_KEYPAIR_NAME</span>=<span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>


<p>Happy identity switching.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/07/22/managing-multiple-aws-identities/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Django &#8211; sharing a memcached instance</title>
		<link>http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/</link>
		<comments>http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 11:23:00 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[prefix]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=135</guid>
		<description><![CDATA[Update: Some Curious User brought to my attention, that a ticket has been opened which, when implemented, will add a setting for a cache prefix. It will also allow other cache key manipulations. Until recently I&#8217;ve been using the file:// &#8230; <a href="http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong>
Some <a href="http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/comment-page-1/#comment-812">Curious User</a> brought to my attention, that <a href="http://code.djangoproject.com/ticket/13795">a ticket</a> has been opened which, when implemented, will add a setting for a cache prefix. It will also allow other cache key manipulations.</p>

<p>Until recently I&#8217;ve been using the <code>file://</code> django cache, but that has a &#8220;problem&#8221; when multiple users needs to manipulate the cache (think uid 80 writes a key, that uid 1000 wants to delete).</p>

<p>My problem with the <code>memcached://</code> django cache provider has been, that it cannot handle being used on a shared memcached instance, because of the danger of key collissions.</p>

<p><span id="more-135"></span>
If project A and project B would share a memcached instance, they basiclly share the same global namespace. So if they both write a key called <code>actor</code> there is no telling what will happen.</p>

<p>So I wrote a little cache backend for django, that uses the current <code>memcached</code> backend, but adds a pre-defined prefix to all keys.</p>

<p><strong>Usage:</strong>
Put the code somewhere inside your project in a file called <code>memcached&#95;key&#95;prefix.py</code>, and set your <code>CACHE&#95;BACKEND</code> to something like: <code>path.to.memcached&#95;key&#95;prefix:///127.0.0.1:11211/?key&#95;prefix=sewc&#95;&amp;foo=bar&amp;timeout=3600</code></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;Memcached cache backend with key prefixing&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">cache</span>.<span style="color: black;">backends</span>.<span style="color: black;">base</span> <span style="color: #ff7700;font-weight:bold;">import</span> InvalidCacheBackendError
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">cache</span>.<span style="color: black;">backends</span>.<span style="color: black;">memcached</span> <span style="color: #ff7700;font-weight:bold;">import</span> CacheClass <span style="color: #ff7700;font-weight:bold;">as</span> MemcachedCacheClass
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">utils</span>.<span style="color: black;">encoding</span> <span style="color: #ff7700;font-weight:bold;">import</span> smart_unicode, smart_str
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CacheClass<span style="color: black;">&#40;</span>MemcachedCacheClass<span style="color: black;">&#41;</span>:
    <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>, server, params<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #008000;">self</span>._key_prefix = smart_str<span style="color: black;">&#40;</span>params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'key_prefix'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyError</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> InvalidCacheBackendError<span style="color: black;">&#40;</span><span style="color: #483d8b;">'key_prefix not specified'</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span>server, params<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _get_key<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._key_prefix + smart_str<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> add<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, value, timeout=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, value, timeout<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, default=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, default<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, value, timeout=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, value, timeout<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> delete<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">delete</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_many<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, keys<span style="color: black;">&#41;</span>:
        keys = <span style="color: black;">&#91;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> key <span style="color: #ff7700;font-weight:bold;">in</span> keys<span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">get_many</span><span style="color: black;">&#40;</span>keys<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> incr<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, delta=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">incr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, delta<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> decr<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, delta=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">decr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, delta<span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Django compatible PyAMF test client</title>
		<link>http://swag.dk/blog/2009/04/21/django-compatible-pyamf-test-client/</link>
		<comments>http://swag.dk/blog/2009/04/21/django-compatible-pyamf-test-client/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 13:18:19 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Client]]></category>
		<category><![CDATA[PyAMF]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=92</guid>
		<description><![CDATA[While working on a project using PyAMF today, i was about to write a unittest of a service method, when I realized that it would be harder than necessary to make unittests of the service. The cause of this is, &#8230; <a href="http://swag.dk/blog/2009/04/21/django-compatible-pyamf-test-client/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While working on a project using <a href="http://pyamf.org/">PyAMF</a> today, i was about to write a unittest of a service method, when I realized that it would be harder than necessary to make unittests of the service. The cause of this is, that there was no test client wrapper for the django test client for pyamf, which means, that to do a unittest of the gateway, you&#8217;ll have to start a django server, and run a unittest elsewhere.</p>

<p>Seeing as how there are a lot of benefits to using djangos own test suite (fixtures and automatic database generation to name a few), i set out to write a little test client for PyAMF to utilize django&#8217;s test client, so it would be possible to write a proper test suite.</p>

<p>This turned out great, and is now <a href="http://pyamf.org/ticket/508">ticket 508</a> over at PyAMFs trac. Look at the <a href="http://pyamf.org/attachment/ticket/508/client.py">client.py</a> for the code. At some point, it will be integrated into PyAMF mainline as <code>p.r.c.django.TestClient</code> (or something like that).</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/04/21/django-compatible-pyamf-test-client/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Choosing a font in Carbon Emacs</title>
		<link>http://swag.dk/blog/2009/04/18/choosing-a-font-in-carbon-emacs/</link>
		<comments>http://swag.dk/blog/2009/04/18/choosing-a-font-in-carbon-emacs/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 22:30:30 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Change font]]></category>
		<category><![CDATA[Emacs]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=93</guid>
		<description><![CDATA[It took me a while of messing with fontsets and various other settings, before i came across how to select fonts in the Emacs wiki: M-x mac-font-panel-mode and pick the font you want. M-x describe-font and copy the font name &#8230; <a href="http://swag.dk/blog/2009/04/18/choosing-a-font-in-carbon-emacs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It took me a while of messing with fontsets and various other settings, before i came across <a href="http://www.emacswiki.org/emacs/SetFonts#toc6">how to select fonts</a> in the <a href="http://emacswiki.org/">Emacs wiki</a>:</p>

<ol>
<li>M-x mac-font-panel-mode and pick the font you want.</li>
<li>M-x describe-font and copy the font name (e.g. <code>“-apple-inconsolata-medium-r-normal--13-130-72-72-m-130-iso10646-1”</code>)</li>
<li>Add the following to your <code>.emacs</code> file:
<code>(set-default-font
 "-apple-inconsolata-medium-r-normal--13-130-72-72-m-130-iso10646-1")</code></li>
</ol>

<p>In my case the lines for font selection looks like this:</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>set-face-font 'default <span style="color: #ff0000;">&quot;-apple-proggytiny-medium-r-normal--11-110-72-72-m-110-iso10646-1&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>Using this to make the font display without antialisaing:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">defaults <span style="color: #c20cb9; font-weight: bold;">write</span> org.gnu.Emacs AppleAntiAliasingThreshold <span style="color: #000000;">100</span></pre></div></div>


<p>And this to position emacs in the top left, and be 104 chars wide, and 64 high.</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> initial-frame-alist `<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>width <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">106</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>height <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span>
			    <span style="color: #66cc66;">&#40;</span>top <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>left <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>And of course some other tweaks. Re-learning emacs is turning out to be a good idea. But that&#8217;s another story.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/04/18/choosing-a-font-in-carbon-emacs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A ongoing discussion about C and libmemcached &#8211; don&#8217;t censor me</title>
		<link>http://swag.dk/blog/2009/03/26/a-ongoing-discussion-about-c-and-libmemcached-dont-censor-me/</link>
		<comments>http://swag.dk/blog/2009/03/26/a-ongoing-discussion-about-c-and-libmemcached-dont-censor-me/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 11:01:50 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Insanity]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=75</guid>
		<description><![CDATA[And there I was, happy as a 4-year old who had just found a whole box of strawberries. Then all of a sudden, along came a post about how Amir Salihefendic had made a little daemon in C, using libevent &#8230; <a href="http://swag.dk/blog/2009/03/26/a-ongoing-discussion-about-c-and-libmemcached-dont-censor-me/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>And there I was, happy as a 4-year old who had just found a whole box of strawberries.</p>

<p>Then all of a sudden, along came a post about how <a href="http://amix.dk/">Amir Salihefendic</a> had made a <a href="http://amix.dk/blog/viewEntry/19414">little daemon in C</a>, using libevent and memcached, that would allow him to cache a http request in memcached. I don&#8217;t quite get why one would like to reinvent a reverse http proxy like that, and stated my arguments on why I thought that this seemed like a really bad idea.</p>

<p>As you can tell from the comments, he did not agree. It ended up with him deleting my last comment. Good thing I kept a copy in my clipboard. So, here are the comments.
<span id="more-75"></span></p>

<p><strong>Mads Sülau Jørgensen 25. Mar</strong></p>

<p>This could just be me, but why would you do this over a proxy setup with Varnish, Squid og Apache with mpm&#95;event and mod&#95;cache? Seems a little like you&#8217;ve reinvented the wheel.</p>

<p>Also, as a previous commenter has suggested, why did you not just use the memcached support in nginx, and if it was because of the lack of multiple memcache servers, why not patch it and send this patch back to the community?</p>

<p><strong>amix 25. Mar</strong></p>

<p>Mads Sülau Jørgensen:</p>

<p>There are two reasons why I didn&#8217;t patch nginx with this support:</p>

<p>Separation of concerns: Running a separate polling server architecture I can monitor it and scale it independently
Patching nginx would not have been that easy
I prefer using memcached than Varnish or other caching options and memcached gives me a lot of options (deletion of multiple keys at once, consistent hashing and easy and proven scaling to terabytes of cache &#8211; - just to name a few).</p>

<p><strong>Mads Sülau Jørgensen 25. Mar</strong></p>

<p>Ah, but you could, easily, monitor a running nginx instance. The same goes for a running varnish instance. I&#8217;m not necessarily talking about running the nginx memcached module in the same process or the same server as your main proxy is running. So you&#8217;d archive the seperation by running another process or running the process on another box. Easy.</p>

<p>And the memcached module of nginx does not seem /that/ difficult to work with. Sure you&#8217;d have to do alot of handling of dead servers, and timeouts that could block the whole server. The current one probably handles this already. I don&#8217;t use it, so I&#8217;m not one to say.</p>

<p>With your keys structured correctly, you can flush (purge) multiple keys easily in varnish. You can provide your own hashing function as well. You could also use nginx&#8217;s hash balancer to send the request to the &#8220;correct&#8221; backend server.</p>

<p>The thing about hacking some server together with libevent in a language, that might not be onces main language is, that you&#8217;ll end up missing a lot of things, and it may end up exploding in your face. From the top of my head, what happens if a memcached server dies, memory leaks, what happens if the process segfaults (see memory leak).</p>

<p>It might be super-duper fast, but is it stable :)</p>

<p><strong>amix 25. Mar</strong></p>

<p>Mads Sülau Jørgensen:</p>

<pre>plurk@web04:~$ ps aux | grep poll_server
plurk     29835  2.3  0.0   2376  1048 ?        
S    Mar24  27:23 /home/plurk/poll_server/poll_server 0.0.0.0 16000</pre>

<p>It&#8217;s pretty stable (has not crashed since I started it), uses very little CPU and very little memory. If I stop the server, the nginx server will just route everything to Python servers. I doubt Varnish or patching Nginx would have been faster or more stable &#8211; - and they would have added a big conceptual complexity.</p>

<p>Personally, I would much rather fix errors in a 100 line program than a thousands line program. It&#8217;s separation of concerns and separation of complexity. So sure, I could have extended nginx+memcached or hacked Varnish &#8211; - but I chose to keep it simple, learn some new things and solve the problem in a very specialized way.</p>

<p><strong>Mads Sülau Jørgensen 25. Mar</strong></p>

<p>There is no hacking in the varnish solution what so ever. Varnish is a http proxy cache, it would be doing what is was designed to do. And my guess is, that it would be doing it well. My guess is also, that you&#8217;d end up using it alot, and to do more that using it with this one goal.</p>

<p>With a specialized solution, you have to handle a new process, you have to handle the errors that process can give, and you have to keep it running.</p>

<p>Using the tools already available makes more sense to me, and that&#8217;s what I&#8217;m doing when I run into a problem. Others, more proficient programmers usually have had the same problem before me.</p>

<p><strong>amix 25. Mar</strong></p>

<p>Mads Sülau Jørgensen:</p>

<p>Varnish is a good HTTP proxy and cache, no doubt about that, but it lacks a lot of things to be used in my setup. For example, how can I invalidate 1000 keys at once and do it in a split of a second? Their purging capabilities seem to be pretty primitive&#8230;</p>

<p>Plus, who said that I won&#8217;t iterate over this and go into a new direction &#8211; for example, benchmark this against a long-polling &#8220;comet&#8221; solution. Going into this direction would mean dropping Varnish &#8211; while I only need to change and add some lines of code to create a long polling solution of this.</p>

<p><strong>Mads Sülau Jørgensen 25. Mar</strong></p>

<p>Well, with a properly designed URL schema, you&#8217;d do something like this:</p>

<p>PURGE /poll/user/(?:mads|john|abc*)+/status</p>

<p>And then you&#8217;d have the VCL to handle that purge via purge_url(). The above will then purge the urls matching the regular expression, (so mads or john or starting with abc).</p>

<p>It actually works both fast, and well. It does not purge the cache when you tell it, but when you request an object. That makes invalidation <em>fast</em>.</p>

<p>See http://varnish.projects.linpro.no/ for more information.</p>

<p>You can more or less configure varnish to do what you want it to do via it&#8217;s configuration file.</p>

<p>As for the comet solution, your post more or less said that. Right about where it said something about push being hard to scale, which strikes me as a very odd comment. With push, you just have to be able to handle a horde of mostly idle connections, for which http://code.google.com/p/erlycomet/ or something like that, would probably be the way to go.</p>

<p><strong>amix 25. Mar</strong></p>

<p>Mads Sülau Jørgensen:</p>

<p>I am pretty tired of keep repeating myself. So this will be the last comment I answer you.</p>

<p>Varnish is a good cache, but it&#8217;s not really suited where I am headed. An example, how would you change the Varnish solution to support long polling? I can explore this option pretty easily with a basic understanding of how to build a custom server that can perform really good and that can be customized for my needs. I personally want to learn how to build things myself (also things that are complex and that require some thinking and engineering). I know I&#8217;ll need this skill longer down the road and practice makes a master &#8211; so the more practice I have, the better I&#8217;ll be at doing this.</p>

<p><strong>this is the reply he deleted</strong></p>

<p>&#62; I am pretty tired of keep repeating myself. So this will be the last comment I answer you.</p>

<p>The consider this answer a bonus for your collection of entropy.</p>

<p>&#62; An example, how would you change the Varnish solution to support long polling?</p>

<p>I&#8217;m quite sure that I never implied, that varnish would be suitable to do long polling (or other non-cachable long lived requests). If you think I did, please, tell me where.</p>

<p>&#62; I can explore this option pretty easily with a basic understanding of how to build a custom server that can perform really good and that can be customized for my needs.</p>

<p>Yes, and that&#8217;s very fine and good, but that&#8217;s not really what your post says. It basiclly says &#8220;Using libmemcached and a C program, I can make a lot of http requests. Fast. And I don&#8217;t expect to use comet. Push is not what I want.&#8221; but from the looks of it, you may have wanted it to say &#8220;Hi, I made this little C program using libmemcached that I&#8217;m planing on developing into a comet server at some point. I need to extend the server with push in the future.&#8221;</p>

<p>&#62; I personally want to learn how to build things myself (also things that are complex and that require some thinking and engineering).</p>

<p>Yes, throwing something together is very fun, throwing something together in C is even more fun. But throwing something together in C for a large website, because, it&#8217;s just fun, well, for a lot of the web developers in the world, that&#8217;s a really bad idea. It may work today, tomorrow but will it work in a week, a month or a year?</p>

<p>&#62; I know I&#8217;ll need this skill longer down the road and practice makes a master &#8211; so the more practice I have, the better I&#8217;ll be at doing this.</p>

<p>Then I&#8217;d really recommend you looking at this:</p>

<p>http://aleccolocco.blogspot.com/2008/10/gazillion-user-comet-server-with.html</p>

<p>A comet server in C, with all the boiler plate completed. Just insert logic.</p>

<p>You might also get a kick out of reading up on memory management, input validation, how to avoid buffer overflows and just plain old K&amp;R C.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/03/26/a-ongoing-discussion-about-c-and-libmemcached-dont-censor-me/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using mercurial with eclipse to edit actionscript files on os x</title>
		<link>http://swag.dk/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/</link>
		<comments>http://swag.dk/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 09:38:33 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[Mercurial]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/</guid>
		<description><![CDATA[We have recently switched to use mercurial as our DVCS. We&#8217;re hosting our many repositories on bitbucket. I love it. When it comes to actionscript files, flash has always had odd newlines. For some reason, it has always used \r &#8230; <a href="http://swag.dk/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.konstellation.dk/">We</a> have recently switched to use <a href="http://www.selenic.com/mercurial/wiki/">mercurial</a> as our DVCS. We&#8217;re hosting our many repositories on <a href="http://bitbucket.org/">bitbucket</a>.</p>

<p>I love it.</p>

<p>When it comes to actionscript files, flash has always had odd newlines. For some reason, it has always used <code>\r</code> as it&#8217;s newline. Mercurial, beeing a unix tool, likes it&#8217;s newlines to be <code>\n</code>. So what better to do, than to make mercurial encode/decode the files for us.</p>

<p>To do that, we can simply add the following to our <code>.hgrc</code> file:</p>

<pre>
[encode]
*.as = perl -pe 's/\r/\n/g'

[decode]
*.as = perl -pe 's/\r/\n/g'
</pre>

<p>I don&#8217;t really know wether the decode part is necessary, but I like to keep it around (if someone should commit poison).</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mercurial trac commit hook</title>
		<link>http://swag.dk/blog/2008/10/13/mercurial-trac-commit-hook/</link>
		<comments>http://swag.dk/blog/2008/10/13/mercurial-trac-commit-hook/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 15:03:50 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=56</guid>
		<description><![CDATA[Having searched a lot around google, it does not seem that anyone has published their trac commit hooks for mercurial. Since I had to use just that, I&#8217;ve cooked up a little hook which is based on the hook from &#8230; <a href="http://swag.dk/blog/2008/10/13/mercurial-trac-commit-hook/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Having searched a lot around google, it does not seem that anyone has published their <a href="http://trac.edgewall.org/">trac</a> commit hooks for <a href="http://www.selenic.com/mercurial/">mercurial</a>. Since I had to use just that, I&#8217;ve cooked up a little hook which is based on the hook from the <code>timingandestimationplugin</code>. I&#8217;ve created it as a changegroup hook, and it&#8217;s probably filled with bugs, but it seems to work and it catches the <code>fixes #42</code> and such.</p>

<p>To use the hook you must place it somewhere inside your <code>PYTHONPATH</code> and tell mercurial to use it (I placed it in a module called trachook &#8212; don&#8217;t call your module trac):</p>

<pre linenumbers="1">
[hooks]
changegroup = python:trachook.hook
</pre>

<p>And tell the hook where to find your trac installation:</p>

<pre linenumbers="1">
[trac-hook]
root = /path/to/trac
url = http://url/to/trac
</pre>

<p>Grab your own copy of the <a href="http://www.bitbucket.org/madssj/mercurial-trac-hook/src/">source</a>, and happy coding. And a big thank you to <a href="http://noehr.org/">Jesper Nøhr</a>.</p>

<p>Also, if you need a place to host your mercurial repository but don&#8217;t wan&#8217;t to set it up yourself, check out <a href="http://www.bitbucket.org/">bitbucket</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2008/10/13/mercurial-trac-commit-hook/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Actionscript 3 &#8211; posting XML data with URLLoader</title>
		<link>http://swag.dk/blog/2008/07/30/actionscript-3-posting-xml-data-with-urlloader/</link>
		<comments>http://swag.dk/blog/2008/07/30/actionscript-3-posting-xml-data-with-urlloader/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 13:33:34 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=32</guid>
		<description><![CDATA[So, you want to POST some XML data to a web service. Let&#8217;s for the fun of it say, that we would like to POST the following piece of XML to a PHP script: 1 &#60;request command=&#34;run-command&#34; /&#62; Now, the &#8230; <a href="http://swag.dk/blog/2008/07/30/actionscript-3-posting-xml-data-with-urlloader/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So, you want to POST some XML data to a web service.</p>

<p>Let&#8217;s for the fun of it say, that we would like to POST the following piece of XML to a PHP script:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;request</span> <span style="color: #000066;">command</span>=<span style="color: #ff0000;">&quot;run-command&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>


<p><span id="more-32"></span></p>

<p>Now, the way I&#8217;d do that in Actionscript 3, is like this:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> a<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> = <span style="color: #000000; font-weight: bold;">&lt;</span>request command=<span style="color: #990000;">&quot;run-command&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> r<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLRequest</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://localhost/service.php&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
r.<span style="color: #004993;">data</span> = a;
r.<span style="color: #004993;">method</span> = <span style="color: #004993;">URLRequestMethod</span>.<span style="color: #004993;">POST</span>;
r.<span style="color: #004993;">contentType</span> = <span style="color: #990000;">&quot;text/xml&quot;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> l<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLLoader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLLoader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
l.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;success&quot;</span>, l.<span style="color: #004993;">data</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
l.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">IOErrorEvent</span>.<span style="color: #004993;">IO_ERROR</span>, <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;failed&quot;</span>, e<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
l.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span>r<span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>


<p>But it seems, that older flash players (&lt;= 9,0,45,0) does not call the XML&#8217;s toString() or something. The result is, that the URLLoader makes a GET request, which is a bit hard to debug as version 9,0,115,0 does send the XML correctly.</p>

<p>Anyway, the correct way to do it, seems to be to manually call the XML&#8217;s .toXMLString() method, like so:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> a<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> = <span style="color: #000000; font-weight: bold;">&lt;</span>request command=<span style="color: #990000;">&quot;run-command&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> r<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLRequest</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://localhost/service.php&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
r.<span style="color: #004993;">data</span> = a.<span style="color: #004993;">toXMLString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
r.<span style="color: #004993;">method</span> = <span style="color: #004993;">URLRequestMethod</span>.<span style="color: #004993;">POST</span>;
r.<span style="color: #004993;">contentType</span> = <span style="color: #990000;">&quot;text/xml&quot;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> l<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLLoader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLLoader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
l.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;success&quot;</span>, l.<span style="color: #004993;">data</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
l.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">IOErrorEvent</span>.<span style="color: #004993;">IO_ERROR</span>, <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;failed&quot;</span>, e<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
l.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span>r<span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2008/07/30/actionscript-3-posting-xml-data-with-urlloader/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fun with mod_macro and django</title>
		<link>http://swag.dk/blog/2008/03/19/fun-with-mod_macro/</link>
		<comments>http://swag.dk/blog/2008/03/19/fun-with-mod_macro/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 19:36:06 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[coniuro aps]]></category>
		<category><![CDATA[mod_macro]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/2008/03/19/fun-with-mod_macro/</guid>
		<description><![CDATA[At work we yestoday decided to update our internal url structure for our client test sites, issue management systems and such arround a bit. Seeing as we decided on purchasing a genuine signed ssl wildcard certificate, we needed to change &#8230; <a href="http://swag.dk/blog/2008/03/19/fun-with-mod_macro/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://www.coniuro.dk/" title="Coniuro ApS - we code">work</a> we yestoday decided to update our internal url structure for our client test sites, issue management systems and such arround a bit. Seeing as we decided on purchasing a genuine signed ssl wildcard certificate, we needed to change our url&#8217;s a bit.</p>

<p><span id="more-12"></span></p>

<p>We used to have a url schema consisting of the following components:</p>

<pre><code>    [dev.|test.]&lt;project-name&gt;.&lt;client-name&gt;.companyname.tld
</code></pre>

<p>That made for very long url&#8217;s, and furthermore, the url&#8217;s would not be supported by a <code>*.companyname.tld</code>. Based on this infomation, and wanting to create a more generelized, nicer url schema, we choose to cross over to the following schema:</p>

<pre><code>   [dev-|test-]&lt;project-name&gt;.companyname.tld
</code></pre>

<p>Which besides from supporting a wildcard ssl certificate just fine, just, well, looks nicer.</p>

<p>Either way, me beeing in charge of the contents in <code>/etc/</code>, and getting a little sick of growing apache configuration files, I chooose to write some macros to configure our project sites.</p>

<p>We generally have 2 types of project sites. <a href="http://www.djangoproject.com/" title="Django - a python web framework for very rapid development, love it!">django</a> based sites, and PHP based sites.</p>

<p>A typical django apache configuration consists of a VirtualHost and some mod_python settings inside a Location block. Lot&#8217;s of configuration, very little actual diffrence between the configration projects in between.</p>

<p>An example of a django based application running inside a apache VirtualHost, could look like this:</p>

<pre><code>    &lt;VirtualHost *:80&gt;
        ServerName test-project.company.tld
        &lt;Location /&gt;
            SetHandler python-program
            PythonHandler django.core.handlers.modpython
            PythonPath "['/path/to/project', '/other/path/to/inject']"
            SetEnv DJANGO_SETTINGS_MODULE project.settings
        &lt;/Location&gt;
        &lt;Location /media/&gt;
            SetHandler none
        &lt;/Location&gt;
        Alias /media/admin/ /path/to/django/contrib/admin/media/
        Alias /media/ /path/to/project/media/
    &lt;/VirtualHost&gt;
</code></pre>

<p>And the on top of that comes SSL configuration, auth and such. 25 lines of configuration per django site. Now, I really wanted to acomplish two things.</p>

<ul>
<li>Make the configuration easier to maintain</li>
<li>Enable other users to setup sites without knowing the depths of django and <code>mod_python</code></li>
</ul>

<p>Now, apache configuration, meet <a href="http://www.cri.ensmp.fr/~coelho/mod_macro/" title="mod_macro - a configuration macro module for apache2">mod_macro</a>. The solution to my problem was very simple. Create a macro that handles all the django configuration, given 4 parameters. A server name (i.e. the url), the parent directory of the django site and the module name of the site.</p>

<p>So i started building my macro.</p>

<pre><code>    &lt;Macro DjangoSite $servername $root $module&gt;
        &lt;VirtualHost *:80&gt;
            ServerName $servername
            &lt;Location /&gt;
                SetHandler python-program
                PythonHandler django.core.handlers.modpython
                PythonPath "['/common/path/to/inject', '$root']"
                SetEnv DJANGO_SETTINGS_MODULE $modulename.settings
            &lt;/Location&gt;
            &lt;Location /media/&gt;
                SetHandler none
            &lt;/Location&gt;
            Alias /media/admin/ /path/to/django/contrib/admin/media/
            Alias /media/ $root/$module/media/
        &lt;/VirtualHost&gt;
    &lt;/Macro&gt;
</code></pre>

<p><code>mod_macro</code> is really simple, it will do a longest-match search and replace on the macro&#8217;s content. So, if we had a django site in <code>/path/to/djangosite/testsite/</code> the above macro could be used as:</p>

<pre><code>    Use DjangoSite www.example.org /path/to/djangosite/ testsite
</code></pre>

<p>Which would expand to:</p>

<pre><code>    &lt;VirtualHost *:80&gt;
        ServerName www.example.org
        &lt;Location /&gt;
            SetHandler python-program
            PythonHandler django.core.handlers.modpython
            PythonPath "['/common/path/to/inject', '/path/to/djangosite/']"
            SetEnv DJANGO_SETTINGS_MODULE testsite.settings
        &lt;/Location&gt;
        &lt;Location /media/&gt;
            SetHandler none
        &lt;/Location&gt;
        Alias /media/admin/ /path/to/django/contrib/admin/media/
        Alias /media/ /path/to/djangosite/testsite/media/
    &lt;/VirtualHost&gt;
</code></pre>

<p>Hope you find this infomation a little useful. I actually like looking at our apache configuration file now.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2008/03/19/fun-with-mod_macro/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Asynchronous socket server in python</title>
		<link>http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/</link>
		<comments>http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 14:39:52 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[asynchat]]></category>
		<category><![CDATA[asyncore]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[socket]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/</guid>
		<description><![CDATA[The code for a very simple asynchronous socket server written in python utilizing the asynchat module. It&#8217;s all in good fun. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 &#8230; <a href="http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The code for a very simple asynchronous socket server written in python utilizing the asynchat module. It&#8217;s all in good fun.</p>

<p><span id="more-8"></span></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">asyncore</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">asynchat</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Lobby<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <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><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">clients</span> = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> leave<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, client<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">clients</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>client<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> join<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, client<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">clients</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span>client<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> send_to_all<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> client <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">clients</span>:
            client.<span style="color: black;">push</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Client<span style="color: black;">&#40;</span><span style="color: #dc143c;">asynchat</span>.<span style="color: black;">async_chat</span><span style="color: black;">&#41;</span>:
    <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>, conn, lobby<span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">asynchat</span>.<span style="color: black;">async_chat</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, conn=conn<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span> = <span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">set_terminator</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span> = lobby
        <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> collect_incoming_data<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span> += data
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> found_terminator<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;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span>.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #483d8b;">&quot;QUIT&quot;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span>.<span style="color: black;">leave</span><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;">close_when_done</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span>.<span style="color: black;">send_to_all</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span> + <span style="color: #008000;">self</span>.<span style="color: black;">terminator</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span> = <span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Server<span style="color: black;">&#40;</span><span style="color: #dc143c;">asynchat</span>.<span style="color: black;">async_chat</span><span style="color: black;">&#41;</span>:
    <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><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">asynchat</span>.<span style="color: black;">async_chat</span>.<span style="color: #0000cd;">__init__</span><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;">create_socket</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">socket</span>.<span style="color: black;">AF_INET</span>, <span style="color: #dc143c;">socket</span>.<span style="color: black;">SOCK_STREAM</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">set_reuse_addr</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">bind</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;127.0.0.1&quot;</span>, <span style="color: #ff4500;">12345</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">listen</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span> = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> set_lobby<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, lobby<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span> = lobby
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> handle_accept<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        sock, addr = <span style="color: #008000;">self</span>.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        client = Client<span style="color: black;">&#40;</span>sock, <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    lobby = Lobby<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    server = Server<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    server.<span style="color: black;">set_lobby</span><span style="color: black;">&#40;</span>lobby<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">asyncore</span>.<span style="color: black;">loop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>


<p>Booting the server is a matter of <code>python echoserver.py</code>.</p>

<p>After that, telnet can be used to induce messages, as such:
<pre>
$ telnet localhost 12345
foo
foo</pre>
The same will be true for any amount of clients. This is so simple. Love it.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
