Python常用脚本

1、图片转base64

# !/usr/local/python3.8/bin/python3
# -*- coding:UTF-8 -*-

import os
import base64

# 图片文件夹是/root/images
log_d = '/root/images'
logFiles = os.listdir(log_d)
# 只转图片格式
suffix = ("png", "jpg", "jpeg")
# 在/root/images内遍历文件
for filename in logFiles:
    print(filename)
    bool_file = filename.endswith(suffix)
    if bool_file:
        with open(filename, 'rb') as f:
            # image_base64 = base64.b64encode(f.read())  #bytes类型
            image = f.read()
            image_base64 = str(base64.b64encode(image), encoding='utf-8')  # 字符串类型
            print(image_base64)
    else:
        print('文件格式不匹配:{}'.format(filename))

2、解压zip文件

#!/usr/local/python3.8/bin/python3
# -*- coding:UTF-8 -*-
# pip install shutil
# https://docs.python.org/zh-cn/3/library/shutil.html

import zipfile, os
from werkzeug.utils import secure_filename

'''
基本格式:zipfile.ZipFile(filename[,mode[,compression[,allowZip64]]])
mode:可选 r,w,a 代表不同的打开文件的方式;r 只读;w 重写;a 添加
compression:指出这个 zipfile 用什么压缩方法,默认是 ZIP_STORED,另一种选择是 ZIP_DEFLATED;
allowZip64:bool型变量,当设置为True时可以创建大于 2G 的 zip 文件,默认值 True;

'''

def unzip(path, folder_abs):
    zip_file = zipfile.ZipFile(path)
    zip_list = zip_file.namelist()  # 得到压缩包里所有文件

    for f in zip_list:
        zip_file.extract(f, folder_abs)  # 循环解压文件到指定目录
    zip_file.close()  # 关闭文件,必须有,释放内存
    os.remove(path)

def savefile(f, savepath):
    basepath = os.path.dirname(__file__)
    upload_path = os.path.join(basepath, savepath, secure_filename(f.filename))
    f.save(upload_path)
    unzip(upload_path, savepath)

 3、打包文件zipfile,tarfile

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


# pip install zipfile38,tarfile不需要安装直接导入
import tarfile, zipfile


def zipDir(dirpath, outFullName):  # dirpath:是要打包的目录, outFullName:是压缩包名字
    zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
    for path, dirnames, filenames in os.walk(dirpath):
        fpath = path.replace(dirpath, '')
        for filename in filenames:
            zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
    zip.close()


def compress_file(dirpath, tarfilename):  # tarfilename是压缩包名字,dirname是要打包的目录
    if os.path.isfile(dirpath):
        with tarfile.open(tarfilename, 'w') as tar:
            tar.add(dirpath)
    else:
        with tarfile.open(tarfilename, 'w') as tar:
            #记录当前工作目录
            cur_path = os.getcwd()
            #切换工作目录
            os.chdir(dirpath)
            for root, dirs, files in os.walk('.'):
                for single_file in files:
                    # if single_file != tarfilename:
                    filepath = os.path.join(root, single_file)
                    tar.add(filepath)
            #切换回原来工作目录
            os.chdir(cur_path)


if __name__ == '__main__':
    zipDir('D:\\game\\build', 'abc.zip')
    compress_file('test.txt', 'test.tar.gz')
    compress_file('/tmp/test', 'test.tar.gz')

 4、python逐行读取文件

#!/usr/bin/python3.8
# -*- coding:UTF-8 -*-

# 使用fileinput模块
import fileinput
for line in fileinput.input("test.txt"):
    print(line, end='')

# 对一个文件对象使用for循环读每行数据
with open('test.txt','r',encoding='utf-8') as f:
    for line in f:
        print(line, end='')

# 使用readline()函数
with open('test.txt','r',encoding='utf-8') as f:
    line = f.readline()
    while line:
        print(line, end='')  # 在 Python 3中使用
        line = f.readline()

# 一次读取多行数据(f.readlines(2):一次读取两行数据)
with open('test.txt','r',encoding='utf-8') as f:
    tag=True
    while tag:
        lines = f.readlines(2)
        if lines:
            for line in lines:
                print(line, end='')
        else:
            tag=False 

5、读取文件指定行或最后几行

#!/usr/bin/python3.8
# -*- coding:UTF-8 -*-

import linecache


# 放入缓存防止内存过载
def get_line_count(filename):
    """
    :param filename: 文件名
    :return: 返回文件总的行数
    """
    with open(filename, 'r', encoding="utf-8") as f:
        count = 1
        while True:
            buffer = f.read(1024 * 1)
            if not buffer:
                break
            count += buffer.count('\n')
    return count


# 读取文件最后几行,方式一:使用seek()方法
def readfileline(lines, filepath, off=-50):
    """
    file.seek(offset[, whence])
    file:表示文件对象;
    whence:作为可选参数,用于指定文件指针要放置的位置,该参数的参数值有 3 个选择:0 代表文件头(默认值)、1 代表当前位置、2 代表文件尾。
    offset:表示相对于 whence 位置文件指针的偏移量,正数表示向后偏移,负数表示向前偏移。
            例如:
                当whence == 0 && offset == 3(即seek(3,0)),表示文件指针移动至距离文件开头处 3 个字符的位置;
                当whence == 1 && offset == 5(即seek(5,1)),表示文件指针向后移动,移动至距离当前位置5个字符处;
                当whence == 2 && offset == -5(即seek(-5,2)),表示文件指针向前移动,从文件末尾向前移动5个字符。
    :param lines: 要读取的行数
    :param filepath: 文件路径
    :param off: 读取的字符数
    :return:
    """
    linenumber = get_line_count(filepath)
    with open(filepath, 'rb') as f:
        while True:
            f.seek(off, 2)
            res = f.readlines()
            if len(res) > lines:
                for i in range(1, len(res)):
                    print(linenumber - lines + i, res[i].decode(), end="")
                break
            off += -50


