9月 28

pyinotify监控文件和文件夹变化

【2011-9-28 张子萌】

之前写了脚本,死循环调用inotifywait监控文件夹,如果文件有变动,则启动

rsync进行同步。但是当前需求有点变化,文件要按照日期建文件夹进行存储,

且文件变化很快,如果直接监控最顶级目录系统资源将消耗很大,所以考虑还是自

写一下好,如果系统资源量变化大,且不用时时同步时可以根据文件变动对变动

对文件进行记录,可以按照优先规则进行同步。

官方网站 : http://trac.dbzteam.org/pyinotify

系统要求:Linux kernel with inotify 2.6.13

Python 2.4

直接使用easy_install安装非常简单

# sudo easy_install pyinotify

如果不能联网,则需要直接下载压缩包,进行编译安装。

首先测试下是否可用:

使用以下命令监控/tmp文件夹,

# python -m pyinotify /tmp

/tmp文件夹下新建1.txt,并随后进行删除。看到显示的记录如下:

效果不错,继续向下进行。只要有创建、删除和关闭写就打印出变化的文件或目录,代码如下:

import re

import pyinotify

wm = pyinotify.WatchManager()

mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE

class EventHandler(pyinotify.ProcessEvent):

def process_IN_CREATE(self, event):

self.rebuild(event)

def process_IN_DELETE(self, event):

self.rebuild(event)

def process_IN_CLOSE_WRITE(self, event):

self.rebuild(event)

def rebuild(self, event):

chang_name=re.compile(“.+.swp$|.+.swx$|.+.swpx$”)

if not chang_name.match(event.pathname):

print event.pathname

handler = EventHandler()

notifier = pyinotify.Notifier(wm, handler)

wdd = wm.add_watch(‘/tmp’, mask, rec=True,auto_add=True )

notifier.loop()

代码里使用正则表达式过滤因为使用vim打开文件产生的缓存文件。也可以用exclude_filter方法

在官方文档中例子如下,但是我测试多次没有成功,所以直接用正则过滤。

# Exclude patterns from list

excl_lst = [‘^/etc/apache[2]?/’,

‘^/etc/rc.*’,

‘^/etc/hostname’,

&nb
sp;
‘^/etc/hosts’,

‘^/etc/(fs|m)tab’,

‘^/etc/cron..*’]

excl = pyinotify.ExcludeFilter(excl_lst)

# Add watches

res = wm.add_watch([‘/etc/hostname’, ‘/etc/cups’, ‘/etc/rc0.d’],

pyinotify.ALL_EVENTS, rec=True, exclude_filter=excl)

如果监控文件太多,需要对系统做一下修改sysctl -n -w fs.inotify.max_user_watches=16384

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个人观点

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/

5月 27

python连接oracle和mysql备忘

import cx_Oracle
    
def sql_comm(sql_run):
    db = cx_Oracle.connect(‘user’, ‘passwd’, ‘IP:port/sid’) 
    try:
        cursor=db.cursor()
    except cx_Oracle.ERROR,e:
        print “Error %d:%s”%(e.args[0],e.args[1])
    try:
           cursor.execute(sql_run)
    result_set=cursor.fetchall()
           cursor.close()
           db.commit()
           db.close()
    return result_set
    except e:
        print “Error %s”%(e.args[0])
        cursor.close()
        db.close()

python连接mysql

import MySQLdb

def sql_comm(sql_run):
        try:
                conn=MySQLdb.connect(host=host,user=username,passwd=pwd,db=database)
                cursor = conn.cursor()
        except MySQLdb.Error,e:
                print “Error %d:%s”%(e.args[0],e.args[1])
        try:
                cursor.execute(sql_run)
                result_set=cursor.fetchall()
                cursor.close()
                db.close()
                return result_set
        except MySQLdb.Error,e:
                print “Error %d:%s”%(e.args[0],e.args[1])
                cursor.close()
                db.close()

注意部分,在mysql修改数据时最好使用commit,如果只是select就不用了,不然得不到数据。
在oracle和mysql插入大量数据时,可以使用executemany,需要注意的是,oracle插入大量
数据时要将数据类型为元组,然后放到列表中。建议一次插入10000行。