随笔分类 -  python

摘要:$ gcc -shared -Wl,-soname,testlib -o testlib.so -fPIC testlib.c如果在Mac OS X ,则$ gcc -shared -Wl,-install_name,testlib.so -o testlib.so -fPIC testlib.ci... 阅读全文
posted @ 2014-12-29 17:24 李伯波 阅读(415) 评论(0) 推荐(0)
摘要:python中自带了处理python的模块,使用时候直接import json即可。使用loads方法即可将json字符串转换成python对象,对应关系如下: JSON Python object dict array list string unicode num... 阅读全文
posted @ 2014-09-01 09:05 李伯波 阅读(3460) 评论(0) 推荐(0)
摘要:print"ID of last record is ", int(cursor.lastrowid) #最后插入行的主键IDprint"ID of inserted record is ", int(conn.insert_id()) #最新插入行的主键ID,conn.insert_id()一定要... 阅读全文
posted @ 2014-05-10 11:39 李伯波 阅读(221) 评论(0) 推荐(0)
摘要:gcc -fPIC -shared bob_hash.c -o bob_hash.so //把bob_hash.c文件编译成动态库函数定义: int hash_string(void *tmpstr)python: >>> from ctypes import CDLL,c_int,c_void_p>>> bob_hash = CDLL('/home/gby/workspace/bob_hash.so')>>> hash_string = bob_hash.hash_string>>> hash_strin 阅读全文
posted @ 2013-08-03 11:49 李伯波 阅读(478) 评论(0) 推荐(0)
摘要:for item in msg: print "%X,"%ord(item) 阅读全文
posted @ 2013-07-10 13:16 李伯波 阅读(1468) 评论(0) 推荐(0)
摘要:参考:http://www.cnblogs.com/fxjwind/archive/2011/07/05/2098636.html http://amazingjxq.com/2012/01/09/python%E8%B0%83%E7%94%A8c%E5%87%BD%E6%95%B0/>>>cd /home/jxq/code/gcc -fPIC -shared bob_hash.c -o bob_hash.so然后在python里面用ctypes加载刚才的动态库。argtypes和restype分别对应函数的参数和返回值的类型。这样就可以直接调用了。 >>> 阅读全文
posted @ 2013-07-08 18:36 李伯波 阅读(359) 评论(0) 推荐(0)
摘要:1、encode,decode:用来编码和 解码文件的,也可以对StringIO里的数据做编解码2、encodestring,decodestring:用来编码和解码字符串3、b64encode和b64decode:用来编码和解码字符串,并且有一个替换符号字符的功能 阅读全文
posted @ 2013-07-06 16:55 李伯波 阅读(171) 评论(0) 推荐(0)
摘要:一个简单的例子:from pyDes import *data = "Please encrypt my data"k = des("DESCRYPT", CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)d = k.encrypt(data)print "Encrypted: %r" % dprint "Decrypted: %r" % k.decrypt(d)assert k.decrypt(d, padmode=PAD_PKCS5) 阅读全文
posted @ 2013-07-06 16:44 李伯波 阅读(1224) 评论(0) 推荐(0)
摘要:最简方法: apt-get install python-mysqldb源代码方法: 然后解压,打开README:里面有安装过程: $ tar xfz MySQL-python-1.2.1.tar.gz $ cd MySQL-python-1.2.1 $ # edit site.cfg if nec... 阅读全文
posted @ 2013-07-01 15:24 李伯波 阅读(749) 评论(0) 推荐(0)
摘要:import threadingimport threadclass userProcess(threading.Thread): def __init__(self,data): threading.Thread.__init__(self) self.data = data def run(self): passif __name__ == '__main__': data=['ggg'] processThread = userProcess(data) processThread.start() 阅读全文
posted @ 2012-11-21 16:52 李伯波 阅读(114) 评论(0) 推荐(0)
摘要:进程可以通过调用multiprocessing的Process进行创建,下面代码创建两个进程。[root@localhost ~]# cat twoproces.py #!/usr/bin/env pythonfrom multiprocessing import Processimport osdef output():print "My pid is :%d\n" % os.getpid()print "My parent is:%d\n" % os.getppid()def main():p=Process(target=output)p.star 阅读全文
posted @ 2012-11-21 16:49 李伯波 阅读(265) 评论(0) 推荐(0)
摘要:import sys, socketopt = raw_input('1:through IP find domain name\n2:through domain name')while opt != '2' and opt != '1': print "error\n" opt = raw_input('1:through IP find domain name\n2:through domain name') if opt == '1': addr = raw_input('IP 阅读全文
posted @ 2012-11-21 15:49 李伯波 阅读(579) 评论(0) 推荐(0)
摘要:方法一:为了便于代码管理,将全局变量统一放到一个模块中,然后在使用全局变量的时候,导入全局变量模块,通过这种方法来进行使用全局变量;在一个模块中定义全局变量:#global.pyGLOBAL_1=1GLOBAL_2=2GLOBAL_3='HelloWorld'然后在一个模块中导入全局变量定义模块,在新模块中使用全局变量:importglobalValues defprintGlobal():print(globalValues.GLOBAL_1)print(globalValues.GLOBAL_3)globalValues.GLOBAL_2+=1#modifyvaluesif 阅读全文
posted @ 2012-11-21 11:35 李伯波 阅读(245) 评论(0) 推荐(0)