<?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/"
	>

<channel>
	<title>SamuraiBlog.com &#187; twisted</title>
	<atom:link href="http://samuraiblog.com/wordpress/tag/twisted/feed/" rel="self" type="application/rss+xml" />
	<link>http://samuraiblog.com/wordpress</link>
	<description>Cursing and Cigarettes</description>
	<pubDate>Sat, 24 Apr 2010 01:36:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>File Uploads in twisted.web</title>
		<link>http://samuraiblog.com/wordpress/2009/10/30/file-uploads-in-twisted-web/</link>
		<comments>http://samuraiblog.com/wordpress/2009/10/30/file-uploads-in-twisted-web/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 06:36:42 +0000</pubDate>
		<dc:creator>Samuel Sutch</dc:creator>
		
		<category><![CDATA[Articles]]></category>

		<category><![CDATA[Code]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[twisted]]></category>

		<category><![CDATA[twisted.web]]></category>

		<guid isPermaLink="false">http://samuraiblog.com/wordpress/?p=172</guid>
		<description><![CDATA[Twisted is a python package for building asynchronous applications. It&#8217;s wide range of uses and sub-packages are daunting; I&#8217;ve developed with Twisted for several years now and still find it&#8217;s vast expanse of code intimidating. Twisted in 60 seconds is an excellent resource for anyone beginning with twisted and twisted.web. Here, we&#8217;re filling yet another [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://twistedmatrix.com/trac/">Twisted</a> is a python package for building asynchronous applications. It&#8217;s wide range of uses and sub-packages are daunting; I&#8217;ve developed with Twisted for several years now and still find it&#8217;s vast expanse of code intimidating. <a href="http://jcalderone.livejournal.com/50562.html">Twisted in 60 seconds</a> is an excellent resource for anyone beginning with twisted and twisted.web. Here, we&#8217;re filling yet another gap in the twisted documentation.</p>
<p>Not surprisingly, handling file uploads in twisted.web is straight forward. Files are accessed through the <code>args</code> attribute of the <code>request</code> parameter to your render method. Since there can be multiple files for that given field, whether there are one or more, <code>request.args[FIELD_NAME]</code> will always be an array.</p>
<pre lang="python">
from twisted.internet import reactor
from twisted.web import resource, server

class FileUploadService(resource.Resource):
  def render_GET(self, request):
    return '''
<form enctype="multipart/form-data" method="post" action=".">
<input type="file" name="da_file"/>
<input type="submit"/>
              </form>

'''

  def render_POST(self, request):
    return 'You submitted a file that was %i bytes' % len(request.args['da_file'][0])

root = resource.Resource()
root.putChild("upload", FileUploadService())
factory = server.Site(root)
reactor.listenTCP(8088, factory)
reactor.run()
</pre>
<p>According to the twisted.web documentation. This method of handling file uploads is not preferred. In fact, the preferred way is to use the more complex twisted.web2 package which supports streaming uploads. However, the twisted devs are working on porting most of twisted.web2&#8217;s features to twisted.web in an effort to consolidate the two. In production code, I have chosen to stick with twisted.web since it seems to be the package which will remain the most compatible with future versions of Twisted.</p>
]]></content:encoded>
			<wfw:commentRss>http://samuraiblog.com/wordpress/2009/10/30/file-uploads-in-twisted-web/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
