[ Content | Sidebar ]

Posts tagged python

一个python版的开源web截图工具:python-webkit2png

http://github.com/AdamN/python-webkit2png/
一个使用pyqt4里的QtWebkit实现的命令行的web截图软件,可用于服务器端的web截图
貌似是个比较新的项目,以前找过类似的工具,没有多好用的,但是这个真的很好用,看一下参数就知道了

Creates a screenshot of a website using QtWebkit.This program comes with
ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to
redistribute it under the terms of the GNU General Public License v2.
 
Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -x WIDTH HEIGHT, --xvfb=WIDTH HEIGHT
                        Start an 'xvfb' instance with the given desktop size.
  -g WIDTH HEIGHT, --geometry=WIDTH HEIGHT
                        Geometry of the virtual browser window (0 means
                        'autodetect') [default: (0, 0)].
  -o FILE, --output=FILE
                        Write output to FILE instead of STDOUT.
  -f FORMAT, --format=FORMAT
                        Output image format [default: png]
  --scale=WIDTH HEIGHT  Scale the image to this size
  --aspect-ratio=RATIO  One of 'ignore', 'keep', 'crop' or 'expand' [default:
                        none]
  -F FEATURE, --feature=FEATURE
                        Enable additional Webkit features ('javascript',
                        'plugins')
  -w SECONDS, --wait=SECONDS
                        Time to wait after loading before the screenshot is
                        taken [default: 0]
  -t SECONDS, --timeout=SECONDS
                        Time before the request will be canceled [default: 0]
  -W, --window          Grab whole window instead of frame (may be required
                        for plugins)
  --style=STYLE         Change the Qt look and feel to STYLE (e.G. 'windows').
  -d DISPLAY, --display=DISPLAY
                        Connect to X server at DISPLAY.
  --debug               Show debugging information.
  --log=LOGFILE         Select the log output file
Copy Code 

使用举例:
使用之前安装依赖的包

sudo apt-get install python-qt4
Copy Code 

OK,可以用了,假如在用ubuntu桌面版的话就很简单了

#加上运行权限 
chmod +x ./webkit2png.py
#高度设置为0,就可以获得整个页面的截图了
./webkit2png.py -g 1024 0 http://bluehua.org -o test.png
Copy Code 

假如运行在服务器上,没有x环境,则需要安装一些额外的依赖包

#安装xvfb用于虚拟X环境
apt-get install xvfb
#安装中文字体
apt-get install xfonts-wqy
#配置字体
fontconfig-voodoo -f -s zh_CN
Copy Code 

运行时需要加额外的参数

#x选项用于指定虚拟出来的X桌面的尺寸
./webkit2png.py -x 1024 768 -g 1024 0 http://bluehua.org -o test.png
Copy Code 

恩,其实我找这个东西的目的是想在服务器端获取一个页面的实际渲染宽度,目的达成~

———–
post by gmail~

相对完美的设计思路~

有没有可能写一套代码适应以下三种情况:
1. 客户端没有开启javascript,或者使用ucweb一类的弱智浏览器访问时,功能没有影响
2. 客户端开启了javascript,每个页面都有单独加载的js,但是禁用ajax,只帮助用户交互
3. 客户端开启了javascript,为了提高载入速度,尽量使所有操作用ajax完成

略有所得: http://www.blogo2.com
源代码: http://code-of-emptyhua.googlecode.com/svn/trunk/blogo2

没有禁用js的时候,页面切换,表单提交(注册和登录没有用)尽量的使用ajax,而且部分操作会使用弹层(登录后的管理),禁用js之后,则所有操作换成普通表单提交在页面之间跳来跳去。。

还有一个功能没有开启,就是可以选择不用ajax,但是独立开的页面js依然有效。

有兴趣的同学可以check源代码,这种模式的实现:
1. 开发的时候禁用ajax,单个页面开发,而且要求禁用js时业务可以走通,页面内的js和css统一由模板引入。
2. 对需要开启ajax的链接增加hook,例如data-ajax=”dialog:{}”,则表明在开启ajax模式后,点击这个链接需要弹窗
3. ajax模式下只需要对单个页面更换特有的模板,动态加载需要的js和css即可。

这种设计的特点:
1. 尽量一个人包前后端。。
2. json不潮了,ajax返回html代码,优点是js不用套页面了,缺点是流量大

over,虽然没有完美的代码,但是我尽量~

———–
post by gmail~

多线程使用pycurl时出现段错误(segmentation fault)

http://www.python-forum.org/pythonforum/viewtopic.php?f=5&t=9395

只需要加一个选项

client.setopt(pycurl.NOSIGNAL, 1)
Copy Code 

文档的解释

Pass a long. If it is 1, libcurl will not use any functions that install signal handlers or any functions that cause signals to be sent to the process. This option is mainly here to allow multi-threaded unix applications to still set/use all timeout options etc, without risking getting signals. (Added in 7.10)

If this option is set and libcurl has been built with the standard name resolver, timeouts will not occur while the name resolve takes place. Consider building libcurl with c-ares support to enable asynchronous DNS lookups, which enables nice timeouts for name resolves without signals.

一个支持url变量的压力测试工具pywebbench

可能是我的本子配置不行,服务器上连接数可以上1000

./pywebbench.py -c 1000 -t 10 http://172.16.0.38/test.php
 
1000 clients, running 10 sec, timeout setting 2.
Requests: 7387 total ,7385 susceed, average 99%.
2 fail:
timeout:1
connect refused:1
not 2xx status:0
Copy Code 

————————————-
2010.7.14更新 可以输出更多信息,svn版本7

100 clients, running 10 sec, timeout setting 2.
Requests: 1361 total ,1027 susceed, average 75%.
334 fail:
timeout:254
connect refused:80
not 2xx status:0
Copy Code 

—————————–
测试需要,特别是测试缓存,逮住一个url压没什么用,所以写了一个pywebbench满足需求

#url中的id会顺序变化
pywebbench.py -c 100 -t 10 http://test.com/xx.php?id={10000,12000000}
#url中的id随机变化
pywebbench.py -c 100 -t 10 http://test.com/xx.php?id={10000,12000000#random}
Copy Code 

实际数据

#这是pywebbench跑的数据,curl这么牛逼吗,远远把webbench落在后面。。。
$python ~/bin/pywebbench.py -c 300 -t 10 http://localhost/
 
300 clients, running 10 sec.
Requests: 20430 total 19318 susceed, 1112 failed.
 
#这是webbench跑的数据
$webbench -c 300 -t 10 http://localhost/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
 
Benchmarking: GET http://localhost/
300 clients, running 10 sec.
 
Speed=56964 pages/min, 3997446 bytes/sec.
Requests: 9494 susceed, 0 failed.
Copy Code 

已知问题:
python的线程貌似开不多,-c 500 开不了。。

下载:
http://code-of-emptyhua.googlecode.com/svn/trunk/pywebbench.py
需要安装pycurl:sudo apt-get install python-pycurl

terminal里看农历的东西

曾经装了一个 liblunar ,用来在gnome panel里显示农历日历,今天升级到10.04,发现不灵了。由于liblunar提供了python的lib,于是简单写了一个命令行版的前端,顺便保持对python的熟悉程度。最近接触的语言有点杂,码代码时经常会搞混。。。

下载:zhcal.zip

使用之前需要安装liblunar,编译选项

./configure --prefix=/usr --enable-python
make
sudo make install
Copy Code 

———————————–
另:分享一个firefox插件default full zoom level, 可以设置页面的默认缩放级别,对于高分屏的同学会有帮助~
现在终端的字体我已经调到16,浏览器默认缩放120%,基本上回到1024时代~