Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用生成器对b64编码数据中丢失的字符进行多处理-可能/如何?_Python 3.x_Generator_Python Multiprocessing - Fatal编程技术网

Python 3.x 使用生成器对b64编码数据中丢失的字符进行多处理-可能/如何?

Python 3.x 使用生成器对b64编码数据中丢失的字符进行多处理-可能/如何?,python-3.x,generator,python-multiprocessing,Python 3.x,Generator,Python Multiprocessing,处理作为b64编码字符串接收的数据。出于某种原因,前几天我收到了数据,其中一组b64编码字符串中有四个空格字符,使得解码输出不正确。我制作了一个小python脚本来强制执行完成任务的缺少的字符,但是我想看看是否可以通过多处理来提高速度。 我对使用生成器不太熟悉,所以这似乎让我在让它与多处理一起工作的过程中感到不舒服 下面的示例代码与我正在使用的代码不完全相同,但非常相似 导入base64 从maskprocessor将maskprocessor作为maskproc导入 从TQM导入TQM 导入字

处理作为b64编码字符串接收的数据。出于某种原因,前几天我收到了数据,其中一组b64编码字符串中有四个空格字符,使得解码输出不正确。我制作了一个小python脚本来强制执行完成任务的缺少的字符,但是我想看看是否可以通过多处理来提高速度。 我对使用生成器不太熟悉,所以这似乎让我在让它与多处理一起工作的过程中感到不舒服

下面的示例代码与我正在使用的代码不完全相同,但非常相似

导入base64
从maskprocessor将maskprocessor作为maskproc导入
从TQM导入TQM
导入字符串
将多处理作为mp导入
NCORE=8
KEY=b“一些解密密钥”
可打印字符=set(字节(string.printable,'ascii'))
def可打印(数据):
如果all(可打印字符中的字符表示数据中的字符)为False,则返回True
def解密(密码):
尝试:
plain=base64.b64解码(密码)#someDecryptAlgo(密钥,base64.b64解码(密码))
回归平原
除:
一无所获
def slow_生成器(密码):
def gen_to_队列(输入密码):
对于密码中的密码:
输入输出(密码)
对于范围内的(NCORE):
输入输出((无,无))
def过程(输入、输出):
尽管如此:
cipher=input_q.get()
如果密码为“无”:
输出输出(无)
打破
输出q.put((密码,解密(密码)))
input_q=mp.Queue(maxsize=NCORE*2)
输出_q=mp.Queue(maxsize=NCORE*2)
gen_pool=mp.pool(1,初始值设定项=gen_to_队列,initargs=(输入密码))
pool=mp.pool(NCORE,初始化器=process,initargs=(输入,输出))
完工工人=0
尽管如此:
密码,plain=output_q.get()
如果plain为None:
完工工人+=1
如果完工工人==n核心:
打破
其他:
普通密码
如果名称=“\uuuuu main\uuuuuuuu”:
actual_msg=“您所有的基地都属于我们”
实际数据=b'QWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz'#“您所有的基地都属于我们
收到的数据=b'QWxsI lvdXIgY FzZSBhcmUg mVsb25n HRvIHVz'
charset=“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/”
n_ws=接收到的数据。解码('utf-8')。计数('')
g=maskproc(接收到的字符数据,解码('utf-8')。替换('','1'),自定义字符集1=charset)
输出=慢速发电机(g)
对于密码,在zip中为纯文本(x[0],x[1]):
如果可打印(普通):
如果是普通解码('utf-8')==实际消息:
打印(f“找到正确的密码:{cipher}”)
打印(f“机密消息:{plain}”)
打破
解码后的b64数据使用静态密钥解密,因此检查是否找到正确字节的函数显然比上面的示例更复杂,但我认为该示例代表了我遇到的相同问题,使生成器能够进行多处理。不涉及读/写文件

我希望避免创建一个由
maskproc
生成器创建的所有候选对象的列表,但到目前为止,如果不这样做,我无法使
多处理
工作,因此我想知道是否有人得到了一些如何做的建议

编辑:假设我想并行运行
N
(4/8/16/etc)进程,是否可以创建一个长度
N
的列表,然后对候选对象进行处理,这样就不必创建长度为64^4的列表

Edit2:将代码更改为从中采用的代码

Edit3:我想我成功地让它工作了。现在,如果可能的话,我只想找到一种实现TQM进度条的方法

import base64
from maskprocessor import maskprocessor as maskproc
from tqdm import tqdm
import string
import multiprocessing as mp

NCORE = 8
KEY = b"some decryption key"
printable_chars = set(bytes(string.printable, 'ascii'))

def printable(data):
    return True if all(char in printable_chars for char in data) else False

def decrypt(cipher):
    try:
        plain = base64.b64decode(cipher) #someDecryptAlgo(KEY, base64.b64decode(cipher))
        return plain
    except:
        return None

def slow_generator(ciphers):
    def gen_to_queue(input_q, ciphers):
        for cipher in ciphers:
            input_q.put(cipher)
        for _ in range(NCORE):
            input_q.put(None)

    def process(input_q, output_q):
        while True:
            cipher = input_q.get()
            if cipher is None:
                output_q.put((None, None))
                break
            output_q.put((cipher, decrypt(cipher)))

    input_q = mp.Queue(maxsize=NCORE * 2)
    output_q = mp.Queue(maxsize=NCORE * 2)

    gen_pool = mp.Pool(1, initializer=gen_to_queue, initargs=(input_q, ciphers))
    pool = mp.Pool(NCORE, initializer=process, initargs=(input_q, output_q))

    finished_workers = 0
    while True:
        cipher, plain = output_q.get()
        if plain is None:
            finished_workers += 1
            if finished_workers == NCORE:
                break
        else:
            yield cipher, plain

if __name__ == "__main__":
    actual_msg = "All your base are belong to us"
    actual_data_b64 = b'QWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz' # "All your base are belong to us
    received_data_b64 = b'QWxsI lvdXIgY FzZSBhcmUg mVsb25n HRvIHVz'
    #charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"
    charset = "HmYI"
    n_ws = received_data_b64.decode('utf-8').count(' ')
    g = maskproc(received_data_b64.decode('utf-8').replace(' ', '?1'), custom_charset1=charset)
    output = slow_generator(g)
    for cipher, plain in output:
        if printable(plain):
            if plain.decode('utf-8') == actual_msg:
                print(f"Found correct cipher: {cipher}")
                print(f"Secret message: {plain}")
                break

你找到这个线程了吗?关于多重处理和生成器等等,有很多问题和答案。@MarekSchwarz我已经尝试将它应用到Astariul()的代码中但它不起作用,我很确定我在这里没有看到的是一个非常愚蠢的东西。我将原始帖子中的代码更新为我根据帖子中的建议尝试采用的代码。如果有人能指出我犯的任何愚蠢的错误,我会很高兴,真的很恼人。@MarekSchwarz现在开始工作了,至少在我运行了它。现在只要找到正确的密码,它就会停止。也许还可以尝试添加TQM进度条。