﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-Steven.L-随笔分类-python</title><link>http://www.cnitblog.com/stevenluan/category/4893.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 23:30:18 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 23:30:18 GMT</pubDate><ttl>60</ttl><item><title>《转载》Python几种开发工具介绍</title><link>http://www.cnitblog.com/stevenluan/archive/2007/03/07/23702.html</link><dc:creator>Steven.L</dc:creator><author>Steven.L</author><pubDate>Wed, 07 Mar 2007 04:24:00 GMT</pubDate><guid>http://www.cnitblog.com/stevenluan/archive/2007/03/07/23702.html</guid><wfw:comment>http://www.cnitblog.com/stevenluan/comments/23702.html</wfw:comment><comments>http://www.cnitblog.com/stevenluan/archive/2007/03/07/23702.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stevenluan/comments/commentRss/23702.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stevenluan/services/trackbacks/23702.html</trackback:ping><description><![CDATA[
		<h2> </h2>
		<br />
		<span style="FONT-SIZE: 10.5pt">blueski推荐 [2006-6-6]</span>
		<br />
		<span style="FONT-SIZE: 10.5pt">出处：cppblog.com</span>
		<br />
		<span style="FONT-SIZE: 10.5pt">作者：任我行</span>
		<br />
		<span style="FONT-SIZE: 10.5pt"> <font size="+0"><br /><br /><p style="FONT-SIZE: 9pt"></p><div><div><div><strong>1 IDLE</strong><br />1.1 IDLE是python创初人Guido van Rossum使用python and Tkinter来创建的一个集成开发环境。要使用IDLE必须安装python and Tkinter。<br />1.2 特性：自动缩进，彩色编码，命令历史(Alt+p,Alt+n)和单词自动(Alt+/)完成。<br />1.3 用IDLE执行Tkinter程序，不要在程序中包括mainloop。IDLE本身就是Tkinter应用程序，它会自动调用mainloop。再调用一次mainloop会与IDLE的事件循环冲突，造成运行时错误。<br /><br /><strong>2 BlackAdder</strong><br />2.1 BlackAdder支持windows and linux环境。用它创建的程序可在任何一种平台上运行，负责维护它的是TheKompany.com。他们发布了该软件的个人版，只提供有限的支持；以及专业版，需要许可，面向商业软件开发者。详情请访问<a href="http://www.thekompany.com/products/blackadder。" target="_blank">www.thekompany.com/products/blackadder。</a><br /><br /><strong>3 PythonWorks</strong><b><br /></b>3.1 它由SecretLabs公司发布，该公司致力于Python项目的开发与部署提供相应的工具。PythonWorks是一种“快速应用程序开发”（RAD）工具，程序员可借助它快速地创建、调试和发布程序。<br />3.2 PythonWorks包括一个HTML/XML编辑器、具有完全索引的文档、一个源码管理系统（用于项目分组）、自定义的代码编辑特性以及一个GUI布局编辑器。支持jython。<br />3.3 允许用户以多种形式对项目进行打包：源码形式、可执行程序形式以及.pyc形式。<br />3.4 有版权，需购买，详情请访问<a href="http://www.pythonware.com/products/works/index.htm" target="_blank">www.pythonware.com/products/works/index.htm</a><br /><br /><strong>4 Wing IDE</strong><br />4.1 Wing IDE由Archaeopteryx Software公司开发与支持，适用于windows and linux环境。它提供一个源码分析器和浏览器、项目管理能力以及文本编辑器和调试器。<br />4.2 它由python编写，并包含python源码。购买者可自定义源码，以适应自已的要求，但是，不可将修改过的代码用于赢利目的。<br />4.3 全功能演示版可在archaeopteryx.com/wingide下载。<br /><br /><strong>5 Pythonwin</strong><br />5.1 Pythonwin集成开发环境包含在由ActiveState出品的ActivePython中，ActivePython是一个二进制build。其中包括用于XML处理的expat模块，以及一系列windows工具。<br />5.2 提供的特性有：彩色编码、源代码折叠、单词完成及自动缩进。调试器支持事后诊断功能、标准的单步调试、断点设计及变量监视。<br />5.3 详情请访问aspn.activestate.com/activepython。<br /><br /><strong>6 Komodo</strong><br />6.1 ActiveState还为python提供了另一个IDE，名为Komodo，可用它在windows and linux上开发程序。Komodo最大的特点是，它是Perl,PHP,Tcl,HTML,XML AND XSLT的一种集成开发环境。<br /><br /><strong>7 Boa Constructor</strong><br />7.1 Boa是一个跨平台的python IDE和WxPython GUI。它提供可视化的编程和操作框架，能方便地进行程序的设计。它有一个对象浏览器，并提供有各种资源的视图，有一个html文档生成器，还有一个先进的调试器和完整的帮助系统。Boa还提供对zope的支持，能添加、删除、复制、剪切、导入和导出zope中的对象。能编辑对象属性和进行python脚本的调试。Boa是用python基于WxPython库编写的。 用起来就象vb或delphi一样方便。<br />7.2 下载地址是：<a href="http://boa-constructor.sourceforge.net/" target="_blank">http://boa-constructor.sourceforge.net/</a>，要安装Boa要先安装合适版本的python和WxPython。<br /><br />在windows平台我就是用pythonwin，感觉还不错，和idle差不多，在windows平台的可考虑用它。</div><div><strong>Python </strong><strong>集成開發環境</strong><b><br /></b><br />Take a look at <a href="http://wiki.python.org/moin/PythonEditors">PythonEditors</a> if you don't want IntegratedDevelopmentEnvironments. </div><div><img id="_x0000_i1025" height="15" alt="(!)" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/idea.png" width="15" border="0" /><strong>Please keep wiki links as wiki links, use external links only if there is no existing page for the IDE.</strong></div><table cellpadding="0" width="759" border="1"><tbody><tr><td width="129"><div><strong>Name</strong></div></td><td width="167"><div><strong>Platform</strong></div></td><td width="455"><div><strong>Notes</strong></div></td></tr><tr><td width="129"><div><a href="http://anjuta.sourceforge.net/"><img id="_x0000_i1029" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />Anjuta</a></div></td><td width="167"><div>Unix + GNOME </div></td><td width="455"><div>Cross-language IDE; syntax coloring &amp; folding for Python. </div></td></tr><tr><td width="129"><div><a href="http://www.python-ide.com/arachno_python.php"><img id="_x0000_i1030" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />ArachnoPython</a></div></td><td width="167"><div>Windows, Linux x86 </div></td><td width="455"><div>Commercial IDE for Python, support Webware and Python Unit. (not yet available for sale) </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/BlackAdder">BlackAdder</a></div></td><td width="167"><div>Windows/Linux </div></td><td width="455"><div>Commercial with all the features of IDLE, support for Ruby, and Qt GUI editor. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/BoaConstructor">BoaConstructor</a></div></td><td width="167"><div>Python + wxPython </div></td><td width="455"><div>RAD GUI building IDE, similar to Delphi. </div></td></tr><tr><td width="129"><div><a href="http://www.newplanetsoftware.com/jcc/jcc.php"><img id="_x0000_i1031" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />Code Crusader</a></div></td><td width="167"><div>Linux x86 </div></td><td width="455"><div>Commercial IDE with support to 30 other languages. </div></td></tr><tr><td width="129"><div><a href="http://www.codeforge.com/"><img id="_x0000_i1032" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />CodeForge</a></div></td><td width="167"><div>Unix </div></td><td width="455"><div>Commercial cross-language IDE; nice multi-user project manager. </div></td></tr><tr><td width="129"><div><a href="http://drpython.sourceforge.net/"><img id="_x0000_i1033" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />DrPython</a></div></td><td width="167"><div>Python + wxPython </div></td><td width="455"><div>Simple, clean editing environment for Python intended primarily for use in schools. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/Eclipse">Eclipse</a></div></td><td width="167"><div>Java + SWT </div></td><td width="455"><div>See <a href="http://wiki.python.org/moin/EclipsePythonIntegration">EclipsePythonIntegration</a> for Python support. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/eric">eric</a></div></td><td width="167"><div>Python + <a href="http://wiki.python.org/moin/PyQt">PyQt</a> + QScintilla </div></td><td width="455"><div>Popular python IDE with interfaces to Qt Designer, Qt Linguist, unittest. </div></td></tr><tr><td width="129"><div><a href="http://gazpacho.sicem.biz/"><img id="_x0000_i1034" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />Gazpacho</a></div></td><td width="167"><div>PyGTK </div></td><td width="455"><div>A GUI designer for (Py)GTK, similar to Glade. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/IDEStudio">IDEStudio</a></div></td><td width="167"><div>Python + Tkinter + Tix </div></td><td width="455"><div>An extended version of IDLE using <a href="http://wiki.python.org/moin/Tix">Tix</a>. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/IDLE">IDLE</a></div></td><td width="167"><div>Python + Tkinter </div></td><td width="455"><div>Part of Python distribution. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/IDLEfork">IDLEfork</a></div></td><td width="167"><div>Python + Tkinter </div></td><td width="455"><div>An experimental fork of IDLE. Since, Python 2.3, IDLEfork has become the new "main" Idle. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/jedit">jedit</a></div></td><td width="167"><div>Java 1.4+ VM </div></td><td width="455"><div>Popular multi-language IDE. </div></td></tr><tr><td width="129"><div><a href="http://www.kdevelop.org/"><img id="_x0000_i1035" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />KDevelop</a></div></td><td width="167"><div>Unix + KDE </div></td><td width="455"><div>Cross-language IDE for KDE - supports Python syntax/projects. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/Komodo">Komodo</a></div></td><td width="167"><div>Windows/Linux </div></td><td width="455"><div>Commercial multi-language and cross-platform IDE. Includes a GUI Builder. </div></td></tr><tr><td width="129"><div><a href="http://netbeans.org/"><img id="_x0000_i1036" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />Netbeans</a></div></td><td width="167"><div>Java </div></td><td width="455"><div>A Java IDE with an optional plugin for Jython. </div></td></tr><tr><td width="129"><div><a href="http://pida.berlios.de/"><img id="_x0000_i1037" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />PIDA</a></div></td><td width="167"><div>Any (with Python + GTK) </div></td><td width="455"><div>Python IDE (code browser, debugger, profiler) which uses Vim as its editor. </div></td></tr><tr><td width="129"><div><a href="http://mmm-experts.com/Products.aspx?ProductId=4"><img id="_x0000_i1038" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />PyScripter</a></div></td><td width="167"><div>Windows </div></td><td width="455"><div>A nice alternate Python IDE for Windows (debugger included), also <em>...providing a strong scripting solution for Delphi Applications.</em></div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/PythonCard">PythonCard</a></div></td><td width="167"><div>Python + wxPython </div></td><td width="455"><div>RAD GUI building IDE </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/PythonWin">PythonWin</a></div></td><td width="167"><div>Windows </div></td><td width="455"><div>Available either as part of <a href="http://wiki.python.org/moin/ActiveState">ActiveState</a>'s <a href="http://wiki.python.org/moin/ActivePython">ActivePython</a> distribution (which is not open source) or as part of the <a href="http://wiki.python.org/moin/Win32All">Win32All</a> extensions from Mark's pages (which is open source). </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/PythonWorks">PythonWorks</a> Pro </div></td><td width="167"><div>Windows/Linux </div></td><td width="455"><div>(no longer being developed) <a href="http://wiki.python.org/moin/SecretLabs">SecretLabs</a>' commercial IDE. Includes Tk layout editor. </div></td></tr><tr><td width="129"><div><a href="http://sourcenav.sf.net/"><img id="_x0000_i1039" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />SourceNavigator</a></div></td><td width="167"><div>Unix/Windows with Tcl/Tk </div></td><td width="455"><div>Cross-language IDE &amp; Source code analysis tool. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/SPE/"><img id="_x0000_i1040" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />SPE</a></div></td><td width="167"><div>Windows, Linux, MacOsX, FreeBSD, etc. </div></td><td width="455"><div>SPE is an open-source, multi-platform python IDE with <a href="http://www.wxpython.org/"><img id="_x0000_i1041" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />wxPython</a> interface. It has auto indentation &amp; completion, call tips, syntax coloring &amp; highlighting, uml viewer, class explorer, source index, auto todo list, sticky notes, pycrust shell, file browsers, drag&amp;drop, context help, <a href="http://www.blender.org/"><img id="_x0000_i1042" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />Blender</a> support, ... Spe ships with <a href="http://www.digitalpeers.com/pythondebugger"><img id="_x0000_i1043" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />rpdb2</a> (remote encrypted debugger), <a href="http://wxglade.sourceforge.net/"><img id="_x0000_i1044" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />wxGlade</a> (gui designer), <a href="http://pychecker.sourceforge.net/"><img id="_x0000_i1045" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />PyChecker</a> (source code doctor) and <a href="http://project5.freezope.org/kiki"><img id="_x0000_i1046" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />Kiki</a> (regex console). Note: SPE has a Windows install binary, all other platforms must work with the source code - although donations are currently being amassed to create an OS X version.</div></td></tr><tr><td width="129"><div><a href="http://www.xored.com/trustudio"><img id="_x0000_i1047" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />TruStudio Foundation</a></div></td><td width="167"><div>Linux/Windows/Mac </div></td><td width="455"><div>Python and PHP IDE - open source project and a foundation for development tools for scripting languages built on top of Eclipse Platform </div></td></tr><tr><td width="129"><div><a href="http://www.xored.com/trustudio"><img id="_x0000_i1048" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />TruStudio Professional</a></div></td><td width="167"><div>Linux/Windows/Mac </div></td><td width="455"><div>Advanced Python and PHP IDE - built on top of <a href="http://wiki.python.org/moin/TruStudio">TruStudio</a> Foundation and introducing many exceptional features </div></td></tr><tr><td width="129"><div><a href="http://www.activestate.com/VisualPython/"><img id="_x0000_i1049" height="11" alt="[WWW]" src="file:///C:/Documents%20and%20Settings/Administrator/桌面/红旗计划/设计/C++博客%20-%20任我行%20-%20Python几种开发工具介绍.files/moin-www.png" width="11" border="0" />Visual Python</a></div></td><td width="167"><div>Windows 2000/XP with Visual Studio.NET </div></td><td width="455"><div>(commercial) A Python plug-in for Visual Studio.NET (requires <a href="http://wiki.python.org/moin/ActivePython">ActivePython</a> or Win32all) </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/VisualWx">VisualWx</a></div></td><td width="167"><div>Windows </div></td><td width="455"><div>GUI designer for wxPython. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/WingIde">WingIde</a></div></td><td width="167"><div>Windows/Linux/Mac OS X/Unix </div></td><td width="455"><div>Commercial Python IDE with networked debugger, source browser, and syntax coloring/auto-completing editor. Free license for non-commercial open-source developers with an established project. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/WxDesigner">WxDesigner</a></div></td><td width="167"><div>Unix/Windows/Mac </div></td><td width="455"><div>Non-free Cross-language GUI dialog designer with integrated support for editing your Python callback functions. </div></td></tr><tr><td width="129"><div><a href="http://wiki.python.org/moin/WxGlade">WxGlade</a></div></td><td width="167"><div>Unix/Windows/Mac </div></td><td width="455"><div>A "designer" for <a href="http://wiki.python.org/moin/WxPython">WxPython</a> (requires wxPython) </div></td></tr></tbody></table></div><div></div><div>External collections: </div></div></font></span>
<img src ="http://www.cnitblog.com/stevenluan/aggbug/23702.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stevenluan/" target="_blank">Steven.L</a> 2007-03-07 12:24 <a href="http://www.cnitblog.com/stevenluan/archive/2007/03/07/23702.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《转载》吹响反击Ruby On Rails的号角</title><link>http://www.cnitblog.com/stevenluan/archive/2007/03/07/23693.html</link><dc:creator>Steven.L</dc:creator><author>Steven.L</author><pubDate>Wed, 07 Mar 2007 01:52:00 GMT</pubDate><guid>http://www.cnitblog.com/stevenluan/archive/2007/03/07/23693.html</guid><wfw:comment>http://www.cnitblog.com/stevenluan/comments/23693.html</wfw:comment><comments>http://www.cnitblog.com/stevenluan/archive/2007/03/07/23693.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stevenluan/comments/commentRss/23693.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stevenluan/services/trackbacks/23693.html</trackback:ping><description><![CDATA[
		<h1> </h1>
		<div class="dots">作者: , 　出处:IT专家网社区,　责任编辑: 叶江,　 
<div class="posted"><font face="宋体">2006-04-24 09:16</font></div></div>
		<div class="content">
				<h5>　　最近Ruby On Rails(ROR)的火热程度真的让人很惊讶，《程序员》也用了大量的篇幅来介绍这个新兴的Web Application快速开发框架。对新鲜东西向来都很好奇的我却对ROR提不起兴趣，也许是因为Ruby由日本人创造的缘故吧，我对Ruby一直有着抗拒感，尽管ROR是丹麦的小伙子David开发的。</h5>
				<div id="pip">
						<iframe marginwidth="0" marginheight="0" src="http://www.techtarget.com.cn/ttfiles/ttimages/cmsflash/webservices_pip.html" frameborder="0" width="300" scrolling="no" height="250" bordercolor="#000">
						</iframe>
				</div>
				<div class="guanggao">
						<span id="contentAdv">
						</span>
				</div>
				<p>　　最近Ruby On Rails(ROR)的火热程度真的让人很惊讶，《程序员》也用了大量的篇幅来介绍这个新兴的Web Application快速开发框架。对新鲜东西向来都很好奇的我却对ROR提不起兴趣，也许是因为Ruby由日本人创造的缘故吧，我对Ruby一直有着抗拒感，尽管ROR是丹麦的小伙子David开发的。</p>
				<p>　　抗拒归抗拒，ROR的成功着实是一种促使我去思考的动力。最简单的，Ruby与Python有着无比紧密的联系，那么在Python下有没有类似ROR的开发框架呢?答案是有，而且还非常的多!大家可以从两个site中找到很多很多的选择:WebProgramming和Web Development with Python。在这让人眼花缭乱的框架中，Zope是最为有名的，但是利用它来做开发却不见得高效，而且Zope所充当的角色更像是一个Application Server，与ROR所关注的范畴并不是很吻合;接下来就是CherryPy、Karrigell、TurboGears和Django了。对于CherryPy的了解其实是来源于TurboGears，因为TurboGears是Python世界中直接与ROR对话的框架，它是ROR的竞争者，因此最早进入了我的视野，大家可以看到ROR与TurboGears同时出现在了这条新闻中:TurboGears: Python on Rails? 。它最大的特点是并没有reinvent the wheel，而是充分利用了现有的优秀框架，提供了从前端至后端的解决方案，而CherryPy正是TurboGears的组成部分之一。Django也是ROR的竞争者，不过它还处于beta阶段，官方版本至今还没有发布，但是它已经受到了广泛重视，而国内的Python大牛limodou也更看好Django，认为它会比TurboGears做得更好。推荐Karrigell也是受到了limodou的影响，他对Karrigell也是宠爱有加，大家可以浏览他的blog，里面有一篇文章说明了他选择Karrigell的理由。</p>
				<p>　　说了半天，面对那么多的选择，大家是不是有些厌烦了呢?是的，我一开始见到这样的一个列表的时候，我也有点蒙了。我开始明白ROR的成功或许不仅因为它本身的强大，而是众人拾柴火焰高啊!Web Programming Framework在Python世界里所面临的困窘丝毫不亚于纷扰的J2EE世界。由于对这些框架了解还不多，我无法为大家作出Professional的推荐，只能够说说自己的想法了。</p>
				<p>　　在这众多框架中，我真正尝试过的只有CherryPy，它真的很小巧，而功能一点都不差。小小一百多K的体积竟然还包含了一个Http Server，所完成的应用程序可以放到一个.py文件中，部署起来会非常的方便，如果仅仅是开发一个小型网站，CherryPy是一个不错的选择。大家可以从IBM网站上的CherryPy for CGI programmers这篇文章中获得一些对CherryPy的感性认识。从中你会很惊喜的发现，代码量竟然是如此之少!由于CherryPy简单而不失强大，更重要的，它是TurboGears的重要组成部分，因此从CherryPy开始了解Python的Web Programming确实可以作为千里之行的第一步啊!</p>
				<p>　　在利用这些框架来开发Web应用程序，你会发现Template是一个十分重要概念。Template就是MVC中的View一样，定义了应用程序的表现层。因此要开发一个实用的Web应用程序，成熟易用的Template系统是必不可少的，在TurboGears中，Kid就是这样的一个Template System。同时，Karrigell框架中Template系统对中文的支持是最好的，更加重要的是你可以从limodou的blog上找到丰富的参考资料。因此，你需要做更规模稍大一点的网站的时候，不妨考虑一下Karrigell了。BTW:limodou兄是一个热心的人，呵呵～～～</p>
				<p>　　以上四个列举出来的框架就差Django还没有细说了，对于它我还真的不知道该说些什么了，大家还是看看Python wiki上对它的介绍吧。而且，limodou兄也是对它倍加推崇的。最后不得不提一下一种现在还没有出现也许在不久的将来会出现的Web开发方式——利用IronPython来开发ASP.NET!这种方式会在什么时候出现，它的影响将会有多大我们不得而知。但是这确实值得期待，我相信这样的开发方式会提高ASP.NET的开发效率。</p>
				<p>　　以前学习Python还是停留在一个纯粹的学习阶段，而现在有了ROR的刺激，我想对于Python的关注也将转移到Web Programming上了。我相信这样的一个学习实践过程是很有价值的，毕竟我学习.NET，学习J2EE，学习Portal，都是围绕着Web Programming。偶是一个花心的人，喜欢去了解很多的东西，虽然都不深刻。其实，了解那么多就是为了能够拓宽自己的思维，从不同的开发方式中为各种问题寻求更好的solution，也就是T型发展的一个面了，至于某一点的深入就放到以后再说吧!吾好读书，不求甚解也，呵呵～～～</p>
				<p>　　让我们都来关注Python在Web Programming的发展吧!欢迎各位与我一起讨论Web Programming with Python!</p>
				<p>　　[后记] 这只是一篇介绍性的文字，以“吹响反击Ruby On Rails的号角”似乎有点名不副其实了。虽然我在文中提到我对Ruby有些抗拒，但是我也深信对于技术不该有偏见。因此，我也会关注ROR的发展，毕竟它获得了广大开发人员的认可，忽视它可是不明智的!当然，我还是希望Python能够在这场竞争中迎头赶上，最终胜出。:)</p>
		</div>
