如何修复python int太大而无法转换为c long

时间:2019-01-20 11:38:22

标签: python

我正在使用Windows 10为我的DVR生成安全密码,但我忘记了密码。

在笔记本上安装了Python 3.7.2

RAM:16GB 硬盘:1TB

#!/usr/bin/env python3

import sys
from re import search
from numpy import uint32
from requests import get
from datetime import datetime

def keygen(seed):
    magic = 0
    for i, char in enumerate(seed):
        i += 1
        magic += i * ord(char) ^ i
    secret = str(uint32(1751873395 * magic))

    c = str.maketrans("012345678", "QRSqrdeyz")
    return secret.translate(c)

def get_serial_date(ip):
    try:
        req = get("http://192.168.178.43/upnpdevicedesc.xml")
    except Exception as e:
        print("Unable to connect to {ip}:\n{e}")
        sys.exit(-1)

    model = search("<modelNumber>(.*)</modelNumber>", req.text).group(1)
    serial = search("<serialNumber>(.*)</serialNumber>", req.text).group(1)
    serial = serial.replace(model, "")
    datef = datetime.strptime(req.headers["Date"], "%a, %d %b %Y %H:%M:%S GMT")
    date = datef.strftime("%Y%m%d")

    return "{serial}{date}"

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: {sys.argv[0]} <ip>")
        print("Connects to a   device and generates a security key")
        sys.exit(1)
    seed = get_serial_date(sys.argv[1])
    print("Got seed: {seed}")
    key = keygen(seed)
    print("Generated security key: {key}")

是否可以解决此错误代码,或者我需要对我的笔记本电脑设置进行某些处理?

这是我得到的错误代码

Got seed: {seed}
Traceback (most recent call last):
  File "beer.py", line 41, in <module>
    key = keygen(seed)
  File "beer.py", line 14, in keygen
    secret = str(uint32(1751873395 * magic))
OverflowError: Python int too large to convert to C long

1 个答案:

答案 0 :(得分:0)

如果您使用uint32将数字减少到0 … 2**32-1范围内,则可以改用以下方法:

secret = str((1751873395 * magic) & 0xffffffff)

还请注意,在循环中结合使用enumeratei += 1会造成混乱。