# 读取文件最后几行,方式二:使用linecache模块
def readline(lines, filepath):
    linecache.clearcache()
    line_count = get_line_count(filepath)
    line_count = line_count - (lines - 1)
    for i in range(lines):
        last_line = linecache.getline(filepath, line_count)
        print(line_count, last_line, end="")
        line_count += 1


def readfile(start, end, filepath):
    """
    :param start: 开始行
    :param end: 结束行
    :param filepath: 文件路径
    :return:
    """
    linecache.clearcache()
    for i in range(start, end + 1):
        last_line = linecache.getline(filepath, i)
        print(i, last_line, end="")


if __name__ == '__main__':
    # 读取第10行到第20行
    readfile(10, 20, 'readtest.log')
    # 读取最后10行
    readline(10, 'readtest.log')
    # 读取最后20行
    readfileline(20, 'readtest.log')

 6、常用HTTP认证方式(示例代码为H3C SecPath F1000-AI-25防火墙API)

import requests

USER = 'admin'
PASSWD = 'xxxxxxx'

##########关闭requests https posts请求告警##########
requests.packages.urllib3.disable_warnings()

########## HTTP Basic Auth ###############
from requests.auth import HTTPBasicAuth

url_auth = 'https://x.x.x.x/api/v1/tokens'
http_Basic_token = requests.post(url_auth, auth=HTTPBasicAuth(USER, PASSWD), verify=False)
Basic_token_id = http_Basic_token.json().get('token-id')
headers = {"X-Auth-Token": Basic_token_id, "Content-type": "application/json", "Accept": "application/json"}
url_acl = 'https://x.x.x.x/api/v1/SecurityPolicies/GetRules'
Basic_acl = requests.get(url_acl, headers=headers, verify=False)
print(Basic_acl.json())

########## HTTP摘要式身份认证 ###############
from requests.auth import HTTPDigestAuth

url_auth = 'https://x.x.x.x/api/v1/tokens'
http_Digest_token = requests.post(url_auth, auth=HTTPDigestAuth(USER, PASSWD), verify=False)
http_Digest_token_token_id = http_Basic_token.json().get('token-id')
headers = {"X-Auth-Token": http_Digest_token_token_id, "Content-type": "application/json", "Accept": "application/json"}
url_acl = 'https://x.x.x.x/api/v1/SecurityPolicies/GetRules'
Digest_acl = requests.get(url_acl, headers=headers, verify=False)
print(Digest_acl.json())

########## 自定义 HTTP身份认证 ##########
# 当一个身份认证程序附加到一个请求时,在设置 request 期间就会调用该模块
# 因此,__call__()方法必须执行使身份认证有效所需的操作。
# 某些形式的身份认证还将添加钩子以提供进一步的功能。
class MyAuth(requests.auth.AuthBase):
    def __call__(self, r):
        # Implement my authentication
        return r
url_auth = 'https://x.x.x.x/api/v1/tokens'
requests.get(url_auth, auth=MyAuth(), verify=False)
# requests post请求发送中文报错
# UnicodeEncodeError: 'latin-1' codec can't encode characters in position 124-132: Body ('测试发送中文') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8
# 方法一:
data = '测试发送中文'
requests.post(url, data.encode('utf-8'))
# 方法二(不推荐):
data1 = '测试发送中文'  # str
data = data1.encode("utf-8").decode("latin1")
requests.post(url, data)

7、mac地址格式转换

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


def macConvert(mac, mode=1, lower=True):
    '''
    MAC 地址格式转换
        mac: 各种格式的mac地址
        mode: 1, 2, 3 三种格式
              mode=1 '10:F0:05:33:F4:BD'
              mode=2 '10-F0-05-33-F4-BD'
              mode=3 '10f0.0533.f4db'
        lower: 默认输出格式为小写
    '''
    tmp = []
    t = mac.replace('.', '').replace('-', '').replace(':', '')
    if lower is True:
        t = t.lower()
    else:
        t = t.upper()
    if mode == 1 or mode == 2:
        for i in range(0, len(t), 2):
            tmp.append(t[i:i + 2])
    else:
        for i in range(0, len(t), 4):
            tmp.append(t[i:i + 4])
    if mode == 1:
        return ':'.join(tmp)
    if mode == 2:
        return '-'.join(tmp)
    if mode == 3:
        return '.'.join(tmp)


mac1 = '10:F0:05:33:F4:BD'
mac2 = '10-F0-05-33-F4-BD'
mac3 = '10f0.0533.f4db'
mac4 = '10f00533f4db'
print(macConvert.__doc__)
print(macConvert(mac1))
print(macConvert(mac2, 3))
print(macConvert(mac3, 2, False))
print(macConvert(mac4, 1, False))
print(macConvert(mac3, 1))

 https://www.cnblogs.com/ningningqi/p/18085030      # python APScheduler任务调度库,相当于Linux定时任务

posted @ 2022-01-27 11:12  風£飛  阅读(175)  评论(0编辑  收藏  举报