<img src ="http://www.cnitblog.com/stevenluan/aggbug/23693.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stevenluan/" target="_blank">Steven.L</a> 2007-03-07 09:52 <a href="http://www.cnitblog.com/stevenluan/archive/2007/03/07/23693.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《转载》用 Python WSGI 混和并匹配 Web 组件</title><link>http://www.cnitblog.com/stevenluan/archive/2007/03/07/23691.html</link><dc:creator>Steven.L</dc:creator><author>Steven.L</author><pubDate>Wed, 07 Mar 2007 01:39:00 GMT</pubDate><guid>http://www.cnitblog.com/stevenluan/archive/2007/03/07/23691.html</guid><wfw:comment>http://www.cnitblog.com/stevenluan/comments/23691.html</wfw:comment><comments>http://www.cnitblog.com/stevenluan/archive/2007/03/07/23691.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/stevenluan/comments/commentRss/23691.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/stevenluan/services/trackbacks/23691.html</trackback:ping><description><![CDATA[
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr valign="top">
								<td width="100%">
										<p id="subtitle">学习有关构建具有最大灵活性的 Web 应用程序的 Python 标准</p>
										<img class="display-img" height="6" alt="" src="http://www.ibm.com/i/c.gif" width="1" />
								</td>
								<td class="no-print" width="192">
										<img height="18" alt="developerWorks" src="http://www.ibm.com/developerworks/cn/i/dw.gif" width="192" />
								</td>
						</tr>
				</tbody>
		</table>
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr valign="top">
								<td width="10">
										<img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="10" />
								</td>
								<td width="100%">
										<table class="no-print" cellspacing="0" cellpadding="0" width="160" align="right" border="0">
												<tbody>
														<tr>
																<td width="10">
																		<img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="10" />
																</td>
																<td>
																		<table cellspacing="0" cellpadding="0" width="150" border="0">
																				<tbody>
																						<tr>
																								<td class="v14-header-1-small">文档选项</td>
																						</tr>
																				</tbody>
																		</table>
																		<table class="v14-gray-table-border" cellspacing="0" cellpadding="0" border="0">
																				<tbody>
																						<tr>
																								<td class="no-padding" width="150">
																										<table cellspacing="0" cellpadding="0" width="143" border="0">
																												<img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="8" />
																												<form name="email" action="https://www.ibm.com/developerworks/secure/email-it.jsp">
																														<input type="hidden" value="学习使用 Python 在 Web 服务器中创建并重用组件。Python 社区创建了 Web 服务器网关接口（Web Server Gateway Interface，WSGI），这是创建跨服务器和框架工作的 Python Web 组件的标准。它提供了一种利用许多不同的 Web 工具开发 Web 应用程序的方法。本文介绍了 WSGI 并展示如何开发出能够集成到设计出色的 Web 应用程序中的组件。" name="body" />
																														<input type="hidden" value="用 Python WSGI 混和并匹配 Web 组件" name="subject" />
																														<input type="hidden" value="cn" name="lang" />
																														<script language="JavaScript" type="text/javascript">
																																<!--
