9月 27

linux下用python生成二维码


【张子萌 2011-9-27】
库的官方网站

http://fukuchi.org/works/qrencode/index.en.html

需要使用CythonCython是用来生成 C 扩展到而不是独立的程序的。所有的加速都是针对一个已经存在的 Python

用的一个函数进行的。还需要安装PIL

先下载了qrencode-3.1.1.tar.gz,使用configuremakemake install进行安装,手动生成了一个图,却是可用。

开始制作。

安装Cypthon

# easy_install cython

在官方下载bitly-pyqrencode-1cfb23c.tar.gz

# tar zxvf bitly-pyqrencode-1cfb23c.tar.gz

# cd bitly-pyqrencode-1cfb23c

使用Cpython安装qrencode

# cython qrencode.pyx

阅读README确认系统中有以下文件

you need libqrencode somewhere in your LD path (/usr/local/lib)

you need qrencode.h somewhere on your include path (/usr/local/include)

# python setup.py install

安装完毕测试以下。

脚本如下

from qrencode import Encoder

cre = Encoder()

img = cre.encode(“博客地址:http://simon-zzm.blog.163.com/ n电子邮件:simon-zzm@163.com”, { ‘width’: 300 })

img.save(‘out.png’)

生成图片如下:

linux下用python生成二维码 - simon-zzm - simon个人观点

9月 27

[转发] nodejs下mysql性能测试

nodejs下mysql性能测试

By qingdu on 2011 年 02 月 14 日
本文为原创文章,出自http://cnodejs.org,转载请注明出处和作者
作者:qingdu
原文:http://cnodejs.org/blog/?p=404

近期对node下的几个mysql操作库做了一下简单的调研
主要是针对这3个module进行
libmysqlclient,mysql-native, mysql

node-libmysqlclient

https://github.com/Sannis/node-mysql-libmysqlclient
基于libmysql的封装, 支持阻塞与非阻塞两种操作方式,目前在query部分功能比较完善,prepare statement方面还是有些bug,实际测试中一直出core

安装

npm install mysql-libmysqlclient

node-mysql

https://github.com/felixge/node-mysql
纯node的实现, 支持prepare statement, 数据的获取支持事件与callback两种方式

安装

npm install mysql

node-mysql-native

https://github.com/sidorares/nodejs-mysql-native

纯node的实现, 内置有简单的连接池支持,支持prepare statement, 数据的获取只支持通过事件方式

安装

npm install mysql-native

测试设计

测试表结构

CREATE TABLE `test10` (
`id` int(10) unsigned NOT NULL DEFAULT '0',
`txt` varchar(500) DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

测试数据

分别生成了含10, 100, 500, 1500行记录的4张表
使用10个并发连接分别对其进行SELECT * FROM table的查询

测试结论

NewImage.jpg

其中数值表示qps值

在结果集较小时node-mysql-native明显胜出
当结果集增大到接近100行时,node-libmysqlclient开始表现出更佳的性能
node-mysql性能与其他两个module差距比较大,如果对性能有一定要求的话,则其基本不可用

测试代码

http://code.google.com/p/cnodejs/source/browse/#svn%2Ftrunk%2Fmysql-benchmark

9月 26

centos安装node.js简单测试

操作系统

Centos 5.4 Linux 2.6.18-164.el5 4G内存、Xeon(TM) CPU 3.20GHz

node-v0.5.7

node的官方网站http://nodejs.org/ 使用官方网站示例。

再安装前请将开发工具(tcl-developenssl-develglibc-devel bzip2*等)安装,可以使用yum工具。

安装

# tar zxvf node-v0.5.7.tar.gz

# cd node-v0.5.7

# ./configure

# make

有报make: [all] Error 1 (ignored) 但是没有找到问题,忽略了直接做下一步。

# make install

收到安装成功讯息。

建立一个node.js档案。

vim node.js

var http = require(‘http’);

http.createServer(function (req, res) {

res.writeHead(200, {‘Content-Type’: ‘text/plain’});

res.end(‘Hello Node.jsn‘);

}).listen(8124, “192.168.1.200”);

console.log(‘Server running at http://192.168.1.200:8124/’);

执行node.js

# node node.js

打开浏览器使用连接http:// 192.168.1.200:8124看到Hello Node.js,就表示node.js已经正在运作中。

进行个简单测试,使用apacheab工具

命令# ./ab -c10000 -n 40000 http:// 192.168.1.200:8124/

Complete requests: 40000

并发数: 3498.75 [#/sec]

平均返回相应时间: 0.286 [ms]

传输量: 266.51 [Kbytes/sec]

无请求失败。

在同一台机器上使用django写个,测试一下。

环境Python 2.6.6 django 1.3.0 关闭debug状态

代码为:

#!/bin/evn python

from django.http import HttpResponse

def index(request):

html = “Hello Djangon”

return HttpResponse(html)

直接使用python自带的调试服务器

# python manage.py runserver 192.168.1.200:9000

因为4000030000时将服务器跑死了(报错apr_socket_recv: Connection timed out (110)),所以修改为25000进行测试,结果也死了,改20000也死了,直接改15000也死了。算了我不测了。

总结:初步看node js效率确实很高,python+django自带的服务器的效

7月 08

python mongodb 变量做集合(collection)名

【2011-7-8 张子萌】
当前需求:1)将某个目录下的html文件插入到mongo数据库中。
2)cellection名为html页的上级目录名。
3)document为文件名、html内容

测试目录位置/home/test

#!/usr/bin/python
#-*- coding:utf-8 -*-
import re
import os
import pymongo

find_file=re.compile(r”.html$”)
find_path=r”/home/test”
find_walk=os.walk(find_path)
conn = pymongo.Connection(“localhost”,27017)
db = conn.mytest
for path,dirs,files in find_walk:
for file in files:
if find_file.search(file):
collection_name=path.split(“/”)[2]
get_html=open(path+”/”+file,’r’)
exec(‘db.’+collection_name+’.save({“file_name”:file,”context”:get_html.read()})’)
get_html.close()

遇到一个问题,好像collection如只是数字(int)插入有问题,所以如果用数字名做collection名可以在前面加个字母。

6月 27

python测试连接mongodb 简单读写

下载pymongo模块,测试机python为2.6.6
[2011-6-27 张子萌]
# wget http://pypi.python.org/packages/source/p/pymongo/pymongo-1.11.tar.gz
解压安装
# tar zxvf pymongo-1.11.tar.gz
# cd pymongo-1.11
# python setup.py install

安装比较简单,下面写一个脚本测试一下是否成功。返回的也是dict。

#!/usr/bin/python
#-*- coding:utf-8 -*-
import pymongo
conn = pymongo.Connection(“localhost”,27017)
db = conn.mytest
cursor = db.mytest.find()
for i in cursor:
print i

有结果就ok了。

a)测试1存取二进制

