﻿<?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>C++博客-无法递归的五点半-随笔分类-Python</title><link>http://www.cppblog.com/lambdacpp/category/6801.html</link><description>for_each(day.begin(),day.end(),bind2nd(Add(),me))</description><language>zh-cn</language><lastBuildDate>Sat, 21 Mar 2009 22:41:31 GMT</lastBuildDate><pubDate>Sat, 21 Mar 2009 22:41:31 GMT</pubDate><ttl>60</ttl><item><title>Hatta Wiki试用</title><link>http://www.cppblog.com/lambdacpp/archive/2009/03/20/77329.html</link><dc:creator>五点半</dc:creator><author>五点半</author><pubDate>Fri, 20 Mar 2009 08:55:00 GMT</pubDate><guid>http://www.cppblog.com/lambdacpp/archive/2009/03/20/77329.html</guid><wfw:comment>http://www.cppblog.com/lambdacpp/comments/77329.html</wfw:comment><comments>http://www.cppblog.com/lambdacpp/archive/2009/03/20/77329.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lambdacpp/comments/commentRss/77329.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lambdacpp/services/trackbacks/77329.html</trackback:ping><description><![CDATA[<a title="冯大辉"  href="http://www.dbanotes.net/">大辉</a>推荐了一个网站www.wikimatrix.org。去看了看虽然一直用Wiki进行一些文档的协作，但那么多Wiki还是第一次哈。于是想找一个用Python开发的wiki(wikimatrix上面有一个Wizard可以帮助您找到想要的)。除了老牌的plone,著名的MoinMoin,还发现了一个叫<a title="hatta"  href="http://hatta.sheep.art.pl/">hatta</a><a></a>的。安装了一下，真还不错。<br>hatta的实现就一个python文件，功能简单，没有权限管理，没有用户管理，作者认为Wiki就是要欢迎天南地北的兄弟都可以修改。hatta的使用简单，按照语法编辑，然后提交了事。中文化也非常简单，Wiki内容本身就可以输入中文，如果觉得页面也应该是中文的也很简单，改改po文件即可。我觉得如果您对Wiki的需求如下就可以考虑使用：<br>
<ol>
    <li>内部使用。如果部署在外网，好事者乱改一气还是比较麻烦的，特别是放一些不该放的。</li>
    <li>需要版本控制的文档协作</li>
    <li>要在文档中嵌入程序代码，代码需要高亮</li>
    <li>需要在文档中嵌入数学公式。hatta使用latex语法输入公式，生成的公式非常漂亮</li>
