<?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>IT妖怪 &#187; Python</title>
	<atom:link href="http://blog.ityao.com/archives/category/backend/python/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.ityao.com</link>
	<description>热爱生活，热爱程序</description>
	<lastBuildDate>Wed, 03 Aug 2011 02:56:30 +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>[Twisted]-Hello World</title>
		<link>http://blog.ityao.com/archives/450</link>
		<comments>http://blog.ityao.com/archives/450#comments</comments>
		<pubDate>Mon, 02 Nov 2009 04:12:35 +0000</pubDate>
		<dc:creator>Daniel Yang</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[twisted]]></category>

		<guid isPermaLink="false">http://blog.ityao.com/?p=450</guid>
		<description><![CDATA[使用一下twisted的getPage功能. 体会python语法特点. 代码: 说明: 1. python中, 注释一般是用 &#8221;&#8217; 这种方式 &#8221;&#8217; 的. 2. 没有{}来表示运行块, 用indent的深度来识别; 不适用分号来表示语句结束. 3. 指定encode的标记十分特别. 4. callback方式的. 5. Deferred对象来自于twisted.internet.defer.Deferred. 6.Python是case sensitive&#8230; 如果程序运行正常, 可以获得google首页所有的html代码.]]></description>
			<content:encoded><![CDATA[<p>使用一下twisted的getPage功能. 体会python语法特点.</p>
<p>代码:</p>
<pre class="brush: python; title: ; notranslate">
# -*- coding: utf-8 -*-

from twisted.web.client import getPage
from twisted.internet import reactor

def printContents(contents):

	print &quot;获得内容:&quot;
	print contents.upper()

	reactor.stop()

def errorHandler(error):
	print error

	reactor.stop()

#请求
deferred = getPage(&quot;http://localhost/test/Babel_s/yappr.php&quot;)

#添加回调
deferred.addCallback(printContents)
deferred.addErrback(errorHandler)

#反应堆~
reactor.run()
</pre>
<p>说明:<br />
1. python中, 注释一般是用 &#8221;&#8217; 这种方式 &#8221;&#8217; 的.<br />
2. 没有{}来表示运行块, 用indent的深度来识别; 不适用分号来表示语句结束.<br />
3. 指定encode的标记十分特别.<br />
4. callback方式的.<br />
5. Deferred对象来自于twisted.internet.defer.Deferred.<br />
6.Python是case sensitive&#8230;</p>
<p>如果程序运行正常, 可以获得google首页所有的html代码.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ityao.com/archives/450/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Twisted]基础介绍.安装</title>
		<link>http://blog.ityao.com/archives/448</link>
		<comments>http://blog.ityao.com/archives/448#comments</comments>
		<pubDate>Mon, 02 Nov 2009 04:10:50 +0000</pubDate>
		<dc:creator>Daniel Yang</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[twisted]]></category>

		<guid isPermaLink="false">http://blog.ityao.com/?p=448</guid>
		<description><![CDATA[介绍 twisted 是python下一个事件驱动的网络引擎库, 支持很多种的协议. 它包含了一个web服务, 多种IM客户端,服务端, 邮件服务协议. 由于规模庞大, twisted分成了几个sub-project. 一起或者分开发布. 稳定性 twisted并不是从0.1发展到8.1.0的. 是一下子跳到8的. 8.1.0是最新的stable的包. 从successful stories来看, 它的客户包括NASA这种级别的(http://twistedmatrix.com/trac/wiki/SuccessStories#NASA). 效率 twisted具有惊人的效率. 下面是一些twisted和Mina对比测试数据. http://blog.bluendo.com/ff/mina-and-twisted-matrix-benchmarks Leopard下的安装 下载, 运行. 建议是从源码安装。 windows下，可能需要安装openssl相关的库。]]></description>
			<content:encoded><![CDATA[<p>介绍<br />
twisted 是python下一个事件驱动的网络引擎库, 支持很多种的协议.<br />
它包含了一个web服务, 多种IM客户端,服务端, 邮件服务协议. 由于规模庞大, twisted分成了几个sub-project. 一起或者分开发布.<br />
<span id="more-448"></span></p>
<p>稳定性<br />
twisted并不是从0.1发展到8.1.0的. 是一下子跳到8的. 8.1.0是最新的stable的包.<br />
从successful stories来看, 它的客户包括NASA这种级别的(http://twistedmatrix.com/trac/wiki/SuccessStories#NASA). </p>
<p>效率<br />
twisted具有惊人的效率.<br />
下面是一些twisted和Mina对比测试数据.</p>
<p>http://blog.bluendo.com/ff/mina-and-twisted-matrix-benchmarks</p>
<p>Leopard下的安装<br />
下载, 运行.<br />
建议是从源码安装。<br />
windows下，可能需要安装openssl相关的库。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ityao.com/archives/448/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twisted下的Flash文件上传</title>
		<link>http://blog.ityao.com/archives/59</link>
		<comments>http://blog.ityao.com/archives/59#comments</comments>
		<pubDate>Tue, 08 Sep 2009 14:22:49 +0000</pubDate>
		<dc:creator>Daniel Yang</dc:creator>
				<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[twisted.web2]]></category>
		<category><![CDATA[文件上传]]></category>

		<guid isPermaLink="false">http://blog.cnflex.org/?p=59</guid>
		<description><![CDATA[Flash Player的文件上传很是让人憋火。除了PHP，CF，Asp.net和它配合良好--不会有差错。
Python下就没有那么方便了。
我曾经在web.py下，试了几天，后来没法子用Django才搞定的。
总是有莫名其妙的问题。]]></description>
			<content:encoded><![CDATA[<p>Flash Player的文件上传很是让人憋火。除了PHP，CF，Asp.net和它配合良好&#8211;不会有差错。<br />
Python下就没有那么方便了。<br />
我曾经在web.py下，试了几天，后来没法子用Django才搞定的。<br />
总是有莫名其妙的问题。<br />
<span id="more-59"></span><br />
我在Twisted的邮件列表里发了一通：</p>
<p>I&#8217;ve been trying to upload files to twisted.web2 backend(FileSaver) with Adobe Flex and I am always getting IOError.<br />
(code see below)<br />
After some sniffing work, I found the error messages returned by twisted.web2:</p>
<p>Unexpected data on same line as boundary: &#8216;&#8211;&#8217;</p>
<p>I believe this flex app works with php backend. So this might be with twisted.web2 or flash player&#8217;s malformed post data.</p>
<p>It seems the twisted error comes from fileupload.py:<br />
(I added 2 prints)</p>
<pre class="brush: plain; title: ; notranslate">
    def _readBoundaryLine(self):
        print &quot;_readBoundaryLine&quot;
        line = self.stream.readline(size=1024)
        if isinstance(line, defer.Deferred):
            line = defer.waitForDeferred(line)
            yield line
            line = line.getResult()
        print line
        if line == &quot;--\r\n&quot;:
            # THE END!
            yield False
            return
        elif line != &quot;\r\n&quot;:
            raise MimeFormatError(&quot;Unexpected data on same line as boundary: %r&quot; % (line,))
        yield True
        return
    _readBoundaryLine = defer.deferredGenerator(_readBoundaryLine)
</pre>
<p>with html uploading form, it outputs:</p>
<p>2009-08-20 23:06:08+0800 [-] _readBoundaryLine<br />
2009-08-20 23:06:08+0800 [-] &#8211;<br />
2009-08-20 23:06:08+0800 [-]<br />
2009-08-20 23:06:08+0800 [-] <POST /upload (1, 1)> &#8212;&#8212;&#8211;<br />
2009-08-20 23:06:08+0800 [-] FileUpload</p>
<p>but with flex uploading:</p>
<p>2009-08-20 23:06:27+0800 [-] _readBoundaryLine<br />
2009-08-20 23:06:27+0800 [-]<br />
2009-08-20 23:06:27+0800 [-]<br />
2009-08-20 23:06:27+0800 [-] _readBoundaryLine<br />
2009-08-20 23:06:27+0800 [-]<br />
2009-08-20 23:06:27+0800 [-]<br />
2009-08-20 23:06:27+0800 [-] _readBoundaryLine<br />
2009-08-20 23:06:27+0800 [-] &#8211;</p>
<p>Same file, different posted data.</p>
<p>Any ideas?</p>
<p>Does this happen to be a bug, or is there any way to walk around this?</p>
<p>My testing code:</p>
<p>python code:</p>
<pre class="brush: plain; title: ; notranslate">
from twisted.web2 import http_headers, resource, \
	static, server, channel, http, responsecode
from twisted.python import util

FORMHTML = &quot;&quot;&quot;
&lt;html&gt;
&lt;p&gt;for test&lt;/p&gt;
&lt;form action=&quot;/upload&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
	&lt;input type=&quot;file&quot; name=&quot;FileUpload&quot; /&gt;
	&lt;input type=&quot;submit&quot; value=&quot;Upload&quot;/&gt;
&lt;/form&gt;
&lt;/html&gt;
&quot;&quot;&quot;

class MyFileSaver(static.FileSaver):
	&quot;&quot;&quot;for test only&quot;&quot;&quot;
	def render(self, req):
		print req, '--------'
		if req.files:
			for fieldName in req.files:
				print fieldName

		return http.Response(responsecode.OK, {}, stream='ok')

class Toplevel(resource.Resource):
  addSlash = True
  def render(self, ctx):
	return http.Response(stream=FORMHTML)

  child_upload = MyFileSaver(util.sibpath(__file__, ''),
  	expectedFields=['FileUpload'],
  		allowedTypes=(
  			http_headers.MimeType('image', 'jpeg'),
  			http_headers.MimeType('image', 'png'),
  			http_headers.MimeType('image', 'gif'),
  		)
  	)
  child_swf = static.File(util.sibpath(__file__, 'FileUpload.swf'))
site = server.Site(Toplevel())

# Standard twisted application Boilerplate
from twisted.application import service, strports
application = service.Application(&quot;demoserver&quot;)
s = strports.service('tcp:8080', channel.HTTPFactory(site))
s.setServiceParent(application)
</pre>
<p>My flex code:</p>
<pre class="brush: plain; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;!-- http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/ --&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
        layout=&quot;vertical&quot;
        verticalAlign=&quot;middle&quot;
        backgroundColor=&quot;white&quot;
        creationComplete=&quot;init();&quot;&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            private var fileRef:FileReference;

            private const FILE_UPLOAD_URL:String = &quot;http://127.0.0.1:8080/upload&quot;;

            private function init():void {
                fileRef = new FileReference();
                fileRef.addEventListener(Event.SELECT, fileRef_select);
                fileRef.addEventListener(ProgressEvent.PROGRESS, fileRef_progress);
                fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
                fileRef.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
                fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSec);
            }

            private function browseAndUpload():void {
                fileRef.browse();
                message.text = &quot;&quot;;
            }

            private function ioErrorHandler(event:IOErrorEvent):void {
            	trace(event);
            }

            private function onSec(event:SecurityErrorEvent):void{
            	trace(event);
            }

            private function fileRef_select(evt:Event):void {
                try {
                    message.text = &quot;size (bytes): &quot; + numberFormatter.format(fileRef.size);

                    var req:URLRequest = new URLRequest();
                    req.url = FILE_UPLOAD_URL;
                    req.method = URLRequestMethod.POST;

                    fileRef.upload(req, &quot;FileUpload&quot;);
                } catch (err:Error) {
                    message.text = &quot;ERROR: zero-byte file&quot;;
                }
            }

            private function fileRef_progress(evt:ProgressEvent):void {
                progressBar.visible = true;
            }

            private function fileRef_complete(evt:Event):void {
                message.text += &quot; (complete)&quot;;
                progressBar.visible = false;
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:NumberFormatter id=&quot;numberFormatter&quot; /&gt;

    &lt;mx:Button label=&quot;Upload file&quot;
            click=&quot;browseAndUpload();&quot; /&gt;
    &lt;mx:Label id=&quot;message&quot; /&gt;
    &lt;mx:ProgressBar id=&quot;progressBar&quot;
            indeterminate=&quot;true&quot;
            visible=&quot;false&quot; /&gt;

&lt;/mx:Application&gt;
</pre>
<p>解决方法：<br />
暂时没有，但是有一个patch:</p>
<p>http://twistedmatrix.com/trac/ticket/2113</p>
<p>一起哭吧。。。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ityao.com/archives/59/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