插入MP3试试看。首先上传一个test.mp3,然后把MP3转为二进制,最后入库。
#!/usr/bin/python
#-*- coding:utf-8 -*-
import pymongo
import bson
conn = pymongo.Connection(“localhost”,27017)
db = conn.mytest
get_mp3=open(‘test.mp3′,’rb’) #以二进制方法读取MP3
bin=bson.Binary(get_mp3.read()) #转换对象
db.mytest.save({“file_name”:”test_mp3″,”mp3″:bin}) #保存入库
get_mp3.close()

如库查询看到以下结果,MP3已经入库成功。
> db.mytest.find({},{file_name:1})
{ “_id” : ObjectId(“4e0a70d0b4a1024472000000”), “file_name” : “test_mp3” }

现在在把MP3读出来看看是否可以用。
#!/usr/bin/python
#-*- coding:utf-8 -*-
import pymongo
conn = pymongo.Connection(“localhost”,27017)
db = conn.mytest
cursor=db.mytest.find({“file_name” : “test_mp3”},{“mp3”:1})
file=open(‘123.mp3′,’wb’)
print >>file,cursor[0][“mp3”] #因为测试库中就有一首所以不用循环了
file.close

取出MP3试听是否成功。

b)测试2存取字符型。文本文件保存也可以用二进制,但是字符型更好,便于索引和
查找。
首先建立一个test.txt测试文本,内容如下:
first
second
three
four

用以下脚本入库
#!/usr/bin/python
#-*- coding:utf-8 -*-
import pymongo
conn = pymongo.Connection(“localhost”,27017)
db = conn.mytest
get_txt=open(‘test.txt’,’r’)
for line in get_txt: #也可以不用循环,将文件插入到一个值里
db.mytest.insert({“file_name”:”test_txt”,”content”:line})
get_txt.close()

入库完毕登录mongodb检查,结果如下:
> db.mytest.find({“file_name”:”test_txt”},{})
{ “_id” : ObjectId(“4e0a762bb4a1024508000000”), “content” : “firstn”, “file_name” : “test_txt” }
{ “_id” : ObjectId(“4e0a762bb4a1024508000001”), “content” : “secondn”, “file_name” : “test_txt” }
{ “_id” : ObjectId(“4e0a762bb4a1024508000002”), “content” : “threen”, “file_name” : “test_txt” }
{ “_id” : ObjectId(“4e0a762bb4a1024508000003”), “content” : “fourn”, “file_name” : “test_txt” }

现在在把文本读出来看看是否可以用。
#!/usr/bin/python
#-*- coding:utf-8 -*-
import pymongo
conn = pymongo.Connection(“localhost”,27017)
db = conn.mytest
cursor=db.mytest.find({“file_name” : “test_txt”},{“content”:1})
file=open(‘123.txt’,’a’)
for i in cursor:
print >>file,i[“content”]
file.close

查看文本已经输出,因为是追加,并且在库里保存了回车符“n”,所以输出的文件都会隔一行写一行。

http://api.mongodb.org/python/1.11/installation.html
http://pypi.python.org/pypi/pymongo/