ab(Apache Benchmark)运行apr_socket_recv报错

发表于2012年1月12日

在Mac OS X Lion下运行ab会报如下错误:

apr_socket_recv: Connection reset by peer (54)

比如:

$ ab -n 1000 -c 10 http://localhost:8888/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
 
Benchmarking localhost (be patient)
Send request failed!
Send request failed!
Send request failed!
apr_socket_recv: Connection reset by peer (54)

新版的Apache已经解决了这个问题,可以获取源代码并编译如下:

wget http://mirrors.kahuki.com/apache//httpd/httpd-2.3.16-beta.tar.bz2
tar jxvf httpd-2.3.16-beta.tar.bz2 
cd httpd-2.3.16
./configure

我只需要一个ab,所以只编译support目录即可。

cd support
make
#...

重新运行ab

./ab -n 1000 -c 10 http://localhost:8888/

Bingo! :)

对于老一些的版本的Apache,需要打补丁

wget https://www.rtfm.ro/download/patches/ab.patch --no-check-certificate
patch -p0 < ./ab.patch

会有一行出错,不要理,继续往下走。

patching file support/ab.c
Hunk #1 FAILED at 670.
Hunk #2 FAILED at 1683.
Hunk #3 FAILED at 1767.
3 out of 3 hunks FAILED -- saving rejects to file support/ab.c.rej

然后按前面的步骤编译Apache的support目录。

参考:这里补丁作者

Ubuntu下PHP哈希冲突漏洞快修

发表于2012年1月5日

方法如下:

sudo apt-get install php5-suhosin
sudo sed -i "s/;suhosin\.post\.max_vars/suhosin\.post\.max_vars/" /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 (cli) (built: Dec 13 2011 18:45:32) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Suhosin v0.9.29, Copyright (c) 2007, by SektionEins GmbH

注:测试环境是Ubuntu 10.04 + PHP 5.3.2 + PHP5-FPM

refs:
PHP哈希冲突浅析
suhosin
Testing vs the hash collision vulnerability

Python处理Last-Modified

发表于2011年11月16日

Python检查某个URI是否有修改,可以检查HTTP Response的Last-Modified字段

import urllib2
import datetime
from urllib2 import HTTPError
import datetime
def check_modified(url, last_modified, field_name="Last-Modified"):
    request = urllib2.Request(url) 
    opener = urllib2.build_opener() 
    request.add_header('If-Modified-Since', last_modified)
    try:
        resp = opener.open(request)
        print "Last-Modified: " + resp.headers.get(field_name)
    except HTTPError as e:
        if 304 == e.code:
            print "Not Modified Since: " + last_modified
 
last_modified = datetime.datetime(2011,11,15,0,0).strftime("%a, %d %h %Y %H:%M:%S GMT")
check_modified('http://www.kuaishubao.com', last_modified)

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

check_modified('http://sunliwen.com', last_modified, field_name="date")

参考:http://www.faqs.org/rfcs/rfc2616.html

如何检测浏览器和操作系统的信息

发表于2011年11月11日

在实现一个功能时,需从UserAgent中抽取出浏览器和操作系统的信息。

浏览器:

  • Opera
  • Chrome
  • Internet Explorer
  • Safari
  • Firefox

操作系统:

  • Windows
  • iPad
  • iPhone
  • Android
  • Mac OS X
  • Linux

实现方法如下:

function getBrowser() {
    var agent = navigator.userAgent;
    return window.opera ? "Opera"
        : /chrom/i.test(agent) ? "Chrome" 
        : /msie/i.test(agent) ? "Internet Explorer" 
        : /applewebkit/i.test(navigator.appVersion) ? "Safari" 
        : /mozilla/i.test(agent) && !/compatible|webkit/i.test(agent) ? "Firefox" 
        : "";
}
 
function getOS() {
    var agent = navigator.userAgent;
    return /windows/i.test(agent) ? "Windows" 
        : /ipad/i.test(agent) ? "iPad" 
        : /iphone/i.test(agent) ? "iPhone" 
        : /android/i.test(agent) ? "Android" 
        : /mac/i.test(agent) ? "Mac OS X" 
        : /x11/i.test(agent) || /linux/i.test(agent) ? "Linux" 
        : "";
}

需要注意的是,检测顺序敏感。

参考:
* Browser detect
* Detect Mobile Browsers

用Python Decorator简化代码

发表于2011年10月14日

看码不说话。

def deco(callable):
    def method(a, b):
        print "a: %s" % a
        print "b: %s" % b
        return callable(a, b)
    return method
 
@deco
def test1(a,b):
    print a+b
 
test1(10,20)

上面的Decorator只能装饰有两个参数的函数,否则

@deco
def test2(a,b,c):
    print a+b+c

会报错

>>> test2(10,20,30)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: method() takes exactly 2 arguments (3 given)

如需支持装饰任意参数的函数,得使用以下方法:

def deco(callable):
    def method(*args, **kws):
        print "args: %s" % repr(args)
        print "kws: %s" % repr(kws)
        return callable(*args, **kws)
    return method
 
@deco
def test1(a,b):
    print a+b
 
@deco
def test2(a,b,c):
    print a+b+c
 
test1(10,20)
test1(10,b=20)
test2(10,20,30)
test2(10,b=20,c=30)

结果是:

>>> test1(10,20)
args: (10, 20)
args: {}
30
>>> test1(10,b=20)
args: (10,)
args: {'b': 20}
30
>>> test2(10,20,30)
args: (10, 20, 30)
args: {}
60
>>> test2(10,b=20,c=30)
args: (10,)
args: {'c': 30, 'b': 20}
60

无觅相关文章插件,快速提升流量