<?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>Kellbot! &#187; python</title>
	<atom:link href="http://www.kellbot.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kellbot.com</link>
	<description>Tinysaurs and Hacking and Nonsense</description>
	<lastBuildDate>Sat, 07 Jan 2012 19:49:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Extracting + Graphing Wii Fit data</title>
		<link>http://www.kellbot.com/2010/05/extracting-graphing-wii-fit-data/</link>
		<comments>http://www.kellbot.com/2010/05/extracting-graphing-wii-fit-data/#comments</comments>
		<pubDate>Thu, 13 May 2010 20:31:35 +0000</pubDate>
		<dc:creator>Kellbot</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[wii]]></category>
		<category><![CDATA[WiiFit]]></category>

		<guid isPermaLink="false">http://www.kellbot.com/?p=609</guid>
		<description><![CDATA[

In preparation to tinker with the miCoach data, I started with some better-travelled exercise bits: WiiFit body test data. Starting with Jansen Price's excellent blog post on the subject, I slowly worked through the data and wrote a python script to interpret the binaries and save them to a CSV. By using the excellent ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><p><a href="http://www.kellbot.com/wp-content/uploads/WiiFitdata.jpg"><img class="aligncenter size-full wp-image-610" title="WiiFitdata" src="http://www.kellbot.com/wp-content/uploads/WiiFitdata.jpg" alt="" width="616" height="329" /></a></p>
<p>In preparation to tinker with the miCoach data, I started with some better-travelled exercise bits: WiiFit body test data. Starting with Jansen Price&#8217;s <a href="http://jansenprice.com/blog?id=9-Extracting-Data-from-Wii-Fit-Plus-Savegame-Files">excellent blog post on the subject</a>, I slowly worked through the data and wrote a python script to interpret the binaries and save them to a CSV. By using the excellent flot javascript library, I was able to generate the nice graph above. There was a lot of trial and error, but here&#8217;s an overview of the process:</p>
<ol>
<li>Copy Wii save game data to the SD card. This is done from <strong>Wii Options</strong> &gt; <strong>Data              Management</strong> &gt; <strong>Save Data</strong> &gt; <strong>Wii</strong></li>
<li>Find the save game data on the card. It&#8217;s in something like &#8216;private/wii/title/RFPE&#8217;, although different regions may have slightly different codes. RFPE is the code for WiiFit Plus. Copy the WiiFit data.bin file from the SD card to your local machine.</li>
<li>Decrypt data.bin. This is explained pretty well <a href="http://jansenprice.com/blog?id=9-Extracting-Data-from-Wii-Fit-Plus-Savegame-Files">here</a>. To create the keys I ended up creating text files with the hex string for each and then using &#8220;xxd -r -p sd_iv_hex sd_iv&#8221; et al to save a binary version. If you&#8217;re getting &#8220;MD5 mismatch&#8221; errors, you probably saved the keys incorrectly. If you aren&#8217;t sure, check the file size. They should be 16 bytes each.</li>
<li>Run the decrypted RPHealth.dat through a parser (I wrote one in Python for this)</li>
<li>Run the CSV through your favorite graph generation library. I use <a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;ved=0CBYQFjAA&amp;url=http%3A%2F%2Fcode.google.com%2Fp%2Fflot%2F&amp;ei=i17sS76fNoL-8AaY8Z2EBQ&amp;usg=AFQjCNHH828GR4jYTW24KE8RKUO0IqllvQ&amp;sig2=ZBXS47RapZbG2kNvbNZw8A">flot</a> because Google Charts don&#8217;t handle dates very well.</li>
</ol>
<p>Thanks to Jansen&#8217;s handy chart of which bits are where, writing the parser was pretty easy. This isn&#8217;t the most elegant code I&#8217;ve ever written, but it gets the job done:</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;">struct</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">csv</span>
&nbsp;
&nbsp;
mii = <span style="color: #ff4500;">0</span>
<span style="color: #808080; font-style: italic;">#we know that each record is 0x9271 bytes long</span>
record_length = 0x9281
&nbsp;
record_start = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#path to WiiFit data file</span>
infile = <span style="color: #483d8b;">'RPHealth.dat'</span>
&nbsp;
FH = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>infile, <span style="color: #483d8b;">'rb'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">## It loops through 7 profiles, because I happen to know I have 7.</span>
<span style="color: #808080; font-style: italic;">## A better approach would be to go to the end of the file, of course.</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span>mii <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #808080; font-style: italic;">#go to the start of the current record</span>
    FH.<span style="color: black;">seek</span><span style="color: black;">&#40;</span>record_start<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#read the first 30 bytes (header + name)</span>
    line = FH.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#for some reason names are stored as N a m e instead of Name.</span>
    <span style="color: #808080; font-style: italic;">#Throw away the header any extranous spaces</span>
    data = <span style="color: #dc143c;">struct</span>.<span style="color: black;">unpack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;9xcxcxcxcxcxcxcxcxcxcxc&quot;</span>,line<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Condense our unpacked characters into a string</span>
    wf_name = <span style="color: #dc143c;">string</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>data,<span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#open a new CSV file for this person.</span>
    <span style="color: #808080; font-style: italic;">#If the name is shorter than 4 characters or has whitespace, the script</span>
    <span style="color: #808080; font-style: italic;">#will exit. This should probably be fixed.</span>
    FW = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'WiiFit_'</span>+wf_name<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>:<span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">'.csv'</span>, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
    recordWriter = <span style="color: #dc143c;">csv</span>.<span style="color: black;">writer</span><span style="color: black;">&#40;</span>FW, delimiter=<span style="color: #483d8b;">&quot;,&quot;</span>, quotechar=<span style="color: #483d8b;">&quot;'&quot;</span>, quoting=<span style="color: #dc143c;">csv</span>.<span style="color: black;">QUOTE_MINIMAL</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Weigh-in data starts 0x38a1 bytes into the record</span>
    FH.<span style="color: black;">seek</span><span style="color: black;">&#40;</span>record_start + 0x38a1<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#we'll loop through the record data until it starts coming up blank</span>
    <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;">#4 byte date </span>
        line = FH.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>
        data = <span style="color: #dc143c;">struct</span>.<span style="color: black;">unpack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&gt;i&quot;</span>,line<span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;">#bit shift to get the month, day, and year. Could also get time if you wanted.</span>
        year = data<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff4500;">20</span> <span style="color: #66cc66;">&amp;</span> 0x7ff
        month = data<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff4500;">16</span> <span style="color: #66cc66;">&amp;</span> 0xf
        day = data<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff4500;">11</span> <span style="color: #66cc66;">&amp;</span> 0x1f
&nbsp;
        <span style="color: #808080; font-style: italic;">#break the loop if the date comes back 0</span>
        <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>year == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">#format the date into something humans like to read</span>
        date = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>year<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'-'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>month<span style="color: black;">&#41;</span>+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'-'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>day<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">#the next three sets of 2 byte data represent weight, BMI, and balance</span>
        line = FH.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">17</span><span style="color: black;">&#41;</span>
        data = <span style="color: #dc143c;">struct</span>.<span style="color: black;">unpack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&gt;3H&quot;</span>,line<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>:<span style="color: #ff4500;">6</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
        recordWriter.<span style="color: black;">writerow</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>date<span style="color: black;">&#93;</span> + <span style="color: black;">&#91;</span>data<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span> + <span style="color: black;">&#91;</span>data<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span> + <span style="color: black;">&#91;</span>data<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">#now that we're done with the record, advance to the start of the next one</span>
    record_start = record_start + record_length
&nbsp;
    mii = mii+<span style="color: #ff4500;">1</span></pre></div></div>

<p>You can download a copy of it <a href="http://www.kellbot.com/code/WiiFit/wiiFitParser.txt">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kellbot.com/2010/05/extracting-graphing-wii-fit-data/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: Writing a TCP server in Python</title>
		<link>http://www.kellbot.com/2010/02/tutorial-writing-a-tcp-server-in-python/</link>
		<comments>http://www.kellbot.com/2010/02/tutorial-writing-a-tcp-server-in-python/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 23:10:18 +0000</pubDate>
		<dc:creator>Kellbot</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[socket programming]]></category>

		<guid isPermaLink="false">http://www.kellbot.com/?p=532</guid>
		<description><![CDATA[During the last 12 hours of the hackathon I decided to write a TCP server for an old project I want to finally finish. I decided to write it in Python, mostly because my friend Adam likes Python and Adam would inevitably be the one answering my questions when I got stuck. I should ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><p>During the last 12 hours of the hackathon I decided to write a TCP server for an old project I want to finally finish. I decided to write it in Python, mostly because my friend Adam likes Python and Adam would inevitably be the one answering my questions when I got stuck. I should mention that prior to yesterday evening I knew nothing about socket programing. And I only had a vague idea of what threading was.</p>
<p>Since not everyone has friends like Adam, I&#8217;m writing up my findings in a tutorial.</p>
<p><em>Note: A bug in my CSS is causing the code blocks to show up extra wide. I&#8217;ll fix it once I&#8217;m back home from the hackathon</em></p>
<p><strong>Understanding Sockets</strong></p>
<p>First, I&#8217;m going to assume you understand that this is not a tutorial about writing an HTTP server. Instead this server will take connections from clients and keep them open to pass data back and forth until one side decides to close the connection. By keeping the connection open we eliminate the need to constantly poll the server for updates.</p>
<p><a href="http://www.amk.ca/python/howto/sockets/">Socket Programming HOWTO</a> provides a broad overview of sockets and is a good starting place.</p>
<p><strong>Python&#8217;s Socket Library</strong></p>
<p>Luckily python has an easy to use library. Like other libraries, we import it with thusly:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">socket</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span></pre></div></div>

<p>Many of the socket methods you&#8217;ll use are pretty self explanatory:<br />
<code>socket.listen()</code> &#8211; listens for incoming connections<br />
<code>socket.accept()</code> &#8211; accepts an incoming connection<br />
<code>socket.recv()</code> &#8211; returns incoming data as a string<br />
<code>socket.send()</code> &#8211; sends data to client socket*<br />
<code>socket.close()</code> &#8211; closes the socket</p>
<p>*in this context the &#8216;client socket&#8217; can be on either the server or client side. When a client connects to a server, the server creates a new client socket on its end. The two clients, one on each end, communicate with each other while the server socket remains open for incoming connections. This becomes more clear as you work with socket connections.</p>
<p><strong>Writing the server</strong><br />
First thing&#8217;s first, we need to establish our server socket:</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
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">##server.py</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">socket</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>      <span style="color: #808080; font-style: italic;">#import the socket library</span>
&nbsp;
<span style="color: #808080; font-style: italic;">##let's set up some constants</span>
HOST = <span style="color: #483d8b;">''</span>    <span style="color: #808080; font-style: italic;">#we are the host</span>
PORT = <span style="color: #ff4500;">29876</span>    <span style="color: #808080; font-style: italic;">#arbitrary port not currently in use</span>
ADDR = <span style="color: black;">&#40;</span>HOST,PORT<span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;">#we need a tuple for the address</span>
BUFSIZE = <span style="color: #ff4500;">4096</span>    <span style="color: #808080; font-style: italic;">#reasonably sized buffer for data</span>
&nbsp;
<span style="color: #808080; font-style: italic;">## now we create a new socket object (serv)</span>
<span style="color: #808080; font-style: italic;">## see the python docs for more information on the socket types/flags</span>
serv = <span style="color: #dc143c;">socket</span><span style="color: black;">&#40;</span> AF_INET,SOCK_STREAM<span style="color: black;">&#41;</span>    
&nbsp;
<span style="color: #808080; font-style: italic;">##bind our socket to the address</span>
serv.<span style="color: black;">bind</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>ADDR<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;">#the double parens are to create a tuple with one element</span>
serv.<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: #808080; font-style: italic;">#5 is the maximum number of queued connections we'll allow</span></pre></td></tr></table></div>

<p>So now we have a server that&#8217;s listening for a connection. Or at least we did until the script reached the end and terminated, but we&#8217;ll get to that in a bit. Let&#8217;s leave our server hanging and jump to our client software. </p>
<p><strong>Creating the client</strong><br />
 Start a new python script for the client. We&#8217;ll need many of the same constants from the server, but our host will be &#8216;localhost&#8217;. For now we&#8217;ll be running both the server and the client on the same machine.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">##client.py</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">socket</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
HOST = <span style="color: #483d8b;">'localhost'</span>
PORT = <span style="color: #ff4500;">29876</span>    <span style="color: #808080; font-style: italic;">#our port from before</span>
ADDR = <span style="color: black;">&#40;</span>HOST,PORT<span style="color: black;">&#41;</span>
BUFSIZE = <span style="color: #ff4500;">4096</span>
&nbsp;
cli = <span style="color: #dc143c;">socket</span><span style="color: black;">&#40;</span> AF_INET,SOCK_STREAM<span style="color: black;">&#41;</span>
cli.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>ADDR<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Notice that we&#8217;re creating another socket object on this end but instead of binding and listening, we&#8217;re using the <code>connect()</code> method to connect to our server.</p>
<p>So what happens if we run our server and then run our client? Well, not much. While our server starts to listen, it then hits the end of the script. We need it to instead wait until it accepts a connection and then do something with that connection.<br />
<code>socket.accept()</code> does just that, and returns two things: a new client socket and the address bound to the socket on the other end. Once we have that, we can send data!</p>
<p>Continuing on server.py:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">serv = <span style="color: #dc143c;">socket</span><span style="color: black;">&#40;</span> AF_INET,SOCK_STREAM<span style="color: black;">&#41;</span>    
&nbsp;
<span style="color: #808080; font-style: italic;">##bind our socket to the address</span>
serv.<span style="color: black;">bind</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>ADDR<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;">#the double parens are to create a tuple with one element</span>
serv.<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: #808080; font-style: italic;">#5 is the maximum number of queued connections we'll allow</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'listening...'</span>
&nbsp;
conn,addr = serv.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#accept the connection</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'...connected!'</span>
conn.<span style="color: black;">send</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'TEST'</span><span style="color: black;">&#41;</span>
&nbsp;
conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The last step is to jump back over to our client and tell our client to expect to receive data:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">cli = <span style="color: #dc143c;">socket</span><span style="color: black;">&#40;</span> AF_INET,SOCK_STREAM<span style="color: black;">&#41;</span>
cli.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>ADDR<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
data = cli.<span style="color: black;">recv</span><span style="color: black;">&#40;</span>BUFSIZE<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> data
&nbsp;
cli.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Now when you run your server it will wait until a client connects. Once you run your client it will connect and receive a short message (the word &#8220;TEST&#8221; in this case) and print it to the screen. If you wanted to you could have the client send a response, using the same <code>send()</code> and <code>recv()</code> methods (but reversed).</p>
<p>Make sure you <code>close()</code> your connections when you&#8217;re done using them. If you don&#8217;t close things nicely they have a nasty habit of staying bound/connected until you forcibly kill the python process. This can be a real pain when you&#8217;re debugging.</p>
<p>By itself this isn&#8217;t particularly useful, especially considering we can only handle one connection at a time and exit once it&#8217;s closed. By adding a few while loops and some threading we can make this into something much more valuable. As it is, I&#8217;m pretty wiped from the hackathon, so the threading tutorial will have to wait until another day. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.kellbot.com/2010/02/tutorial-writing-a-tcp-server-in-python/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Tinysaur Display</title>
		<link>http://www.kellbot.com/2008/12/tinysaur-display/</link>
		<comments>http://www.kellbot.com/2008/12/tinysaur-display/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 15:15:12 +0000</pubDate>
		<dc:creator>Kellbot</dc:creator>
				<category><![CDATA[lased]]></category>
		<category><![CDATA[SDXF Documentation]]></category>
		<category><![CDATA[craft show]]></category>
		<category><![CDATA[display]]></category>
		<category><![CDATA[laser]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sdxf]]></category>
		<category><![CDATA[Tinysaur]]></category>

		<guid isPermaLink="false">http://www.kellbot.com/?p=99</guid>
		<description><![CDATA[

I'm helping my friend Sara at the Squidfire Holiday Market in Baltimore, Maryland on Sunday. She suggested I bring some Tinysaurs, and so I made a display to neatly hold the Tinysaur kits.

I generated the pattern with a python script I wrote, using the sdxf library.

If you'd like to make your own, the DXF ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><p><a class="tt-flickr tt-flickr-Medium" title="Tinysaur Kit Display" href="http://www.flickr.com/photos/kellbot/3086331030/"><img class="alignnone" src="http://farm4.static.flickr.com/3127/3086331030_fbf5268d51.jpg" alt="Tinysaur Kit Display" width="500" height="375" /></a></p>
<p>I&#8217;m helping my friend <a href="http://girlscantell.typepad.com">Sara</a> at the Squidfire Holiday Market in Baltimore, Maryland on Sunday. She suggested I bring some Tinysaurs, and so I made a display to neatly hold the Tinysaur kits.</p>
<p>I generated the pattern with a <a href="http://kellbot.com/sdxf/tinysaurdisplay.py">python script</a> I wrote, using the <a href="http://www.kellbot.com/sdxf-python-library-for-dxf/">sdxf library</a>.</p>
<p>If you&#8217;d like to make your own, the DXF files are up on <a href="http://www.thingiverse.com/thing:235">Thingiverse</a>, or you can grab the python scripts and make one to your own dimensions. I cut it on the laser, but there&#8217;s no reason it couldn&#8217;t be cut on a scroll saw.</p>
<p><a class="tt-flickr tt-flickr-Small" title="first, second, success" href="http://www.flickr.com/photos/kellbot/3085502759/"><img class="alignnone" src="http://farm4.static.flickr.com/3176/3085502759_bc519292e1_m.jpg" alt="first, second, success" width="240" height="180" /></a></p>
<p>Here you can see my first attempt, second attempt, and final. The first two were in cardboard, fantastic for prototyping.</p>
<p>Once all this craft show stuff is over I will probably make a few available in my <a href="http://kfarrell.etsy.com">Etsy shop</a> in case folks who do craft shows are interested in one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kellbot.com/2008/12/tinysaur-display/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adam Mayer explains pointers to art students</title>
		<link>http://www.kellbot.com/2008/11/programming-pointers-for-art-students/</link>
		<comments>http://www.kellbot.com/2008/11/programming-pointers-for-art-students/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 13:52:25 +0000</pubDate>
		<dc:creator>Kellbot</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[art school]]></category>
		<category><![CDATA[dead sharks]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[silliness]]></category>

		<guid isPermaLink="false">http://www.kellbot.com/?p=92</guid>
		<description><![CDATA[I don't have a background in CS. In fact I got my degree in Crafts. Yes, you can get a degree in Crafts. And I have one.
Most of my programming skills are self-taught, which is fine most of the time but occasionally gets me into trouble. Recently I learned about pointers the hard way, ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><p style="text-align: left;">I don&#8217;t have a background in CS. In fact I got my degree in Crafts. Yes, you can get a degree in Crafts. And I have one.</p>
<p style="text-align: left;">Most of my programming skills are self-taught, which is fine most of the time but occasionally gets me into trouble. Recently I learned about pointers the hard way, and to help clarify things my friend Adam broke it down into art school terms for me. It was so hilarious (and helpful) that I&#8217;m reposting it here.</p>
<blockquote style="text-align: left;"><p>You&#8217;re telling me you went through art school without once discussing referers and referents?  WHAT KIND OF PUNK-ASS ART SCHOOL DID YOU GO TO?  Let me break it down to you in art-school terms, then:</p>
<p>In python, you can think of all variables as pointers.  All they do is point to objects.  When you say:<br />
<code>&gt;&gt; constructivism = 6</code><br />
You can think of this as creating an &#8220;integer object&#8221; with a value of 6.  Constructivism is not itself 6 (which is to say, constructivism is not &#8220;6&#8243; in the way that brutalist materialism might be &#8220;6&#8243;).  Instead, constructivism is a variable which points to this newly created<br />
integer object with a value of 6.  If you were to say:<br />
<code>&gt;&gt; constructivism = []</code><br />
or<br />
<code>&gt;&gt; constructivism = Socket()</code><br />
then you&#8217;ll be creating an new empty list object, or a new socket object, and then constructivism will point to that instead.</p>
<p>That&#8217;s all pretty simple.  Then there&#8217;s this:<br />
<code>&gt;&gt; futurism = constructivism</code><br />
Now, constructivism is already a pointer to something else.  Futurism, however, will not point to constructivism itself: instead it will point to whatever constructivism points too, much like in 1991 Saatchi did not point at Damien Hirst, but whatever Damien Hirst was pointing at at the time; in this instance a dead shark.  Note that while Damien Hirst went on to point at other things, Saatchi is still pointing at the dead shark.  So:<br />
<code>&gt;&gt; hirst = Shark( dead=True )<br />
Hirst is now pointing at a dead shark.<br />
&gt;&gt; saatchi = hirst<br />
Saatchi is now pointing at the same dead shark.  Now,<br />
&gt;&gt; hirst = Skull( bling=True )<br />
Hirst is now pointing at a blinged-out skull, but Saatchi is still stuck on the shark.</code></p>
<p>When two variables are pointing at the same thing, they both see any changes made to that thing over time.  So, for example, look at the following code:</p>
<p><code>&gt;&gt; # note that hirst does not make the shark himself, but calls a constructor<br />
&gt;&gt; hirst = Shark( dead=True )<br />
&gt;&gt; saatchi = hirst<br />
&gt;&gt; print hirst.living<br />
False</code></p>
<p>Both Hirst and Saatchi are referring to the same dead shark.  But, later:</p>
<p><code>&gt;&gt; # Saatchi uses his money to bring the shark back to life<br />
&gt;&gt; saatchi.ressurect()<br />
&gt;&gt; print saatchi.living<br />
True<br />
&gt;&gt; print hirst.living<br />
True</code></p>
<p>However, let&#8217;s move on:<br />
<code>&gt;&gt; hirst = Skull()  # Hirst has moved on to other dumb shit<br />
&gt;&gt; # What doth life?<br />
&gt;&gt; print hirst.living<br />
AttributeError: Skull has no attribute 'living'</code></p>
<p>&#8230; because hirst now points to a skull, and not the shark.</p>
<p>I could go on, but I&#8217;m impatient to see what google ads start popping up for this thread.</p>
<p>-a<br />
(Actually, you probably really need to hear about scoping, but I&#8217;ll do that in terms of objectification and the male gaze.)</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.kellbot.com/2008/11/programming-pointers-for-art-students/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Python library for generating DXF files</title>
		<link>http://www.kellbot.com/2008/11/python-library-for-generating-dxf-files/</link>
		<comments>http://www.kellbot.com/2008/11/python-library-for-generating-dxf-files/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 23:22:29 +0000</pubDate>
		<dc:creator>Kellbot</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SDXF Documentation]]></category>
		<category><![CDATA[autocad]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[dxf]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.kellbot.com/?p=88</guid>
		<description><![CDATA[I've gotten a little frustrated with the limitations of using Processing to generate PDFs for laser cutting. Primarily, there's no support for "hairline" thickness lines, which add an extra step to getting things ready to lase, and there's no way to separate lines into different layers so it can be hard to work with ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><p>I&#8217;ve gotten a little frustrated with the limitations of using Processing to generate PDFs for laser cutting. Primarily, there&#8217;s no support for &#8220;hairline&#8221; thickness lines, which add an extra step to getting things ready to lase, and there&#8217;s no way to separate lines into different layers so it can be hard to work with the file later if you want to raster etch some lines and vector cut others.</p>
<p>Adam suggested looking into a DXF library someone had written for Python. Indeed, there is a very nice library. Unfortunately the original documentation for it seems to have gone missing.</p>
<p>I&#8217;ve started documenting the library, it&#8217;s called <a href="http://www.kellbot.com/sdxf-python-library-for-dxf/">SDXF</a> and is pretty thorough. I don&#8217;t know Python, or DXF, but so I&#8217;m picking up both as I go along.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kellbot.com/2008/11/python-library-for-generating-dxf-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

