<?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>Odie5533 &#187; Python</title>
	<atom:link href="http://odie5533.com/category/programming/python/feed" rel="self" type="application/rss+xml" />
	<link>http://odie5533.com</link>
	<description></description>
	<lastBuildDate>Mon, 06 Sep 2010 01:31:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Twisted Python RDT/RTSP Library and Stream Downloader</title>
		<link>http://odie5533.com/346-twisted-python-rdt-rtsp-library-and-stream-downloader</link>
		<comments>http://odie5533.com/346-twisted-python-rdt-rtsp-library-and-stream-downloader#comments</comments>
		<pubDate>Tue, 01 Jun 2010 03:09:51 +0000</pubDate>
		<dc:creator>odie5533</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://odie5533.com/?p=346</guid>
		<description><![CDATA[Twisted is a great framework for Python, but they don&#8217;t support RDT or RTSP protocol used for streaming Real video. The RDT/RTSP handler in MPlayer is a bit buggy and suffers from disconnect problems when receiving RTSP packets after switching to raw mode for RDT (though perhaps such occurrences are out of specification, they do [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://twistedmatrix.com/trac/">Twisted</a> is a great framework for Python, but they don&#8217;t support <a href="http://en.wikipedia.org/wiki/Real_Data_Transport">RDT</a> or <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> protocol used for streaming Real video. The RDT/RTSP handler in <a href="http://en.wikipedia.org/wiki/MPlayer">MPlayer</a> is a bit buggy and suffers from disconnect problems when receiving RTSP packets after switching to raw mode for RDT (though perhaps such occurrences are out of specification, they do still occur). Thus, I set off to create my own.</p>

<p>The library includes support for RTSP, RDT, <a href="http://en.wikipedia.org/wiki/Session_Description_Protocol">SDPP</a>, ASMRP, and <a href="https://common.helixcommunity.org/2003/HCS_SDK_r5/htmfiles/rmff.htm">RMFF</a>, and it is all in pure Python and Pythonically coded where possible (see sdpp.py). It requires Python 2.6 and the Twisted Python library. The library has only one known bug, and I can&#8217;t for the life of me find it. Perhaps another set of eyes will effect a solution.</p>

<p>To use the library to simply download an RTSP stream:
<code>python rdt.py -u &lt;URL&gt; -f &lt;FILENAME&gt;</code></p>

<p>If you like this post, <b>please comment</b>. Commenting is fast, anonymous, and greatly appreciated.
<a href="/programs/RDT-RTSP/Twisted RDT-RTSP Library 2010-07-05.zip">Download the Twisted Python RDT-RTSP Library here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://odie5533.com/346-twisted-python-rdt-rtsp-library-and-stream-downloader/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Automatically Download Songs from Grooveshark</title>
		<link>http://odie5533.com/334-automatically-download-songs-from-grooveshark</link>
		<comments>http://odie5533.com/334-automatically-download-songs-from-grooveshark#comments</comments>
		<pubDate>Mon, 31 May 2010 01:11:42 +0000</pubDate>
		<dc:creator>odie5533</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://odie5533.com/?p=334</guid>
		<description><![CDATA[Because manually downloading songs from Grooveshark is too tedious, here&#8217;s a nice process to automatically save all the songs you listen to on Grooveshark. Unlike other methods, this does not use any extra bandwidth so you don&#8217;t need to listen to the song and download it, once is enough. Update After doing some searching, I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>Because <a href="http://odie5533.com/downloading-mp3-songs-from-grooveshark-322">manually downloading songs from Grooveshark</a> is too tedious, here&#8217;s a nice process to automatically save all the songs you listen to on Grooveshark. Unlike other methods, this does not use any extra bandwidth so you don&#8217;t need to listen to the song and download it, once is enough.<span id="more-334"></span></p>

<p><font color="red">Update</font> After doing some searching, I&#8217;ve found a FireFox plugin caled <a href="https://addons.mozilla.org/en-US/firefox/addon/3006/">Video DownloadHelper</a> which performs much the same function as the program I&#8217;ve written, but is more stable and integrated directly into FireFox. Its only downside is that it doesn&#8217;t automatically download the songs as you listen, you need to start downloading the ones you want. But it is more stable than my program and much more user friendly. Be sure to use Mp3tag (listed below) to quickly rename all the files you&#8217;ve downloaded.</p>

<p>Download and install the following pre-requisites:
<ol><li>Python 2.6.5 &#8211; <a href="http://www.python.org/ftp/python/2.6.5/python-2.6.5.msi">http://www.python.org/ftp/python/2.6.5/python-2.6.5.msi</a></li>
<li>dpkt 1.7 &#8211; <a href="http://dpkt.googlecode.com/files/dpkt-1.7.win32.exe">http://dpkt.googlecode.com/files/dpkt-1.7.win32.exe</a></li>
<li>pypcap 1.1 &#8211; <a href="/programs/Grooveshark/pcap-1.1.win32-py2.6.exe">pcap-1.1.win32-py2.6.exe</a></li>
</ol></p>

<p>Save this Python program into a folder of its own: <a href="/programs/Grooveshark/groovegrab.py">groovegrab.py</a></p>

<p>Run the program using <code>python groovegrab.py</code></p>

<p>As you listen to songs on Grooveshark, the folder will populate with mp3 files. Each are named Track_X.mp3. But there&#8217;s a really neat, easy way to rename them. Download <a href="http://www.mp3tag.de/en/download.html">Mp3tag</a> and in the program go to File > Change Directory and choose the directory where the mp3 files are. Select all the mp3 files in the main pane and go to Convert > Tag to Filename.</p>

<p>Enjoy your perfectly named mp3 files from a Grooveshark listening session.</p>

<p>Thanks to dugsong for <a href="http://code.google.com/p/dpkt/">dpkt</a> and <a href="http://code.google.com/p/pypcap/">pypcap</a>, and thanks to Andrew Brampton for his <a href="http://bramp.net/blog/follow-http-stream-with-decompression">tutorial on dpkt/pypcap</a>!</p>

<p>If you liked this post, please comment. Commeting is fast, anonymous, and greatly appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://odie5533.com/334-automatically-download-songs-from-grooveshark/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Wiki Parser v0.1 &#8211; Read Wikipedia Offline</title>
		<link>http://odie5533.com/236-wikiparserv01-read-wikipedia-offline</link>
		<comments>http://odie5533.com/236-wikiparserv01-read-wikipedia-offline#comments</comments>
		<pubDate>Thu, 15 Apr 2010 04:13:00 +0000</pubDate>
		<dc:creator>odie5533</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Wiki]]></category>

		<guid isPermaLink="false">http://odie5533.com/?p=236</guid>
		<description><![CDATA[Wiki Parser is a Python program which takes a Wikipedia dump and hosts it locally, offline. To save disk space, Wiki Parser keeps the dump compressed and serves articles to a web browser without decompressing the entire file. I originally created it to serve up Wikipedia articles from a compressed version, but seek times on [...]]]></description>
			<content:encoded><![CDATA[<p>Wiki Parser is a Python program which takes a Wikipedia dump and hosts it locally, offline. To save disk space, Wiki Parser keeps the dump compressed and serves articles to a web browser without decompressing the entire file. I originally created it to serve up Wikipedia articles from a compressed version, but seek times on the compressed file were too great and the program really only works on smaller Wikipedias such as the Simple English Wikipedia.</p>

<p><a href="http://download.wikimedia.org/simplewiki/20100401/">Download the latest dump of Simple English Wikipedia here</a>
<a href="programs/WikiParser/Wiki Parser v0.1.zip">Download Wiki Parser v0.1 here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://odie5533.com/236-wikiparserv01-read-wikipedia-offline/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Twisted Python library to replicate packets</title>
		<link>http://odie5533.com/195-using-twisted-python-library-to-replicate-packets</link>
		<comments>http://odie5533.com/195-using-twisted-python-library-to-replicate-packets#comments</comments>
		<pubDate>Sat, 01 Aug 2009 13:43:54 +0000</pubDate>
		<dc:creator>odie5533</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Twisted]]></category>

		<guid isPermaLink="false">http://odie5533.com/?p=195</guid>
		<description><![CDATA[Most of the programming I do is packet replication and automation. My workflow is to first analyze the handshakes and protocols in FireFox with the Tamper Data add-on. I use WireShark to nail down the interaction details. Finally, I recreate everything using the Twisted library in Python. Here&#8217;s a boilerplate to quickly recreate packets using [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the programming I do is packet replication and automation. My workflow is to first analyze the handshakes and protocols in FireFox with the <a href="http://tamperdata.mozdev.org/">Tamper Data add-on</a>. I use <a href="http://www.wireshark.org/">WireShark</a> to nail down the interaction details. Finally, I recreate everything using the <a href="http://twistedmatrix.com/trac/">Twisted</a> library in Python.</p>

<p>Here&#8217;s a boilerplate to quickly recreate packets using Twisted. It requests and saves a copy of Google.com. Simply replace with your favorite URL, or un-comment the two lines and comment the other getPage to send a POST request.</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> client
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
&nbsp;
agent = <span style="color: #483d8b;">&quot;Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12 (.NET CLR 3.5.30729)&quot;</span>
headers = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'Accept'</span>:<span style="color: #483d8b;">'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'</span>, <span style="color: #483d8b;">'Accept-Language'</span>:<span style="color: #483d8b;">'en-us,en;q=0.5'</span>, <span style="color: #483d8b;">'Accept-Charset'</span>:<span style="color: #483d8b;">'ISO-8859-1,utf-8;q=0.7'</span><span style="color: black;">&#125;</span>
headers_post = headers.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
headers_post<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Content-Type'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'application/x-www-form-urlencoded'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> success<span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Success!&quot;</span>
    f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'save.html'</span>,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
    f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    reactor.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> failure<span style="color: black;">&#40;</span>fail<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> fail.<span style="color: black;">getErrorMessage</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    reactor.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> request<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    url = <span style="color: #483d8b;">&quot;http://google.com/&quot;</span>
    d = client.<span style="color: black;">getPage</span><span style="color: black;">&#40;</span>url, headers=headers, agent=agent<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;">#post_data = &quot;&quot;</span>
    <span style="color: #808080; font-style: italic;">#d = client.getPage(url, postdata=post_data, method=&quot;POST&quot;, headers=headers_post, agent=agent)</span>
    d.<span style="color: black;">addCallback</span><span style="color: black;">&#40;</span>success<span style="color: black;">&#41;</span>.<span style="color: black;">addErrback</span><span style="color: black;">&#40;</span>failure<span style="color: black;">&#41;</span>
&nbsp;
request<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>


<p>To make it continually request the same page, change the success method to this:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> success<span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Success!&quot;</span>
    f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'save.html'</span>,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
    f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    reactor.<span style="color: black;">callLater</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.5</span>, request<span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://odie5533.com/195-using-twisted-python-library-to-replicate-packets/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python FTP upload using Twisted</title>
		<link>http://odie5533.com/187-python-ftp-upload-using-twisted</link>
		<comments>http://odie5533.com/187-python-ftp-upload-using-twisted#comments</comments>
		<pubDate>Mon, 22 Dec 2008 03:23:01 +0000</pubDate>
		<dc:creator>odie5533</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[Twisted]]></category>

		<guid isPermaLink="false">http://odie5533.com/?p=187</guid>
		<description><![CDATA[Here’s a snippet for uploading a file via FTP using the Twisted Python library available from TwistedMatrix. from twisted.protocols.basic import FileSender from twisted.protocols.ftp import FTPClient from twisted.internet.protocol import ClientCreator from twisted.internet import reactor &#160; def fileTransferFail&#40;failure&#41;: failure.printTraceback&#40;&#41; reactor.stop&#40;&#41; &#160; def cbStore&#40;consumer, filename&#41;: fs = FileSender&#40;&#41; d = fs.beginFileTransfer&#40;open&#40;filename, 'r'&#41;, consumer&#41; d.addCallback&#40;lambda _: consumer.finish&#40;&#41;&#41;.addErrback&#40;fileTransferFail&#41; return d [...]]]></description>
			<content:encoded><![CDATA[<p>Here’s a snippet for uploading a file via FTP using the Twisted Python library available from <a href="http://twistedmatrix.com/">TwistedMatrix</a>.</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">protocols</span>.<span style="color: black;">basic</span> <span style="color: #ff7700;font-weight:bold;">import</span> FileSender
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">protocols</span>.<span style="color: black;">ftp</span> <span style="color: #ff7700;font-weight:bold;">import</span> FTPClient
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span>.<span style="color: black;">protocol</span> <span style="color: #ff7700;font-weight:bold;">import</span> ClientCreator
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> fileTransferFail<span style="color: black;">&#40;</span>failure<span style="color: black;">&#41;</span>:
    failure.<span style="color: black;">printTraceback</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    reactor.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> cbStore<span style="color: black;">&#40;</span>consumer, filename<span style="color: black;">&#41;</span>:
    fs = FileSender<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    d = fs.<span style="color: black;">beginFileTransfer</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>, consumer<span style="color: black;">&#41;</span>
    d.<span style="color: black;">addCallback</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> _: consumer.<span style="color: black;">finish</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">addErrback</span><span style="color: black;">&#40;</span>fileTransferFail<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> d
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> connectionMade<span style="color: black;">&#40;</span>ftpClient, filename, uploadpath = <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> uploadpath <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
        uploadpath = filename
    d1, d2 = ftpClient.<span style="color: black;">storeFile</span><span style="color: black;">&#40;</span>uploadpath<span style="color: black;">&#41;</span>
    d1.<span style="color: black;">addCallback</span><span style="color: black;">&#40;</span>cbStore, filename<span style="color: black;">&#41;</span>.<span style="color: black;">addErrback</span><span style="color: black;">&#40;</span>fileTransferFail<span style="color: black;">&#41;</span>
    d2.<span style="color: black;">addCallback</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> _: reactor.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> d2
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> sendFile<span style="color: black;">&#40;</span>host, port, username, password, filename, uploadpath<span style="color: black;">&#41;</span>:
    creator = ClientCreator<span style="color: black;">&#40;</span>reactor, FTPClient, username, password<span style="color: black;">&#41;</span>
    d = creator.<span style="color: black;">connectTCP</span><span style="color: black;">&#40;</span>host, port<span style="color: black;">&#41;</span>
    d.<span style="color: black;">addCallback</span><span style="color: black;">&#40;</span>connectionMade, filename, uploadpath<span style="color: black;">&#41;</span>.<span style="color: black;">addErrback</span><span style="color: black;">&#40;</span>fileTransferFail<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> d
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    username = <span style="color: #483d8b;">'GoogleUser'</span>
    password = <span style="color: #483d8b;">'GoogleSuperSecretPassword'</span>
    host = <span style="color: #483d8b;">'google.com'</span>
    port = <span style="color: #ff4500;">21</span>
    filename = <span style="color: #483d8b;">'index.html'</span>
    uploadto = <span style="color: #483d8b;">'/public_html/index.html'</span>
    sendFile<span style="color: black;">&#40;</span>host, port, username, password, filename, uploadto<span style="color: black;">&#41;</span>
    reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://odie5533.com/187-python-ftp-upload-using-twisted/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