document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="将此页作为电子邮件发送" /></td><td width="122"><p><a class="smallplainlink" href="javascript:document.email.submit();"><b>将此页作为电子邮件发送</b></a></p></td></tr>');
//-->
																														</script>
																														<tbody>
																																<tr valign="top">
																																		<td width="8">
																																				<img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="8" />
																																		</td>
																																		<td width="16">
																																				<img height="16" alt="将此页作为电子邮件发送" src="http://www.ibm.com/i/v14/icons/em.gif" width="16" vspace="3" />
																																		</td>
																																		<td width="122">
																																				<p>
																																						<a class="smallplainlink" href="javascript:document.email.submit();">
																																								<b>
																																										<font color="#5c81a7" size="1">将此页作为电子邮件发送</font>
																																								</b>
																																						</a>
																																				</p>
																																		</td>
																																</tr>
																																<noscript>
																																		<tr valign="top">
																																				<td width="8">
																																						<img height="1" alt="" src="//www.ibm.com/i/c.gif" width="8" />
																																				</td>
																																				<td width="16">
																																						<img height="16" alt="" src="//www.ibm.com/i/c.gif" width="16" />
																																				</td>
																																				<td class="small" width="122">
																																						<p>
																																								<span class="ast">未显示需要 JavaScript 的文档选项</span>
																																						</p>
																																				</td>
																																		</tr>
																																</noscript>
																														</tbody>
																												</form>
																										</table>
																								</td>
																						</tr>
																				</tbody>
																		</table>
																</td>
														</tr>
												</tbody>
										</table>
										<!--START RESERVED FOR FUTURE USE INCLUDE FILES-->
										<!-- 03/20/06 updated by gretchen -->
										<br />
										<table cellspacing="0" cellpadding="0" width="150" border="0">
												<tbody>
														<tr>
																<td class="v14-header-2-small">拓展 Tomcat 应用</td>
														</tr>
												</tbody>
										</table>
										<table class="v14-gray-table-border" cellspacing="0" cellpadding="0" border="0">
												<tbody>
														<tr>
																<td class="no-padding" width="150">
																		<table cellspacing="0" cellpadding="0" width="143" border="0">
																				<tbody>
																						<tr valign="top">
																								<td width="8">
																										<img height="1" alt="" src="http://www.ibm.com/i/c.gif" width="8" />
																								</td>
																								<td>
																										<img height="16" alt="" src="http://www.ibm.com/i/v14/icons/fw_bold.gif" width="16" vspace="3" border="0" />
																								</td>
																								<td width="125">
																										<p>
																												<a class="smallplainlink" href="http://www.ibm.com/developerworks/cn/kickstart/webserver.html?S_TACT=105AGX52&amp;S_CMP=simpleart">
																														<font color="#5c81a7" size="1">下载 IBM 开源 J2EE 应用服务器 WAS CE 新版本 V1.1</font>
																												</a>
																										</p>
																								</td>
																						</tr>
																				</tbody>
																		</table>
																</td>
														</tr>
												</tbody>
										</table>
										<!--END RESERVED FOR FUTURE USE INCLUDE FILES-->
										<br />
								</td>
						</tr>
				</tbody>
		</table>
		<p>级别: 初级</p>
		<p>
				<a href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#author">
						<font color="#996699">Uche Ogbuji</font>
				</a>(<a href="mailto:uche@ogbuji.net?subject=用 Python WSGI 混和并匹配 Web 组件&amp;cc=htc@us.ibm.com"><font color="#5c81a7">uche@ogbuji.net</font></a>), 首席顾问, Fourthougth, Inc.<br /></p>
		<p>2006 年 8 月 22 日</p>
		<blockquote>学习使用 Python 在 Web 服务器中创建并重用组件。Python 社区创建了 Web 服务器网关接口（Web Server Gateway Interface，WSGI），这是创建跨服务器和框架工作的 Python Web 组件的标准。它提供了一种利用许多不同的 Web 工具开发 Web 应用程序的方法。本文介绍了 WSGI 并展示如何开发出能够集成到设计出色的 Web 应用程序中的组件。</blockquote>
		<!--START RESERVED FOR FUTURE USE INCLUDE FILES-->
		<!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters -->
		<!--END RESERVED FOR FUTURE USE INCLUDE FILES-->
		<p>Web 成功的主要原因是它的灵活性。您会发现在设计、开发和部署 Web 站点和应用程序方面，每个开发人员几乎都有自己的特色。尽管有大量的选择，但是一个 Web 开发人员经常选择惟一的 Web 设计工具、页面风格、内容语言、Web 服务器、中间件和 DBMS 技术的组合，使用不同的实现语言和辅助工具包。为了给所有这些元素一起工作提供最大的灵活性，应该尽可能地通过组件提供 Web 的功能。这些组件应该执行有限数量的任务，并相互很好地配合工作。这么说很容易，但是在实践中完成这一要求是非常困难的，因为有许多实现 Web 技术的不同方法。</p>
		<p>因此开发人员期望着 Web 组件协同工作标准的成熟。这些重要标准的一部分已经开发完成，而且最成功的 Web 开发平台已经成为它们的支柱。突出的例子包括 Java servlet API 和 Ruby on Rails 框架。一些长期流行的 Web 编程语言直到最近才提供了相同的组件化水平，而且借鉴了先前 Web 框架组件标准的经验。一个例子是 Zend Framework for PHP（参见 <a href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#Resources"><font color="#996699">参考资料</font></a>）。另一个例子是 Python 的 Web 服务器网关接口（WSGI）。</p>
		<p>许多人抱怨流行的 Python 编程语言有太多的 Web 框架，从众所周知的 entrant（比如 Zope）到 under-the-radar 框架（比如 SkunkWeb）。有些人认为只要有一些基础标准化了，这种差异可能是件好事。Python 和 Web 专家 Phillip J. Eby 从事这样的标准化工作。他撰写的 Python Enhancement Proposal（PEP）333 定义了 WSGI。</p>
		<p>WSGI 的目标是支持 Python 框架之间更大的协同工作能力。WSGI 的成功会产生一个插件组件生态系统，在这个系统中可以用您喜欢的框架获得最大的灵活性。在本文中，我将介绍 WSGI，主要关注它作为可重用 Web 组件架构的使用方法。在所有讨论和示例代码中，假设您正在使用 Python 2.4 或者更新的版本。</p>
		<p>
				<a name="N1006C">
						<span class="atitle">WSGI 的基本架构</span>
				</a>
		</p>
		<p>WSGI 是在相当严格的约束条件下开发的，但是最重要的是需要对先于它的 Web 框架的向下兼容性。很遗憾，这个约束意味着 WSGI 不能像 Python 开发人员习惯的那么简洁和透明。通常，必须直接处理 WSGI 的开发人员只有那些需要构建框架和可重用组件的开发人员。大多数普通的 Web 开发人员会挑选容易使用的框架，而不接触 WSGI 的细节。</p>
		<p>如果想开发可重用 Web 组件，就必须理解 WSGI。需要理解的第一件事是以 WSGI 的角度来看 Web 应用程序是如何构造的。<a href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#fig1"><font color="#996699">图 1</font></a> 说明了这个结构。</p>
		<br />
		<a name="fig1">
				<b>图 1. HTTP 请求 — 响应如何通过 WSGI 堆栈 </b>
		</a>
		<br />
		<img height="566" alt="WSGI 堆栈" src="http://www.ibm.com/developerworks/cn/web/wa-wsgi/figure1.gif" width="332" />
		<br />
		<p>Web 服务器（也称为网关）是与请求客户机（通常是用户的浏览器）进行基本通信的非常低级的代码。应用层处理较高级别的细节，解释来自用户的请求并准备响应的内容。WSGI 本身的应用程序接口通常只是应用程序框架的更高层的最基本层，为通用的 Web 模式（比如 Ajax 技术或者内容模板系统）提供友好的设施。在服务器或者网关层之上是 WSGI 中间件。这个重要的层包含在服务器和应用程序实现中可以共享的组件。通用 Web 特性（比如用户会话、错误处理和身份验证）可以作为 WSGI 中间件来实现。</p>
		<p>
				<a name="N1008F">
						<span class="atitle">中间的代码</span>
				</a>
		</p>
		<p>WSGI 中间件对于可重用组件是最自然的层。WSGI 中间件看上去对于应用程序是较低层，而对于服务器是较高层。为了添加一些特殊的特性，它监视请求、响应和 WSGI 环境的状态。遗憾的是，WSGI 规范提供了非常差的中间件例子，而且可以找到的大多数其他例子也很简单，只能提供如何快速编写中间件的感性认识。我将使用以下概述给出对 WSGI 中间件承担的过程的感性认识。这里忽略了大多数 WSGI 中间件作者都不必担心的问题。在 Python 中，我使用单词<i>函数</i>，意思是任何可调用的对象。</p>
		<ul>
				<li>
						<b>设置阶段</b>。每当 Web 服务器启动时，设置阶段就发生了。它接受一个中间件实例，其中包括应用程序函数。 
