python几个包的使用

 一,pickle模块使用的数据格式是python专用的,并且不同版本不向后兼容,同时也不能被其他语言说识别。用于将数据方便地从文件或网络存取。示例如下:

#!/usr/bin/python

import pickle

def save_data(file, data):
        pickle.dump(data, open(file, 'w'))

def load_data(file):
        return pickle.load(open(file, 'r'))

if __name__ == '__main__':

        data = {}
        data[0] = "apple"
        data["name"] = "ciaos"

        print data

        save_data("save.dat", data)
        loaddata = load_data("save.dat")

        print loaddata

 执行效果如下:

 

root # python test.py
{0: 'apple', 'name': 'ciaos'}
{0: 'apple', 'name': 'ciaos'}

 

二,ConfigParser解析的配置文件的格式比较象ini的配置文件格式,就是文件中由多个section构成,每个section下又有多个配置项

root # cat test.conf
[db]
db_host=127.0.0.1
db_port=3306
db_user=root
db_pass=password

[concurrent]
thread=10
processor=20

 

#test.py

#!/usr/bin/python

import ConfigParser
import string, os, sys

cf = ConfigParser.ConfigParser()
cf.read("test.conf")
# return section
s = cf.sections()
print 'section:', s

o = cf.options("db")
print 'options:', o

v = cf.items("db")
print 'db:', v

print '-'*60
db_host = cf.get("db", "db_host")
db_port = cf.getint("db", "db_port")
db_user = cf.get("db", "db_user")
db_pass = cf.get("db", "db_pass")

threads = cf.getint("concurrent", "thread")
processors = cf.getint("concurrent", "processor")

print "db_host:", db_host
print "db_port:", db_port
print "db_user:", db_user
print "db_pass:", db_pass

print "thread:", threads
print "processor:", processors
cf.set("db", "db_pass", "zhaowei")
cf.write(open("test.conf", "w"))

 执行效果如下:

root # python test.py
section: ['db', 'concurrent']
options: ['db_host', 'db_port', 'db_user', 'db_pass']
db: [('db_host', '127.0.0.1'), ('db_port', '3306'), ('db_user', 'root'), ('db_pass', 'password')]
------------------------------------------------------------
db_host: 127.0.0.1
db_port: 3306
db_user: root
db_pass: password
thread: 10
processor: 20

 三,python提供了一个日志模块logging,它可以把我们想要的信息全部保存到一个日志文件中,方面我们查看

import logging

def get_logger(log_name):
        logger = logging.getLogger()
        formatter = logging.Formatter('%(pathname)s - %(asctime)s - %(name)s - %(levelname)s - %(message)s',)
        file_handler = logging.FileHandler(log_name)
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)
        logger.setLevel(logging.NOTSET)
        return logger

		
logger = get_logger("file.log")
		
logger.debug('debug')   
logger.info('info')   
logger.warning('warn')  
logger.error('error')
#
# root # cat test.log
# test.py - 2013-05-28 23:59:22,771 - root - ERROR - test one
# test.py - 2013-05-28 23:59:22,771 - root - ERROR - test two
#

  四,import pdb包用于代码调试,在需要设置断点的地方添加pdb.set_trace()

 参照http://blog.csdn.net/luckeryin/article/details/4477233

  五,python脚本中若包含中文字符,运行会出错,可以在源代码文件前面添加如下代码即可

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys

reload(sys)
sys.setdefaultencoding('utf8')

  六,post表单

import urllib
import urllib2

postdata = {"name":"test"}
f = urllib2.urlopen(
        url     = "http://127.0.0.1/post.php",
        data    = urllib.urlencode(postdata)
)
print f.read()

  七,利用urllib2实现文件下载

def downloader(filename, url):
        try:
                f = urllib2.urlopen(url,timeout=10800)
                with open(filename, "wb") as code:
                        code.write(f.read())
        except:
                logger.critical(sys.exc_info())
                logger.error("download error " + url)
                return 0

        logger.info("download ok " + url)
        return 1
#计算下载文件md5
def downloader(filename, url):
        try:
                m = hashlib.md5()  
                f = urllib2.urlopen(url,timeout=10800)
                m.update(f.read())   
                print m.hexdigest()
                time.sleep(2)
        except:
                print "error"
                return 0
 八,http(s)请求设置长连接
#coding=UTF-8
import httplib
import time
from time import clock
import ssl

def url_open(conn_obj):
    """httplib长连接"""

    uri="/test.file"
    try:
        conn_obj.request("GET", uri, "", {"Connection":"keep-alive"})
        #conn_obj.request("POST", "/func","param=0", {"Connection":"Keep-Alive"})
        response = conn_obj.getresponse()
        return response.read()
    except:
        return "error"

if __name__ == "__main__":
    start = time.time()
    #http request
    #conn = httplib.HTTPConnection("localhost:80")

    #https request
    conn = httplib.HTTPSConnection("localhost:443")
    for i in range(100):
        ret = url_open(conn)
    conn.close()
    end = time.time()
    print (end-start)
九, twisted
#!/usr/bin/python

from sys import argv
from pprint import pformat

#from twisted.internet.task import react
from twisted.internet import reactor
from twisted.web.client import Agent, readBody
from twisted.web.http_headers import Headers

total_times = 1000
times = 0

def cbRequest(response):
    print 'Response version:', response.version
    print 'Response code:', response.code
    print 'Response phrase:', response.phrase
    print 'Response headers:'
    print pformat(list(response.headers.getAllRawHeaders()))
    d = readBody(response)
    d.addCallback(cbBody)
    return d

def cbBody(body):
    #print 'Response body:'
    print body

def cbShutdown(ignored):
    global times
    times = times + 1
    if total_times - 1 < times:
    	reactor.stop()

def curl(url):
    agent = Agent(reactor)
    d = agent.request(
        'GET', url,
        Headers({'User-Agent': ['Twisted Web Client Example']}),
        None)
    d.addCallback(cbRequest)
    d.addBoth(cbShutdown)
    return d

if __name__ == '__main__':

    for i in range(total_times):
    	curl("http://web.kuaipan.cn/static/images/pc.png")
    
    reactor.run()
 

你可能感兴趣的:(python)