Python

清华源

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple model

linux中python2和3的pip
python{2,3} -m pip install xxx

Base64 #

import base64
import string
base64.b64decode() #解码
base64.b64encode() #编码

处理变表base64

str1 = "d2G0ZjLwHjS7DmOzZAY0X2lzX3CoZV9zdNOydO9vZl9yZXZlcnGlfD==" #解码字符串

string1 = "TSRQPONMLKJIHGFEDCBAUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" #变换后的码表
string2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

print(base64.b64decode(str1.translate(str.maketrans(string1,string2))))

正则 #

取AB中间字符

(?<=A).*?(?=B)   #不包含AB
A.*?B            #包含AB
A.*?(?=B)        #包含A不包含B
import re
re.findall(r"(?<=A).*?(?=B)",str)

n位字符取一次

re.findall(r'.{2}',str)

requests #

proxies={'https':'127.0.0.1:8080','http':'127.0.0.1:8080'}

线程池 #

from concurrent.futures import ThreadPoolExecutor
def test(x):
    pass
with ThreadPoolExecutor(max_workers=5) as t:
    for x in range(10):
        t.submit(test,x)

hash #

import hashlib
m = hashlib.md5()
m.update(b'123')
m.hexdigest()

hashlib.md5(b'123').hexdigest()

hashlib.new('md5', b'123').hexdigest()

指定md5结尾

import hashlib
i=0
while 1:
    i+=1
    if hashlib.md5(str(i).encode()).hexdigest()[-6:]=='1cf45b':
        print(i)
        break

try #

捕获所有异常

try:
    print(num)
except Exception as e:
    print('产生错误了',e)

JSON #

import json
json.loads(str)
josn.dumps(json)

time #

from datetime import datetime

time='Tue, 06 Sep 2022 03:26:46 GMT'

dd='r'%a, %d %b %Y %H:%M:%S GMT'
print(date.strptime(dd,time))

ouput:
20200906112646
然后在小时上加8

xpath #

from lxml import etree

e=etree.HTML(res)

u=e.xpath(path)

execjs #

import execjs

with open('js1.js','r',encoding='utf-8') as fp:
    js1=fp.read()
print(execjs.compile(js1).call('e','cat'))

# e为函数名,cat为传参

Crypto #

AES ECB pkcs7

from Crypto.Cipher import AES
import base64
from Crypto.Util.Padding import pad, unpad
MODE = AES.MODE_ECB
PAD_STYLE = 'pkcs7'
ENCODING = 'UTF-8'
key='l8N2iooyp07M9IWa'
def encrypt(plaintext: str, key: str) -> str:
    key_bytes = key.encode(ENCODING)
    cipher = AES.new(key_bytes, MODE)
    plaintext_bytes = plaintext.encode(ENCODING)
    plaintext_bytes_padded = pad(plaintext_bytes, AES.block_size, PAD_STYLE)
    ciphertext_bytes = cipher.encrypt(plaintext_bytes_padded)
    ciphertext_base64_bytes = base64.b64encode(ciphertext_bytes) 
    ciphertext = ciphertext_base64_bytes.decode(ENCODING)
    return ciphertext
def decrypt(ciphertext: str, key: str) -> str:
    key_bytes = key.encode(ENCODING)
    decrypter = AES.new(key_bytes, MODE)
    ciphertext_base64_bytes = ciphertext.encode(ENCODING)
    ciphertext_bytes = base64.b64decode(ciphertext_base64_bytes)
    plaintext_bytes_padded = decrypter.decrypt(ciphertext_bytes)
    plaintext_bytes = unpad(plaintext_bytes_padded, AES.block_size, PAD_STYLE)
    plaintext = plaintext_bytes.decode(ENCODING)
    return plaintext
from Crypto.Cipher import DES,AES,PKCS1_v1_5
from Crypto.PublicKey import RSA
import base64
def add(text,L=16):
    l=len(text)
    if l<L:
        text=text+'\0'*(L-l)
    elif l>L:
        text=text+'\0'*(L-l%L)
    return text.encode()
def AES_encrypt(clear,key,iv):
    aes=AES.new(add(key),2,add(iv))
    return base64.b64encode(aes.encrypt(add(clear))).decode()
def AES_decrypt(cipher,key,iv):
    aes=AES.new(add(key),2,add(iv))
    return aes.decrypt(base64.b64decode(cipher.encode())).decode().replace('\0','')
def DES_encrypt(clear,key):
    des=DES.new(add(key,8),DES.MODE_ECB)
    return base64.b64encode(des.encrypt(add(clear,8))).decode()

def RSA_encrypt(clear):
# 公钥加密,只能用对应的私钥解密,反之
    rsa_key = RSA.import_key(open("key.pem").read())
    cipher = PKCS1_v1_5.new(rsa_key)
    return base64.b64encode(cipher.encrypt(clear.encode('utf-8'))).decode()
def RSA_encrypt():
    pass
print(RSA_encrypt('3jtdV+uOwJU='))

RC4

import base64
from urllib.parse import quote
def rc4_main(key = "init_key", message = "init_message"):
    # print("RC4加密主函数")
    s_box = rc4_init_sbox(key)
    crypt = str(rc4_excrypt(message, s_box))
    return  crypt
def rc4_init_sbox(key):
    s_box = list(range(256))  
    # print("原来的 s 盒:%s" % s_box)
    j = 0
    for i in range(256):
        j = (j + s_box[i] + ord(key[i % len(key)])) % 256
        s_box[i], s_box[j] = s_box[j], s_box[i]
    # print("混乱后的 s 盒:%s"% s_box)
    return s_box
def rc4_excrypt(plain, box):
    # print("调用加密程序成功。")
    res = []
    i = j = 0
    for s in plain:
        i = (i + 1) % 256
        j = (j + box[i]) % 256
        box[i], box[j] = box[j], box[i]
        t = (box[i] + box[j]) % 256
        k = box[t]
        res.append(chr(ord(s) ^ k))
    cipher = "".join(res)
    print("加密后的字符串是:" ,quote(cipher))
    return (str(base64.b64encode(cipher.encode('utf-8')), 'utf-8'))

pyqt打包 #

pyinstaller