</li>
				<li>
						<b>处理客户机请求</b>。每当 Web 服务器收到请求时，就会处理客户机请求。 
<ol><li>服务器用这个环境和 <code>server.start_response</code> 参数调用中间件函数。 
</li><li>中间件处理这个环境并调用可调用的应用程序，传递这个环境和包装的函数 <code>middleware.start_response</code>。 
</li><li>应用程序执行；首先它准备响应报头，然后调用 <code>middleware.start_response</code>。 
</li><li>中间件处理响应报头并调用 <code>server.start_response</code>。 
</li><li>服务器将控制传递回中间件，然后回到开始产生响应体块（比如字符串）的应用程序。 
</li><li>对于每一个响应，体块中间件做出任何修改并向服务器传递一些相应的字符串。 
</li><li>一旦处理完应用程序的所有块，中间件就向服务器返回控制，完成当前的请求。 </li></ol></li>
		</ul>
		<br />
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr>
								<td>
										<img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" />
										<br />
										<img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table class="no-print" cellspacing="0" cellpadding="0" align="right">
				<tbody>
						<tr align="right">
								<td>
										<img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" />
										<br />
										<table cellspacing="0" cellpadding="0" border="0">
												<tbody>
														<tr>
																<td valign="center">
																		<img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" />
																		<br />
																</td>
																<td valign="top" align="right">
																		<a class="fbox" href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#main">
																				<b>
																						<font color="#996699">回页首</font>
																				</b>
																		</a>
																</td>
														</tr>
												</tbody>
										</table>
								</td>
						</tr>
				</tbody>
		</table>
		<br />
		<br />
		<p>
				<a name="N100D0">
						<span class="atitle">处理 XHTML 的大胆步骤</span>
				</a>
		</p>
		<p>许多组件技术相当复杂，所以作为指导的例子最好是简单的东西。这不是使用 WSGI 的案例，事实上，我将要介绍一个非常实际的例子。许多开发者更喜欢使用 XHTML Web 页面，因为 XML 技术比 “标记汤” 的 HTML 更容易管理，而且 Web 站点倾向于更容易由机器读取。问题是并非所有的 Web 浏览器都能正确地支持 XHTML。<a href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#listing1"><font color="#996699">清单 1</font></a>（safexhtml.py）是一个 WSGI 中间件模块，它检查到来的请求，看浏览器是否支持 XHTML；如果不支持，就将任何 XHTML 响应翻译成普通的 HTML。可以使用这样的模块让所有主要应用程序代码产生 XHTML，而中间件负责任何需要翻译成 HTML 的地方。仔细查看 <a href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#listing1"><font color="#996699">清单 1</font></a> 并试着把它与前面描述的 WSGI 中间件过程相结合。我提供了足够的注释，您可以识别出代码的不同阶段。</p>
		<br />
		<a name="listing1">
				<b>清单 1（safexhtml.py）. WSGI 中间件为不能处理 XHTML 的浏览器把 XHTML 翻译成 HTML</b>
		</a>
		<br />
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr>
								<td class="code-outline">
										<pre class="displaycode">import cStringIO
