<?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>孙立文的博客</title>
	<atom:link href="http://sunliwen.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sunliwen.com</link>
	<description>Never lose my passion!</description>
	<lastBuildDate>Tue, 15 May 2012 03:24:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Ubuntu下用Vim编辑且不改变GBK编码的文件</title>
		<link>http://sunliwen.com/2012/03/ubuntu-vim-gbk/</link>
		<comments>http://sunliwen.com/2012/03/ubuntu-vim-gbk/#comments</comments>
		<pubDate>Wed, 14 Mar 2012 10:53:18 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[VIM]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=2016</guid>
		<description><![CDATA[Ubuntu下用Vim编辑且不改变GBK编码的文件 sudo vi /var/lib/locales/supported.d/zh 添加以下内容： zh_CN.GB18030 GB18030 zh_CN.GBK GBk zh_CN.GB2312 GB2312 执行 sudo locale-gen vi ~/.vimrc 添加以下内容 set encoding=utf-8 set fileencodings=cp936,gb18030,ucs-bom,utf-8,big5,euc-jp,euc-kr,latin1 set termencoding=utf-8 language zh_CN.GB18030 以上配置可以实现打开编辑GBK编码的文件，但保存时并不改变编码。 Tags: Linux, VIM 没有评论]]></description>
			<content:encoded><![CDATA[<p>Ubuntu下用Vim编辑且不改变GBK编码的文件</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>locales<span style="color: #000000; font-weight: bold;">/</span>supported.d<span style="color: #000000; font-weight: bold;">/</span>zh</pre></div></div>

<p>添加以下内容：</p>
<blockquote><p>
zh_CN.GB18030 GB18030<br />
zh_CN.GBK GBk<br />
zh_CN.GB2312 GB2312
</p></blockquote>
<p>执行</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> locale-gen</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">vi</span> ~<span style="color: #000000; font-weight: bold;">/</span>.vimrc</pre></div></div>

<p>添加以下内容</p>
<blockquote><p>
set encoding=utf-8<br />
set fileencodings=cp936,gb18030,ucs-bom,utf-8,big5,euc-jp,euc-kr,latin1<br />
set termencoding=utf-8<br />
language zh_CN.GB18030
</p></blockquote>
<p>以上配置可以实现打开编辑GBK编码的文件，但保存时并不改变编码。</p>
<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/linux/" rel="tag">Linux</a>, <a href="http://sunliwen.com/tag/vim/" rel="tag">VIM</a><br/>
<a href="http://sunliwen.com/2012/03/ubuntu-vim-gbk/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2012/03/ubuntu-vim-gbk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>笔记：Ubuntu下快速开始使用Python Thrift</title>
		<link>http://sunliwen.com/2012/02/apache-thrift-on-ubuntu-10-04/</link>
		<comments>http://sunliwen.com/2012/02/apache-thrift-on-ubuntu-10-04/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 12:56:28 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache Thrift]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1967</guid>
		<description><![CDATA[本文介绍如何在Ubuntu 10.04下安装Apache Thrift并用Python写一个Demo。 apt-get install libboost-dev libevent-dev python-dev automake pkg-config libtool flex bison sun-java6-jdk wget http://www.apache.org/dist//thrift/0.8.0/thrift-0.8.0.tar.gz tar zxvf thrift-0.8.0.tar.gz cd thrift-0.8.0 ./configure make sudo make install sudo pip install thrift 编辑接口文件 hellowworld.thrift: service HelloWorld { string ping(), string say(1:string msg) } 编辑 server.py #!/usr/bin/env python &#160; import socket import sys sys.path.append&#40;'./gen-py'&#41; &#160; from helloworld import [...]]]></description>
			<content:encoded><![CDATA[<p>本文介绍如何在Ubuntu 10.04下安装Apache Thrift并用Python写一个Demo。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libboost-dev libevent-dev python-dev <span style="color: #c20cb9; font-weight: bold;">automake</span> pkg-config libtool <span style="color: #c20cb9; font-weight: bold;">flex</span> <span style="color: #c20cb9; font-weight: bold;">bison</span> sun-java6-jdk
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.apache.org<span style="color: #000000; font-weight: bold;">/</span>dist<span style="color: #000000; font-weight: bold;">//</span>thrift<span style="color: #000000; font-weight: bold;">/</span>0.8.0<span style="color: #000000; font-weight: bold;">/</span>thrift-0.8.0.tar.gz
<span style="color: #c20cb9; font-weight: bold;">tar</span> zxvf thrift-0.8.0.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> thrift-0.8.0
.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> pip <span style="color: #c20cb9; font-weight: bold;">install</span> thrift</pre></div></div>

<p>编辑接口文件 hellowworld.thrift:</p>

<div class="wp_syntax"><div class="code"><pre class="thrift" style="font-family:monospace;">service HelloWorld {
    string ping(),
    string say(1:string msg)
}</pre></div></div>

<p>编辑 server.py</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'./gen-py'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> helloworld <span style="color: #ff7700;font-weight:bold;">import</span> HelloWorld
<span style="color: #ff7700;font-weight:bold;">from</span> helloworld.<span style="color: black;">ttypes</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> thrift.<span style="color: black;">transport</span> <span style="color: #ff7700;font-weight:bold;">import</span> TSocket
<span style="color: #ff7700;font-weight:bold;">from</span> thrift.<span style="color: black;">transport</span> <span style="color: #ff7700;font-weight:bold;">import</span> TTransport
<span style="color: #ff7700;font-weight:bold;">from</span> thrift.<span style="color: black;">protocol</span> <span style="color: #ff7700;font-weight:bold;">import</span> TBinaryProtocol
<span style="color: #ff7700;font-weight:bold;">from</span> thrift.<span style="color: black;">server</span> <span style="color: #ff7700;font-weight:bold;">import</span> TServer
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> HelloWorldHandler:
  <span style="color: #ff7700;font-weight:bold;">def</span> ping<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;pong&quot;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> say<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, msg<span style="color: black;">&#41;</span>:
    ret = <span style="color: #483d8b;">&quot;Received: &quot;</span> + msg
    <span style="color: #ff7700;font-weight:bold;">print</span> ret
    <span style="color: #ff7700;font-weight:bold;">return</span> ret
&nbsp;
handler = HelloWorldHandler<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
processor = HelloWorld.<span style="color: black;">Processor</span><span style="color: black;">&#40;</span>handler<span style="color: black;">&#41;</span>
transport = TSocket.<span style="color: black;">TServerSocket</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;localhost&quot;</span>, <span style="color: #ff4500;">9090</span><span style="color: black;">&#41;</span>
tfactory = TTransport.<span style="color: black;">TBufferedTransportFactory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
pfactory = TBinaryProtocol.<span style="color: black;">TBinaryProtocolFactory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
server = TServer.<span style="color: black;">TSimpleServer</span><span style="color: black;">&#40;</span>processor, transport, tfactory, pfactory<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Starting thrift server in python...&quot;</span>
server.<span style="color: black;">serve</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;done!&quot;</span></pre></div></div>

<p>编辑 client.py</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'./gen-py'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> helloworld <span style="color: #ff7700;font-weight:bold;">import</span> HelloWorld
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> thrift <span style="color: #ff7700;font-weight:bold;">import</span> Thrift
<span style="color: #ff7700;font-weight:bold;">from</span> thrift.<span style="color: black;">transport</span> <span style="color: #ff7700;font-weight:bold;">import</span> TSocket
<span style="color: #ff7700;font-weight:bold;">from</span> thrift.<span style="color: black;">transport</span> <span style="color: #ff7700;font-weight:bold;">import</span> TTransport
<span style="color: #ff7700;font-weight:bold;">from</span> thrift.<span style="color: black;">protocol</span> <span style="color: #ff7700;font-weight:bold;">import</span> TBinaryProtocol
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
  transport = TSocket.<span style="color: black;">TSocket</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'localhost'</span>, <span style="color: #ff4500;">9090</span><span style="color: black;">&#41;</span>
  transport = TTransport.<span style="color: black;">TBufferedTransport</span><span style="color: black;">&#40;</span>transport<span style="color: black;">&#41;</span>
  protocol = TBinaryProtocol.<span style="color: black;">TBinaryProtocol</span><span style="color: black;">&#40;</span>transport<span style="color: black;">&#41;</span>
  client = HelloWorld.<span style="color: black;">Client</span><span style="color: black;">&#40;</span>protocol<span style="color: black;">&#41;</span>
  transport.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;client - ping&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;server - &quot;</span> + client.<span style="color: black;">ping</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;client - say&quot;</span>
  msg = client.<span style="color: black;">say</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello!&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;server - &quot;</span> + msg
&nbsp;
  transport.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">except</span> Thrift.<span style="color: black;">TException</span>, ex:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>ex.<span style="color: black;">message</span><span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">thrift <span style="color: #660033;">--gen</span> py helloworld.thrift
python server.py
python client.py</pre></div></div>

<p>参考: <a href="http://diwakergupta.github.com/thrift-missing-guide/thrift.pdf">Thrift the missing guide</a></p>
<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/apache-thrift/" rel="tag">Apache Thrift</a>, <a href="http://sunliwen.com/tag/python/" rel="tag">Python</a><br/>
<a href="http://sunliwen.com/2012/02/apache-thrift-on-ubuntu-10-04/#comments-heading">2个评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2012/02/apache-thrift-on-ubuntu-10-04/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ab(Apache Benchmark)运行apr_socket_recv报错</title>
		<link>http://sunliwen.com/2012/01/mac-ab-apr-error/</link>
		<comments>http://sunliwen.com/2012/01/mac-ab-apr-error/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 18:34:57 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AB]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1908</guid>
		<description><![CDATA[在Mac OS X Lion下运行ab会报如下错误： apr_socket_recv: Connection reset by peer &#40;54&#41; 比如： $ ab -n 1000 -c 10 http://localhost:8888/ This is ApacheBench, Version 2.3 &#60;$Revision: 655654 $&#62; Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ &#160; Benchmarking localhost &#40;be patient&#41; Send request failed! Send request failed! Send request failed! [...]]]></description>
			<content:encoded><![CDATA[<p>在Mac OS X Lion下运行ab会报如下错误：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">apr_socket_recv: Connection reset by peer <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">54</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>比如：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ ab <span style="color: #660033;">-n</span> <span style="color: #000000;">1000</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">10</span> http:<span style="color: #000000; font-weight: bold;">//</span>localhost:<span style="color: #000000;">8888</span><span style="color: #000000; font-weight: bold;">/</span>
This is ApacheBench, Version <span style="color: #000000;">2.3</span> <span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #007800;">$Revision</span>: <span style="color: #000000;">655654</span> $<span style="color: #000000; font-weight: bold;">&gt;</span>
Copyright <span style="color: #000000;">1996</span> Adam Twiss, Zeus Technology Ltd, http:<span style="color: #000000; font-weight: bold;">//</span>www.zeustech.net<span style="color: #000000; font-weight: bold;">/</span>
Licensed to The Apache Software Foundation, http:<span style="color: #000000; font-weight: bold;">//</span>www.apache.org<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
Benchmarking localhost <span style="color: #7a0874; font-weight: bold;">&#40;</span>be patient<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Send request failed<span style="color: #000000; font-weight: bold;">!</span>
Send request failed<span style="color: #000000; font-weight: bold;">!</span>
Send request failed<span style="color: #000000; font-weight: bold;">!</span>
apr_socket_recv: Connection reset by peer <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">54</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>新版的Apache已经解决了这个问题，可以获取源代码并编译如下：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>mirrors.kahuki.com<span style="color: #000000; font-weight: bold;">/</span>apache<span style="color: #000000; font-weight: bold;">//</span>httpd<span style="color: #000000; font-weight: bold;">/</span>httpd-2.3.16-beta.tar.bz2
<span style="color: #c20cb9; font-weight: bold;">tar</span> jxvf httpd-2.3.16-beta.tar.bz2 
<span style="color: #7a0874; font-weight: bold;">cd</span> httpd-2.3.16
.<span style="color: #000000; font-weight: bold;">/</span>configure</pre></div></div>

<p>我只需要一个ab，所以只编译support目录即可。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> support
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #666666; font-style: italic;">#...</span></pre></div></div>

<p>重新运行ab</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>ab <span style="color: #660033;">-n</span> <span style="color: #000000;">1000</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">10</span> http:<span style="color: #000000; font-weight: bold;">//</span>localhost:<span style="color: #000000;">8888</span><span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Bingo! :)</p>
<p>对于老一些的版本的Apache，需要打补丁</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> https:<span style="color: #000000; font-weight: bold;">//</span>www.rtfm.ro<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>patches<span style="color: #000000; font-weight: bold;">/</span>ab.patch <span style="color: #660033;">--no-check-certificate</span>
<span style="color: #c20cb9; font-weight: bold;">patch</span> <span style="color: #660033;">-p0</span> <span style="color: #000000; font-weight: bold;">&lt;</span> .<span style="color: #000000; font-weight: bold;">/</span>ab.patch</pre></div></div>

<p>会有一行出错，不要理，继续往下走。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">patching <span style="color: #c20cb9; font-weight: bold;">file</span> support<span style="color: #000000; font-weight: bold;">/</span>ab.c
Hunk <span style="color: #666666; font-style: italic;">#1 FAILED at 670.</span>
Hunk <span style="color: #666666; font-style: italic;">#2 FAILED at 1683.</span>
Hunk <span style="color: #666666; font-style: italic;">#3 FAILED at 1767.</span>
<span style="color: #000000;">3</span> out of <span style="color: #000000;">3</span> hunks FAILED <span style="color: #660033;">--</span> saving rejects to <span style="color: #c20cb9; font-weight: bold;">file</span> support<span style="color: #000000; font-weight: bold;">/</span>ab.c.rej</pre></div></div>

<p>然后按前面的步骤编译Apache的support目录。</p>
<p>参考：<a href="http://forrst.com/posts/Fixing_ApacheBench_bug_on_Mac_OS_X_Lion-wku">这里</a>和<a href="https://www.rtfm.ro/uncategorized/apache-benchmark-bug-pe-freebsd/">补丁作者</a>。</p>
<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/ab/" rel="tag">AB</a>, <a href="http://sunliwen.com/tag/apache/" rel="tag">Apache</a>, <a href="http://sunliwen.com/tag/mac/" rel="tag">Mac</a><br/>
<a href="http://sunliwen.com/2012/01/mac-ab-apr-error/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2012/01/mac-ab-apr-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ubuntu下PHP哈希冲突漏洞快修</title>
		<link>http://sunliwen.com/2012/01/ubuntu-php-collision-workaround/</link>
		<comments>http://sunliwen.com/2012/01/ubuntu-php-collision-workaround/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 01:21:09 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1895</guid>
		<description><![CDATA[方法如下： sudo apt-get install php5-suhosin sudo sed -i &#34;s/;suhosin\.post\.max_vars/suhosin\.post\.max_vars/&#34; /etc/php5/fpm/conf.d/suhosin.ini grep suhosin.post.max_vars /etc/php5/fpm/conf.d/suhosin.ini sudo /etc/init.d/php5-fpm restart 输出是： suhosin.post.max_vars = 1000 #限制最多有1000个POST参数 看PHP版本信息为： $ php -v PHP 5.3.2-1ubuntu4.11 with Suhosin-Patch &#40;cli&#41; &#40;built: Dec 13 2011 18:45:32&#41; Copyright &#40;c&#41; 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright &#40;c&#41; 1998-2010 Zend Technologies with Suhosin v0.9.29, Copyright &#40;c&#41; 2007, [...]]]></description>
			<content:encoded><![CDATA[<p>方法如下：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> php5-suhosin
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-i</span> <span style="color: #ff0000;">&quot;s/;suhosin\.post\.max_vars/suhosin\.post\.max_vars/&quot;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>fpm<span style="color: #000000; font-weight: bold;">/</span>conf.d<span style="color: #000000; font-weight: bold;">/</span>suhosin.ini
<span style="color: #c20cb9; font-weight: bold;">grep</span> suhosin.post.max_vars <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>fpm<span style="color: #000000; font-weight: bold;">/</span>conf.d<span style="color: #000000; font-weight: bold;">/</span>suhosin.ini
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>php5-fpm restart</pre></div></div>

<p>输出是：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">suhosin.post.max_vars = <span style="color: #000000;">1000</span>  <span style="color: #666666; font-style: italic;">#限制最多有1000个POST参数</span></pre></div></div>

<p>看PHP版本信息为：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ php <span style="color: #660033;">-v</span>
PHP 5.3.2-1ubuntu4.11 with Suhosin-Patch <span style="color: #7a0874; font-weight: bold;">&#40;</span>cli<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>built: Dec <span style="color: #000000;">13</span> <span style="color: #000000;">2011</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">45</span>:<span style="color: #000000;">32</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">1997</span>-<span style="color: #000000;">2009</span> The PHP Group
Zend Engine v2.3.0, Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">1998</span>-<span style="color: #000000;">2010</span> Zend Technologies
    with Suhosin v0.9.29, Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2007</span>, by SektionEins GmbH</pre></div></div>

<p>注：测试环境是Ubuntu 10.04 + PHP 5.3.2 + PHP5-FPM</p>
<p>refs:<br />
<a href="http://www.slideshare.net/chaoslawful/luaphp">PHP哈希冲突浅析</a><br />
<a href="http://www.hardened-php.net/suhosin/">suhosin</a><br />
<a href="http://arstechnica.com/civis/viewtopic.php?f=16&amp;t=1164260">Testing vs the hash collision vulnerability</a></p>
<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/linux/" rel="tag">Linux</a>, <a href="http://sunliwen.com/tag/php/" rel="tag">PHP</a><br/>
<a href="http://sunliwen.com/2012/01/ubuntu-php-collision-workaround/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2012/01/ubuntu-php-collision-workaround/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python处理Last-Modified</title>
		<link>http://sunliwen.com/2011/11/python-handle-last-modified/</link>
		<comments>http://sunliwen.com/2011/11/python-handle-last-modified/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 18:19:00 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1702</guid>
		<description><![CDATA[Python检查某个URI是否有修改，可以检查HTTP Response的Last-Modified字段 import urllib2 import datetime from urllib2 import HTTPError import datetime def check_modified&#40;url, last_modified, field_name=&#34;Last-Modified&#34;&#41;: request = urllib2.Request&#40;url&#41; opener = urllib2.build_opener&#40;&#41; request.add_header&#40;'If-Modified-Since', last_modified&#41; try: resp = opener.open&#40;request&#41; print &#34;Last-Modified: &#34; + resp.headers.get&#40;field_name&#41; except HTTPError as e: if 304 == e.code: print &#34;Not Modified Since: &#34; + last_modified &#160; last_modified = datetime.datetime&#40;2011,11,15,0,0&#41;.strftime&#40;&#34;%a, %d %h %Y [...]]]></description>
			<content:encoded><![CDATA[<p>Python检查某个URI是否有修改，可以检查HTTP Response的Last-Modified字段</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">urllib2</span> <span style="color: #ff7700;font-weight:bold;">import</span> HTTPError
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">def</span> check_modified<span style="color: black;">&#40;</span>url, last_modified, field_name=<span style="color: #483d8b;">&quot;Last-Modified&quot;</span><span style="color: black;">&#41;</span>:
    request = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span> 
    opener = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">build_opener</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
    request.<span style="color: black;">add_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'If-Modified-Since'</span>, last_modified<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        resp = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Last-Modified: &quot;</span> + resp.<span style="color: black;">headers</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>field_name<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> HTTPError <span style="color: #ff7700;font-weight:bold;">as</span> e:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff4500;">304</span> == e.<span style="color: #dc143c;">code</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Not Modified Since: &quot;</span> + last_modified
&nbsp;
last_modified = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2011</span>,<span style="color: #ff4500;">11</span>,<span style="color: #ff4500;">15</span>,<span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>.<span style="color: black;">strftime</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%a, %d %h %Y %H:%M:%S GMT&quot;</span><span style="color: black;">&#41;</span>
check_modified<span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.kuaishubao.com'</span>, last_modified<span style="color: black;">&#41;</span></pre></div></div>

<p>有的服务器并不返回Last-Modified, 所以需要看情况处理，比如</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">check_modified<span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://sunliwen.com'</span>, last_modified, field_name=<span style="color: #483d8b;">&quot;date&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>参考：<a href="http://www.faqs.org/rfcs/rfc2616.html">http://www.faqs.org/rfcs/rfc2616.html</a></p>
<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/http/" rel="tag">HTTP</a>, <a href="http://sunliwen.com/tag/python/" rel="tag">Python</a><br/>
<a href="http://sunliwen.com/2011/11/python-handle-last-modified/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2011/11/python-handle-last-modified/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何检测浏览器和操作系统的信息</title>
		<link>http://sunliwen.com/2011/11/detect-useragent/</link>
		<comments>http://sunliwen.com/2011/11/detect-useragent/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 19:17:53 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1664</guid>
		<description><![CDATA[在实现一个功能时，需从UserAgent中抽取出浏览器和操作系统的信息。 浏览器： Opera Chrome Internet Explorer Safari Firefox 操作系统： Windows iPad iPhone Android Mac OS X Linux 实现方法如下： function getBrowser&#40;&#41; &#123; var agent = navigator.userAgent; return window.opera ? &#34;Opera&#34; : /chrom/i.test&#40;agent&#41; ? &#34;Chrome&#34; : /msie/i.test&#40;agent&#41; ? &#34;Internet Explorer&#34; : /applewebkit/i.test&#40;navigator.appVersion&#41; ? &#34;Safari&#34; : /mozilla/i.test&#40;agent&#41; &#38;&#38; !/compatible&#124;webkit/i.test&#40;agent&#41; ? &#34;Firefox&#34; : &#34;&#34;; &#125; &#160; function getOS&#40;&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>在实现一个功能时，需从UserAgent中抽取出浏览器和操作系统的信息。</p>
<p>浏览器：</p>
<ul>
<li>Opera</li>
<li>Chrome</li>
<li>Internet Explorer</li>
<li>Safari</li>
<li>Firefox</li>
</ul>
<p>操作系统：</p>
<ul>
<li>Windows</li>
<li>iPad</li>
<li>iPhone</li>
<li>Android</li>
<li>Mac OS X</li>
<li>Linux</li>
</ul>
<p>实现方法如下：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getBrowser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> agent <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">userAgent</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> window.<span style="color: #660066;">opera</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;Opera&quot;</span>
        <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/chrom/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;Chrome&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/msie/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;Internet Explorer&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/applewebkit/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">appVersion</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;Safari&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/mozilla/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!/</span>compatible<span style="color: #339933;">|</span>webkit<span style="color: #339933;">/</span>i.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;Firefox&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> getOS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> agent <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">userAgent</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009966; font-style: italic;">/windows/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;Windows&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/ipad/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;iPad&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/iphone/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;iPhone&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/android/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;Android&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/mac/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;Mac OS X&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #009966; font-style: italic;">/x11/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009966; font-style: italic;">/linux/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>agent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;Linux&quot;</span> 
        <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>需要注意的是，检测顺序敏感。</p>
<p>参考：<br />
* <a href="http://www.quirksmode.org/js/detect.html">Browser detect</a><br />
* <a href="http://detectmobilebrowsers.com/">Detect Mobile Browsers</a></p>
<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/javascript/" rel="tag">Javascript</a><br/>
<a href="http://sunliwen.com/2011/11/detect-useragent/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2011/11/detect-useragent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用Python Decorator简化代码</title>
		<link>http://sunliwen.com/2011/10/python-decorator/</link>
		<comments>http://sunliwen.com/2011/10/python-decorator/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 06:39:20 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1508</guid>
		<description><![CDATA[看码不说话。 def deco&#40;callable&#41;: def method&#40;a, b&#41;: print &#34;a: %s&#34; % a print &#34;b: %s&#34; % b return callable&#40;a, b&#41; return method &#160; @deco def test1&#40;a,b&#41;: print a+b &#160; test1&#40;10,20&#41; 上面的Decorator只能装饰有两个参数的函数，否则 @deco def test2&#40;a,b,c&#41;: print a+b+c 会报错 &#62;&#62;&#62; test2&#40;10,20,30&#41; Traceback &#40;most recent call last&#41;: File &#34;&#60;stdin&#62;&#34;, line 1, in &#60;module&#62; TypeError: method&#40;&#41; takes exactly 2 arguments [...]]]></description>
			<content:encoded><![CDATA[<p>看码不说话。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> deco<span style="color: black;">&#40;</span><span style="color: #008000;">callable</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> method<span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;a: %s&quot;</span> <span style="color: #66cc66;">%</span> a
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;b: %s&quot;</span> <span style="color: #66cc66;">%</span> b
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> method
&nbsp;
@deco
<span style="color: #ff7700;font-weight:bold;">def</span> test1<span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+b
&nbsp;
test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span></pre></div></div>

<p>上面的Decorator只能装饰有两个参数的函数，否则</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@deco
<span style="color: #ff7700;font-weight:bold;">def</span> test2<span style="color: black;">&#40;</span>a,b,c<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+b+c</pre></div></div>

<p>会报错</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span>,<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;&lt;stdin&gt;&quot;</span>, line <span style="color: #ff4500;">1</span>, <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>module<span style="color: #66cc66;">&gt;</span>
<span style="color: #008000;">TypeError</span>: method<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> takes exactly <span style="color: #ff4500;">2</span> arguments <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span> given<span style="color: black;">&#41;</span></pre></div></div>

<p>如需支持装饰任意参数的函数，得使用以下方法：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> deco<span style="color: black;">&#40;</span><span style="color: #008000;">callable</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> method<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kws<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;args: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;kws: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span>kws<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kws<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> method
&nbsp;
@deco
<span style="color: #ff7700;font-weight:bold;">def</span> test1<span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+b
&nbsp;
@deco
<span style="color: #ff7700;font-weight:bold;">def</span> test2<span style="color: black;">&#40;</span>a,b,c<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+b+c
&nbsp;
test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,b=<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span>,<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,b=<span style="color: #ff4500;">20</span>,c=<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span></pre></div></div>

<p>结果是：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
<span style="color: #ff4500;">30</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,b=<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: black;">&#41;</span>
args: <span style="color: black;">&#123;</span><span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;">20</span><span style="color: black;">&#125;</span>
<span style="color: #ff4500;">30</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span>,<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">20</span>, <span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
<span style="color: #ff4500;">60</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,b=<span style="color: #ff4500;">20</span>,c=<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: black;">&#41;</span>
args: <span style="color: black;">&#123;</span><span style="color: #483d8b;">'c'</span>: <span style="color: #ff4500;">30</span>, <span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;">20</span><span style="color: black;">&#125;</span>
<span style="color: #ff4500;">60</span></pre></div></div>

<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/python/" rel="tag">Python</a><br/>
<a href="http://sunliwen.com/2011/10/python-decorator/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2011/10/python-decorator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在MongoDB中sum某个字段</title>
		<link>http://sunliwen.com/2011/10/mongodb-sum/</link>
		<comments>http://sunliwen.com/2011/10/mongodb-sum/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 08:26:54 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MapReduce]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1489</guid>
		<description><![CDATA[假设每一条记录有一个叫做pv的整数字段，求整个集合中这个字段的和可以用如下方法: &#62; use test switched to db test &#62; db.statistics.insert&#40;&#123;created_on: &#34;2011-10-01&#34;, pv: 100&#125;&#41; &#62; db.statistics.insert&#40;&#123;created_on: &#34;2011-10-02&#34;, pv: 200&#125;&#41; &#62; db.statistics.insert&#40;&#123;created_on: &#34;2011-10-03&#34;, pv: 300&#125;&#41; &#62; var reduce = function&#40;key, values&#41;&#123; ... var count=0; ... values.forEach&#40;function&#40;v&#41; &#123; ... count+=v.pv; ... &#125;&#41;; ... return &#123;count: count&#125;; ... &#125;; &#62; var s = db.statistics.find&#40;&#41;; &#62; reduce&#40;&#34;total_pv&#34;, s&#41;; 结果如下: &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>假设每一条记录有一个叫做pv的整数字段，求整个集合中这个字段的和可以用如下方法:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> <span style="color: #003366; font-weight: bold;">use</span> test
switched to db test
<span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">statistics</span>.<span style="color: #660066;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>created_on<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2011-10-01&quot;</span><span style="color: #339933;">,</span> pv<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">statistics</span>.<span style="color: #660066;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>created_on<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2011-10-02&quot;</span><span style="color: #339933;">,</span> pv<span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">statistics</span>.<span style="color: #660066;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>created_on<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2011-10-03&quot;</span><span style="color: #339933;">,</span> pv<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&gt;</span> <span style="color: #003366; font-weight: bold;">var</span> reduce <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> values<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
...     <span style="color: #003366; font-weight: bold;">var</span> count<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
...     <span style="color: #660066;">values</span>.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...        <span style="color: #660066;">count</span><span style="color: #339933;">+=</span>v.<span style="color: #660066;">pv</span><span style="color: #339933;">;</span>
...     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...     <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>count<span style="color: #339933;">:</span> count<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
... <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&gt;</span> <span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> db.<span style="color: #660066;">statistics</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&gt;</span> reduce<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;total_pv&quot;</span><span style="color: #339933;">,</span> s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>结果如下:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;count&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">600</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/mapreduce/" rel="tag">MapReduce</a>, <a href="http://sunliwen.com/tag/mongodb/" rel="tag">MongoDB</a>, <a href="http://sunliwen.com/tag/nosql/" rel="tag">NoSQL</a><br/>
<a href="http://sunliwen.com/2011/10/mongodb-sum/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2011/10/mongodb-sum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nginx将所有请求转向同一个host</title>
		<link>http://sunliwen.com/2011/09/nginx-rewrite-host/</link>
		<comments>http://sunliwen.com/2011/09/nginx-rewrite-host/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 05:38:38 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[Ops]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1455</guid>
		<description><![CDATA[准备只保留 (sunliwen.com) 这一个域名，但因为有些外链，所以加一条rewrite来兼容。 # Redirect all requests to sunliwen.com if ($host != sunliwen.com) { rewrite ^(.*)$ http://sunliwen.com$1 permanent; } 参考： Nginx &#8211; HttpRewriteModule Tags: Nginx, Ops 2个评论]]></description>
			<content:encoded><![CDATA[<p><!--:zh-->准备只保留 (sunliwen.com) 这一个域名，但因为有些外链，所以加一条rewrite来兼容。</p>

<div class="wp_syntax"><div class="code"><pre class="nginx" style="font-family:monospace;"># Redirect all requests to sunliwen.com
if ($host != sunliwen.com) {
    rewrite ^(.*)$ http://sunliwen.com$1 permanent;
}</pre></div></div>

<p>参考：</p>
<ul>
<li><a href="http://www.nginx.org">Nginx</a> &#8211; <a href="http://wiki.nginx.org/HttpRewriteModule">HttpRewriteModule</a><br />
<!--:--></li>
</ul>
<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/nginx/" rel="tag">Nginx</a>, <a href="http://sunliwen.com/tag/ops/" rel="tag">Ops</a><br/>
<a href="http://sunliwen.com/2011/09/nginx-rewrite-host/#comments-heading">2个评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2011/09/nginx-rewrite-host/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ubuntu下安装PHP-FPM</title>
		<link>http://sunliwen.com/2011/09/fabric-install-nginx-phpfpm-on-ubuntu/</link>
		<comments>http://sunliwen.com/2011/09/fabric-install-nginx-phpfpm-on-ubuntu/#comments</comments>
		<pubDate>Sat, 17 Sep 2011 17:28:13 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[Ops]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1344</guid>
		<description><![CDATA[把VPS的Lenny5换成了Ubuntu10.04，借这个机会试一下效率更高的 PHP-FPM 。 以下是fabric脚本： def init_nmp&#40;&#41;: install_nginx&#40;&#41; install_php&#40;&#41; install_phpfpm&#40;&#41; &#160; def install_nginx&#40;&#41;: sudo&#40;&#34;add-apt-repository ppa:nginx/stable&#34;&#41; sudo&#40;&#34;apt-get update&#34;&#41; sudo&#40;&#34;apt-get -y -q install nginx-full nginx-common&#34;&#41; &#160; def install_php&#40;&#41;: # More packages upon request sudo&#40;&#34;apt-get -y -q --force-yes install php5-cli php5-cgi php5-mysql&#34;&#41; sudo&#40;&#34;apt-get -y -q --force-yes install php5-mcrypt libmcrypt mcrypt&#34;&#41; &#160; def install_phpfpm&#40;&#41;: # TODO: use canonical php-fpm package [...]]]></description>
			<content:encoded><![CDATA[<p>把VPS的Lenny5换成了Ubuntu10.04，借这个机会试一下效率更高的 <a href="http://php-fpm.org">PHP-FPM</a> 。</p>
<p>以下是<a href="http://fabfile.org">fabric</a>脚本：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> init_nmp<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    install_nginx<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    install_php<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    install_phpfpm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> install_nginx<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;add-apt-repository ppa:nginx/stable&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get update&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get -y -q install nginx-full nginx-common&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> install_php<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># More packages upon request</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get -y -q --force-yes install php5-cli php5-cgi php5-mysql&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get -y -q --force-yes install php5-mcrypt libmcrypt mcrypt&quot;</span><span style="color: black;">&#41;</span> 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> install_phpfpm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># TODO: use canonical php-fpm package when available</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;add-apt-repository ppa:brianmercer/php&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get update&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get -y -q install php5-fpm&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>在<code>/etc/php5/fpm/php5-fpm.conf</code>里将tcp修改成unix socket方式，单机情况下性能更好。</p>

<div class="wp_syntax"><div class="code"><pre class="config" style="font-family:monospace;">listen = /var/run/php5-fpm.sock
;listen = 127.0.0.1:9000</pre></div></div>

<p>Nginx里的配置:</p>

<div class="wp_syntax"><div class="code"><pre class="config" style="font-family:monospace;">location ~ \.php$ { 
    try_files $uri = 404; 
    include /etc/nginx/fastcgi_params; 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
&nbsp;
    fastcgi_split_path_info         ^(.+\.php)(.*)$; 
    include fastcgi_params; 
    fastcgi_intercept_errors        on; 
    fastcgi_ignore_client_abort     off; 
    fastcgi_connect_timeout         60; 
    fastcgi_send_timeout            180; 
    fastcgi_read_timeout            180; 
    fastcgi_buffer_size             128k; 
    fastcgi_buffers             4   256k; 
    fastcgi_busy_buffers_size       256k; 
    fastcgi_temp_file_write_size    256k; 
}</pre></div></div>

<hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/nginx/" rel="tag">Nginx</a>, <a href="http://sunliwen.com/tag/ops/" rel="tag">Ops</a>, <a href="http://sunliwen.com/tag/php/" rel="tag">PHP</a>, <a href="http://sunliwen.com/tag/python/" rel="tag">Python</a>, <a href="http://sunliwen.com/tag/ubuntu/" rel="tag">Ubuntu</a><br/>
<a href="http://sunliwen.com/2011/09/fabric-install-nginx-phpfpm-on-ubuntu/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2011/09/fabric-install-nginx-phpfpm-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.619 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-20 15:14:13 -->