</ol>
<br><br><br><br><img src ="http://www.cppblog.com/lambdacpp/aggbug/77329.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lambdacpp/" target="_blank">五点半</a> 2009-03-20 16:55 <a href="http://www.cppblog.com/lambdacpp/archive/2009/03/20/77329.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Quixote笔记</title><link>http://www.cppblog.com/lambdacpp/archive/2008/04/19/47594.html</link><dc:creator>五点半</dc:creator><author>五点半</author><pubDate>Sat, 19 Apr 2008 07:11:00 GMT</pubDate><guid>http://www.cppblog.com/lambdacpp/archive/2008/04/19/47594.html</guid><wfw:comment>http://www.cppblog.com/lambdacpp/comments/47594.html</wfw:comment><comments>http://www.cppblog.com/lambdacpp/archive/2008/04/19/47594.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/lambdacpp/comments/commentRss/47594.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lambdacpp/services/trackbacks/47594.html</trackback:ping><description><![CDATA[<div style="text-align: right;"><span style="font-style: italic;">又是Python。呵呵。谁叫人家帅呢</span><br></div>
<h2>Quixote的部署</h2>
&nbsp;&nbsp;&nbsp; 在Quixote官方白皮书中已经详细描述了Quixote的工作模式。Quixote可以使用Python自代的http_server(主要用于开发调试)和与Apache(或lighttpd)配合使用。<br>&nbsp;&nbsp;&nbsp; Quixote与Apache配合使用方式如下：<br>
<ol>
    <li>使用CGI，文档中称为egular CGI。被认为效率最低的一种方式，因为每一个请求都会创建一个新的进程。</li>
    <li>使用fastCGI，CGI可以运行fastCGI一定是可以应用的。这也是豆瓣采用的方式。在Quixote作者的一个PPT中，他认为fastCGI是buggy的。哦:(也不至于啊。我们正在寻找使用fastCGI的部署经验。</li>
    <li>使用mod_python，将python代码嵌入到Apache中。</li>
    <li>使用SCGI，这是作者推荐的。使用Apache SCGI module scgi_mod将遵循SCGI协议Apache将请求发送到相应的Socket如localhost:3001。而这个Socket由本地运行的一个 Python程序打开。这个Python程序将处理请求，并返回结果。 </li>
</ol>
<h2>SCGI的配置</h2>
&nbsp;&nbsp;&nbsp; Quixote的网站上对SCGI的描述：SCGI协议是CGI协议的替代。它是一种应用程序与HTTP服务接口标准。它有些像FastCGI但他的设计更容易实现。<br><br>&nbsp;&nbsp;&nbsp; 配置SCGI过程如下：<br>
<ul>
    <li>安装各个模块不在话下，debian让程序员有了懒惰的美德：</li>
</ul>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #aptitude&nbsp;install&nbsp;libapache2-mod-scgi&nbsp;python-scgi&nbsp;python-quixote<br><br></span></div>
<ul>
    <li>Apache的配置，添加配置到apache.conf。（有些教程中加入了SetHandler scgi-handler ，但这个加上就很本不会和3000通信。可能是版本的问题。最好不用。） </li>
</ul>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #800000;">Location&nbsp;</span><span style="color: #ff0000;">"/qx"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; SCGIServer&nbsp;localost:3000<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; SCGIHandler&nbsp;On<br></span><span style="color: #0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color: #800000;">Location</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">&nbsp;<br><br></span></div>
<ul>
    <li>重启Apache </li>
</ul>
&nbsp;&nbsp;&nbsp; 配置完成。SCGI的好处在于，修改了Python程序，不用重启Apache，只要重启SCGI就可以了。<br><br>
<h2>第一个Quixote程序</h2>
&nbsp;&nbsp;&nbsp; 一切就绪，我们来一次Quixote的完整之旅。<br><br>
<ul>
    <li>scgi程序要求有一个服务打开3000端口。启动scgi的程序如下: </li>
</ul>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">#</span><span style="color: #008000;">!/usr/bin/python</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;-*-&nbsp;coding:&nbsp;utf-8&nbsp;-*-</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;scgi.quixote_handler&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;QuixoteHandler,&nbsp;main<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;quixote&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;enable_ptl<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;quixote.publish&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;Publisher<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">enable_ptl()&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">启动PTL</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;create_publisher():<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;ourroot&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;RootDirectory<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;Publisher(RootDirectory(),&nbsp;display_exceptions</span><span style="color: #000000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">plain</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">__name__</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">__main__</span><span style="color: #800000;">'</span><span style="color: #000000;">:<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;quixote.server&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;scgi_server<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;scgi_server.run(create_publisher,&nbsp;port</span><span style="color: #000000;">=</span><span style="color: #000000;">3000</span><span style="color: #000000;">,&nbsp;script_name</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">/qx</span><span style="color: #800000;">"</span><span style="color: #000000;">)<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;"></span></div>
<br>
<ul>
    <li>程序结构是比较简单的，使用的是scgi_server的run方法。要注意的是run方法中的script_name和前面apache 的配置Location是一样的。程序的关键是导入了ourroot这样一个ptl 。下面是我们的第一个ptl程序。</li>
</ul>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;-*-&nbsp;coding:&nbsp;utf-8&nbsp;-*-</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #008000;"></span><span style="color: #800000;">"""</span><span style="color: #800000;">这个是我们第一个例子的根目录<br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #800000;"></span><span style="color: #800000;">"""</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;quixote.directory&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;Directory<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;RootDirectory(Directory):<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;_q_exports&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[</span><span style="color: #800000;">""</span><span style="color: #000000;">]<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;_q_index&nbsp;[html]&nbsp;(self):<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">debug&nbsp;message&nbsp;from&nbsp;the&nbsp;index&nbsp;page</span><span style="color: #800000;">"</span><span style="color: #000000;"><br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">"""</span><span style="color: #800000;"><br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;html&gt;<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;head&gt;<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;http-equiv="Content-Type"&nbsp;content="text/html&nbsp;charset=UTF-8"&nbsp;/&gt;<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;第一个例子&lt;/title&gt;<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/head&gt;<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h1&gt;第一个例子有中文!&lt;/h1&gt;<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/body&gt;<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/html&gt;<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #800000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">"""</span><span style="color: #000000;"><br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;"></span></div>
<br>
<ul>
    <li>现在在浏览器中输入http://localhost/qx就可以看到结果了。</li>
    <li>除了运行上面的python脚本，也可以采用这样的方式运行scgi:</li>
    <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">python&nbsp;/var/lib/python-support/python2</span><span style="color: #000000;">.5</span><span style="color: #000000;">/quixote/server/scgi_server.py&nbsp;\<br>&nbsp;&nbsp;&nbsp;&nbsp;--factory</span><span style="color: #000000;">=</span><span style="color: #000000;">FirstApp.create_publisher&nbsp;\<br>&nbsp;&nbsp;&nbsp;&nbsp;--script-name</span><span style="color: #000000;">=</span><span style="color: #000000;">/qx&nbsp;&nbsp;--port</span><span style="color: #000000;">=</span><span style="color: #000000;">3000</span><span style="color: #000000;"><br></span></div>
    <br><br>
</ul>
<h2>Quixote 中文化的要点</h2>
&nbsp;&nbsp;&nbsp; Quixote的中文设置好像很麻烦。其实随着python、Quixote版本的推进，现在这个问题已经很简单了。字符集使用的是utf-8。使用gb2312可能也是可以的。<br><br>
<ol>
    <li>所有源代码使用utf-8在程序的开始加上# -*- coding: utf-8 -*-</li>
    <li>ptl的html模板加上content="text/html charset=UTF-8"</li>
    <li>关键:在quixote的安装路径下有__init__.py，将其中的DEFAULT_CHARSET = 'iso-8859-1'改成 'utf-8'</li>
    <li>也可以不修改__init__.py，使用Publisher的时候把Publisher扩展一下：</li>
</ol>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;UTF8Publisher(Publisher):<br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;quixote.DEFAULT_CHARSET&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">utf-8</span><span style="color: #800000;">"</span><span style="color: #000000;"><br></span><span style="color: #008080;"></span><span style="color: #000000;"></span></div>
<br>   <img src ="http://www.cppblog.com/lambdacpp/aggbug/47594.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lambdacpp/" target="_blank">五点半</a> 2008-04-19 15:11 <a href="http://www.cppblog.com/lambdacpp/archive/2008/04/19/47594.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>