from xml import sax
from Ft.Xml import CreateInputSource
from Ft.Xml.Sax import SaxPrinter
from Ft.Xml.Lib.HtmlPrinter import HtmlPrinter

XHTML_IMT = "application/xhtml+xml"
HTML_CONTENT_TYPE = 'text/html; charset=UTF-8'

class safexhtml(object):
    """
    Middleware that checks for XHTML capability in the client and translates
    XHTML to HTML if the client can't handle it
    """
    def __init__(self, app):
        #Set-up phase
        self.wrapped_app = app
        return

    def __call__(self, environ, start_response):
        #Handling a client request phase.
        #Called for each client request routed through this middleware

        #Does the client specifically say it supports XHTML?
        #Note saying it accepts */* or application/* will not be enough
        xhtml_ok = XHTML_IMT in environ.get('HTTP_ACCEPT', '')

        #Specialized start_response function for this middleware
        def start_response_wrapper(status, response_headers, exc_info=None):
            #Assume response is not XHTML; do not activate transformation
            environ['safexhtml.active'] = False
            #Check for response content type to see whether it is XHTML
            #That needs to be transformed
            for name, value in response_headers:
                #content-type value is a media type, defined as
                #media-type = type "/" subtype *( ";" parameter )
                if ( name.lower() == 'content-type'
                     and value.split(';')[0] == XHTML_IMT ):
                    #Strip content-length if present (needs to be
                    #recalculated by server)
                    #Also strip content-type, which will be replaced below
                    response_headers = [ (name, value)
                        for name, value in response_headers
                            if ( name.lower()
                                 not in ['content-length', 'content-type'])
                    ]
                    #Put in the updated content type
                    response_headers.append(('content-type', HTML_CONTENT_TYPE))
                    #Response is XHTML, so activate transformation
                    environ['safexhtml.active'] = True
                    break

            #We ignore the return value from start_response
            start_response(status, response_headers, exc_info)
            #Replace any write() callable with a dummy that gives an error
            #The idea is to refuse support for apps that use write()
            def dummy_write(data):
                raise RuntimeError('safexhtml does not support the deprecated 
                                       write() callable in WSGI clients')
            return dummy_write

        if xhtml_ok:
            #The client can handle XHTML, so nothing for this middleware to do
            #Notice that the original start_response function is passed
            #On, not this middleware's start_response_wrapper
            for data in self.wrapped_app(environ, start_response):
                yield data
        else:
            response_blocks = []  #Gather output strings for concatenation
            for data in self.wrapped_app(environ, start_response_wrapper):
                if environ['safexhtml.active']:
                    response_blocks.append(data)
                    yield '' #Obey buffering rules for WSGI
                else:
                    yield data

            if environ['safexhtml.active']:
                #Need to convert response from XHTML to HTML 
                xhtmlstr = ''.join(response_blocks) #First concatenate response

                #Now use 4Suite to transform XHTML to HTML
                htmlstr = cStringIO.StringIO()  #Will hold the HTML result
                parser = sax.make_parser(['Ft.Xml.Sax'])
                handler = SaxPrinter(HtmlPrinter(htmlstr, 'UTF-8'))
                parser.setContentHandler(handler)
                #Don't load the XHTML DTDs from the Internet
                parser.setFeature(sax.handler.feature_external_pes, False)
                parser.parse(CreateInputSource(xhtmlstr))
                yield htmlstr.getvalue()
                return



</pre>
								</td>
						</tr>
				</tbody>
		</table>
		<br />
		<p>
				<code>safexhtml</code> 类是完整的中间件实现。每个实例都是一个可调用对象，因为这个类定义了专门的 <code>__call__</code> 方法。向服务器传递类的实例，传递包装成初始化器 <code>__init__</code> 的应用程序。如果把 <code>safexhtml</code> 链到另一个中间件，包装的应用程序可能也是另一个中间件实例。当中间件作为向服务器请求的结果被调用时，这个类首先检查客户机发送的 Accept 报头，看它是否包括正式的 XHTML 媒体类型。如果是这样的话（<code>xhtml_ok</code> 标志），那么发送 XHTML 就是安全的，中间件就不在请求中做任何有意义的事情。</p>
		<p>当客户机不能处理 XHTML 的时候，这个类定义专门的嵌套函数 <code>start_response_wrapper</code>，这个函数的作用是检查来自应用程序的响应报头，看响应是否是 XHTML。如果是，这个响应需要翻译成普通的 HTML，在这个环境中以 <code>safexhtml.active</code> 标记这个事实。对于这个标志，使用这个环境的一个原因是因为它处理将标志返回到其余的中间件代码的范围问题。记住，在应用程序选择的时间异步调用 <code>start_response_wrapper</code>，它可以技巧地管理必要的中间件状态。</p>
		<p>使用这个环境的另一个原因是向下沿着 WSGI 堆栈通信时，内容已经修改。如果需要翻译响应体，不仅要运行 <code>start_response_wrapper</code> 设置 <code>safexhtml.active</code>，而且它还把响应媒体类型改成 <code>text/html</code> 并删除任何 <code>Content-Length</code> 报头，因为翻译会改变响应体的长度，它必须向下游重新计算，这可能由服务器执行。 </p>
		<p>一旦应用程序开始发送响应体，如果需要翻译，它就把数据收集到 <code>response_blocks</code> 列表中。应用程序可能以块的形式发送响应，但是为了保持代码简单，它仅针对完整的 XHTML 输入运行翻译机制。可是，WSGI 规则规定应用程序每次产生块的时候，中间件必须向服务器传送一些东西。传递空字符串也是可以的，这就是它应该做的。一旦应用程序完成，它就固定响应体并通过翻译代码处理响应，然后用最后一个字符串产生完整的输出结果。 </p>
		<p>
				<a href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#listing2">
						<font color="#996699">清单 2</font>
				</a>（wsgireftest.py）是测试中间件的服务器代码。它使用包含非常简单的 WSGI 服务器的 wsgiref。这个模块将包括在 Python 2.5 标准库中。 </p>
		<br />
		<a name="listing2">
				<b>清单 2（wsgireftest.py）. 测试清单 1 的服务器代码</b>
		</a>
		<br />
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr>
								<td class="code-outline">
										<pre class="displaycode">import sys
from wsgiref.simple_server import make_server
from safexhtml import safexhtml

XHTML = open('test.xhtml').read()
XHTML_IMT = "application/xhtml+xml"
HTML_IMT = "text/html"
PORT = 8000


def app(environ, start_response):
    print "using IMT", app.current_imt
    start_response('200 OK', [('Content-Type', app.current_imt)])
    #Swap the IMT used for response (alternate between XHTML_IMT and HTML_IMT)
    app.current_imt, app.other_imt = app.other_imt, app.current_imt
    return [XHTML]

app.current_imt=XHTML_IMT
app.other_imt=HTML_IMT

httpd = make_server('', PORT, safexhtml(app))
print 'Starting up HTTP server on port %i...'%PORT

# Respond to requests until process is killed
httpd.serve_forever()


</pre>
								</td>
						</tr>
				</tbody>
		</table>
		<br />
		<p>
				<a href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#listing2">
						<font color="#996699">清单 2</font>
				</a>读取 <a href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#listing3"><font color="#996699">清单 3</font></a>（test.xhtml）给出的简单 XHTML 文件，并向它提供替代的媒体类型。对于第一次请求使用标准的 XHTML 媒体类型，对于第二次使用 HTML 媒体类型，第三次又使用 XHTML，如此循环。如果响应没有标成 XHTML，就保持响应不变，这个中间件功能留给读者作为练习。 </p>
		<br />
		<a name="listing3">
				<b>清单 3（test.xhtml）. 由清单 2 中的示例服务器使用的简单 XHTML 文件</b>
		</a>
		<br />
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr>
								<td class="code-outline">
										<pre class="displaycode">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" &gt;
  &lt;head&gt;
    &lt;title&gt;Virtual Library&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;p&gt;Moved to &lt;a href="http://vlib.org/"&gt;vlib.org&lt;/a&gt;.&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;


</pre>
								</td>
						</tr>
				</tbody>
		</table>
		<br />
		<p>如果运行 <a href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#listing2"><font color="#996699">清单 2</font></a> 并在理解 XHTML 的浏览器（比如 Firefox）以及不理解 XHTML 的浏览器（比如 Microsoft Internet Explorer）中观看，您应该能够看到这个中间件的效果。为了在每个浏览器中看到中间件操作对响应媒体类型的影响，要在一行中请求两次。使用 View Source 查看产生的响应体，使用 Page Info 特性查看报告的响应媒体类型。也可以使用命令行 HTTP 工具 cURL：用 <code>curl -H 'Accept: application/xhtml+xml,text/html' http://localhost:8000/</code> 模拟理解 XHTML 的浏览器，用 <code>curl -H 'Accept: text/html' http://localhost:8000/</code> 模拟不理解 XHTML 的浏览器，从而测试这个例子。如果想要看响应报头，在每一次调用 cURL 之后使用 <code>-D &lt;filename&gt;</code> 并检查给定的文件名。</p>
		<br />
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr>
								<td>
										<img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" />
										<br />
										<img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table class="no-print" cellspacing="0" cellpadding="0" align="right">
				<tbody>
						<tr align="right">
								<td>
										<img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" />
										<br />
										<table cellspacing="0" cellpadding="0" border="0">
												<tbody>
														<tr>
																<td valign="center">
																		<img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" />
																		<br />
																</td>
																<td valign="top" align="right">
																		<a class="fbox" href="http://www.ibm.com/developerworks/cn/web/wa-wsgi/index.html#main">
																				<b>
																						<font color="#996699">回页首</font>
																				</b>
																		</a>
																</td>
														</tr>
												</tbody>
										</table>
								</td>
						</tr>
				</tbody>
		</table>
		<br />
		<br />
		<p>
				<a name="N10161">
						<span class="atitle">结束语</span>
				</a>
		</p>
		<p>现在您学习了 Python 的 WSGI 以及如何使用它实现可以插入任何 WSGI 服务器和应用程序链的中间件服务。很容易将本文的中间件例子链到进行缓存或者调试的中间件。所有这些都成为组件，因此可以快速地向项目中添加经过反复测试的特性，而不必考虑选择的 WSGI 实现是什么。</p>
		<p>WSGI 是一个相当新的规范，但是兼容的服务器、中间件和工具很快出现，使得 Python Web 框架的前景非常光明。下一次您在 Python 中开发重大的 Web 项目时，一定要通过使用现有的 WSGI 组件来采用 WSGI，还可以考虑为自己或者为您的 Web 开发伙伴创建 WSGI 组件。</p>
<img src ="http://www.cnitblog.com/stevenluan/aggbug/23691.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/stevenluan/" target="_blank">Steven.L</a> 2007-03-07 09:39 <a href="http://www.cnitblog.com/stevenluan/archive/2007/03/07/23691.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>