python数据类型之字符串补充

1.如何随机生成验证码,快速生成内推码

import random   
import string   ###导入字符串模块
code_str = string.ascii_letters + string.digits  ###大小写所有字母和所有数字
print(code_str)
def gen_code(len=4):      
    return "".join(random.sample(code_str, len))   ###返回的是一个从code_str随机选len个字符组成的字符串
print([gen_code(len=6) for i in range(100)])   ###列表生成式,结果
print({gen_code(len=6) for j in range(100)})    ###但当数据太大时,必定会重复,所以这里用集和可以避免

这里写图片描述

2.凯撒加密—加密算法

在密码学中,恺撒密码是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,
明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。

import string
def kaisacrypt(text='hello', k=3):
    lower = string.ascii_lowercase[k:] + string.ascii_lowercase[:k]  # 对原有小写字母向右移动k位
    upper = string.ascii_uppercase[k:] + string.ascii_uppercase[:k]
    table = str.maketrans(string.ascii_letters, lower+upper)    # 用于创建字符串映射的转换表'hello'
    return text.translate(table)      # 根据转换表去转换对应的字符
print(kaisacrypt())

这里写图片描述

3.暴力破解

思路:
测试文本中是否存在至少两个最常见的英文单词, 如果有, 则代表破解成功.

import string
def kaisacrypt(text='hello', k=3):
    lower = string.ascii_lowercase[k:] + string.ascii_lowercase[:k]  # 对原有小写字母向右移动k位
    upper = string.ascii_uppercase[k:] + string.ascii_uppercase[:k]
    table = str.maketrans(string.ascii_letters, lower+upper)    # 用于创建字符串映射的转换表'hello'
    return text.translate(table)      # 根据转换表去转换对应的字符
print(kaisacrypt())

def check(text):

    mostCommonWords = ('the', 'is', 'to', 'not', 'have', 'than', 'for', 'ok', 'and' )
    return  len([1 for word in mostCommonWords  if word in text])>2

def bruteForce(text):
    for i in range(26):
        t = kaisacrypt(text, -i)
        if check(t):
            print(i)
            print(t)
            break
    text = "If not to the sun for smiling, warm is still" \
       " in the sun there, but wewill laugh more confident calm; " \
       "if turned to found his own shadow, appropriate escape, " \
       "the sun will be through the heart,warm each place behind the corner; " \
       "if an outstretched palm cannot fall butterfly, then clenched waving arms, " \
       "given power; if I can't have bright smile, it will face to the sunshine, " \
       "and sunshine smile together, in full bloom."
cryptStr = kaisacrypt(text=text, k=18)
print(cryptStr)

bruteForce(cryptStr)

这里写图片描述
4.基于缓冲区的生产-消费者模型

import random
import time

cacheList = []   # 缓冲区列表, 存放生产的所有包子,工作方式是队列的工作方式(FIFO-first in firest out).
cacheListLen = 5   # 如果缓冲区列表的长度为5, 缓冲区就满了, 生产者不能再生产了.

def isfull():
    return  len(cacheList) == 5   # 缓冲区满了

def consumer(name):    #消费者
    print("%s准备买包子......." %(name))
    while True:
        kind  = yield
        print("%s购买%s包子成功..." %(name, kind))

def producer(name):
    print("%s厨师正在生产包子......." %(name))
    kinds = ['A', 'B', 'C', 'D']
    while True:
        if  not isfull():
            time.sleep(random.random())   # 模拟生产数据耗费的时间
            kind = random.choice(kinds)
            print("%s已经生产好%s包子了..." %(name, kind))
            cacheList.append(kind)
        else:
            print("已经有足够的包子")
            yield

p = producer("hello")
(next(p))
consumers = [consumer('user'+str(i)) for i in range(10)]
for i in consumers:
    if not cacheList:
        print("目前没有包子")
    else:
        if not isfull():   # 如果缓冲区没有满, 就让生产者继续生产包子, 丛上次停止的地方继续执行.
            next(p)
        kind = cacheList.pop()  # 从缓冲区拿出包子给消费者.
        next(i)
        i.send(kind)

这里写图片描述

猜你喜欢

转载自blog.csdn.net/forever_wen/article/details/81838486