Python常用功能模块速查

### 编码转换
# unquote('abc%20def') -> 'abc def'.
import urllib.parse
urllib.parse.unquote('abc%20def')

### 删除字典值为空的元素
dict(filter(lambda x: x[1] != '', dic.items()))
  • postgresql connect
import psycopg2
conn = psycopg2.connect(database='', user='', password=, host='', port=5432)
cur = conn.cursor()
# select
query = 'select * from public.test where id=%(id)s'
query_dict = {'id': id}
cur.execute(query,query_dict)
row = cur.fetchone()
# insert
  • pickle
    with open("test.txt", 'wb+') as f:
        pickle.dump(test.txt, f)

    with open("test.txt", 'rb+') as f:
        text = pickle.load(f)
  • 文件目录遍历
import os
    path = '/tmp'
    for dirpath, dirnames, filenames in os.walk(path):
        print(filenames)
        # 重命名
        os.rename(os.path.join(path,file),os.path.join(path,file))
  • PIL 显示图片流
from PIL import Image
import io
stream = io.BytesIO(pic.content)
img = Image.open(stream) # img = Image.open("pic.jpg")
img.show()
img.close()
  • socket5 proxy
# socket5 proxy:
#
import copy
import socks
import socket
defaultSocket = copy.copy(socket.socket) # proxy switch
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 1080)
# socket.socket = socks.socksocket
  • RE
import re
find_string = re.findall(r"<p>(.*?)</p>" ,text_string)
  • XML:lxml xpath
import lxml.etree as etree
element = etree.HTML(doc.text)
# 获取标签间的值
element.xpath('.//div@class="title"]/a/text()')
# 获取标签中的属性值, 例如一个链接
element_xpath = element.xpath('.//div[@name="title"]/a')
link = element_xpath.attrib.get('href')
  • Requests CookieJar Dict type conversion
cookies = requests.utils.dict_from_cookiejar(r.cookies)
cookies = requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)
s = requests.Session()
s.cookies = cookies
# 两个list 合并为一个字典
a = [1, 2, 3]
b = [a, b, c]
d = dict(zip(a, b))
# {1:a,2:b,3:c}
  • Chrome Cookie 字符串转字典
def chrome_cookie_to_dict(str):
    cookie = {}
    f = str.strip('Cookie:').strip('\n').split(';')
    for line in f:
        name, value = line.strip().split('=')
        cookie[name] = value
    return cookie
if __name__ == '__main__':
    str = 'Cookie:SESSION_COOKIE=3cab1829cea36ddbceb17f7e; JSESSIONID=E2EE5E944C1E82BE0B26613F3D9A9AAB'
    chrome_cookie_to_dict(str) # {'SESSION_COOKIE': '3cab1829cea36ddbceb17f7e', 'JSESSIONID': 'E2EE5E944C1E82BE0B26613F3D9A9AAB'}
  • OptionParser 命令行选项解析器, 应用场景:(命令行选项, 帮助说明)
from optparse import OptionParser, OptionError
usage = 'usage: %prog [options] xxx'
description = HandlerClass.__doc__
parser = OptionParser(usage=usage, description=description)
parser.add_option('-a', dest='address', help='host address', default='127.0.0.1')
parser.add_option('-p', dest='port', help='port number', default=8080)
try:
    (options, args) = parser.parse_args()
except OptionError:
    sys.exit(2)
except OptionError:
    print('No option input')
    sys.exit(2)
  • importlib模块动态加载 应用场景: 组件可插拔(fully pluggable)
import importlib
module = importlib.import_module(module_name)
  • HMAC password generator demo
import hashlib
import hmac
import base64

def hmac_password(message, salt, digestmod=hashlib.sha256):
    """
    digestmod:always_supported:'md5','sha1','sha224','sha256','sha384','sha512'.

    :param message:
    :param salt:
    :param digestmod:
    :return: hmac password
    """
    message = message.encode()
    salt = salt.encode()
    signature = base64.b64encode(hmac.new(salt, message, digestmod=digestmod)
                                 .digest())
    return signature

if __name__ == '__main__':
    m = hmac_password('secret info', 'random sring')
    print(m.decode())
  • Stmp SSL Mail.
import smtplib
import email.mime.multipart
import email.mime.text


from_mail = ''
from_mail_password = ''
to_mail = ''
mail_subject = 'test'
mail_content = '''
                    这是一封自动发送的邮件.
        '''
stmp_host = 'smtp.exmail.qq.com'
stmp_port = '465'
def SMTP_SSL():
    """
    Send mail.

    :return: (250, b'Ok')
    """
    msg = email.mime.multipart.MIMEMultipart()
    msg['from'] = from_mail
    msg['to'] = to_mail
    msg['subject'] = mail_subject
    content = mail_content
    txt = email.mime.text.MIMEText(content)
    msg.attach(txt)
    smtp = smtplib.SMTP_SSL(stmp_host, stmp_port)
    smtp.login(from_mail, from_mail_password)
    # smtp.set_debuglevel(1)
    smtp.sendmail(from_mail, to_mail, str(msg))
    return smtp.noop()

if __name__ == '__main__':
    print(SMTP_SSL())
发布时间:
2017-06-19 08:57
分类:
标签: