如何在if else语句中比较request.get结果

时间:2019-05-30 10:16:19

标签: python if-statement python-requests blockchain ethereum

我正在制作一个新脚本,该脚本将以太坊私钥作为输入列表,并生成带有余额的对应地址,并将余额与私钥和地址一起保存在文件中。

现在,我几乎可以确定我的问题是有条件的,但无法解决。

脚本步骤:  1.以私钥文件作为输入(-i标志)  2.将其转换为公共地址/对其进行解码  3.触发对Etherscan的API调用以获取有关地址的信息  4.如果在API调用中json()[“ result”]> 0,则将其写入输出文件(-o标志),否则打印出并休眠1秒钟

enter image description here

任何人都可以对我犯错的地方提出警告吗?

我的代码:

#!/usr/bin/python
import sys, os, argparse, requests, ethereum, binascii, time
from multiprocessing import Pool

def scanether(balance):
    try:
        # Convert private key to address and print the result
        eth_address = ethereum.utils.privtoaddr(INPUTFILE)
        eth_address_hex = binascii.hexlify(eth_address).decode("utf-8")
        eth_balance = requests.get("https://api.etherscan.io/api?module=account&action=balance&address=0x" + eth_address_hex + "&tag=latest&apikey=APIKEYHERE").json()["result"]

        # Check if the result is > 0
        if ('result' != 0) in r.eth_balance: 
            print("[*] Address with balance found: " + eth_address_hex + priv)
            # Write match to OUTPUTFILE
            fHandle = open(OUTPUTFILE,'a')
            fHandle.write(eth_address_hex + privkey + "\n")
            fHandle.close()
        else:
            print("balance: {} address: 0x{} privkey: {}".format(float(eth_balance)/100000000, eth_address_hex, priv))
            time.sleep(1)


    except Exception as e:
        return

if __name__ == '__main__':
    print("""
# Finding the Ethereum address with balance
        """)
    # Parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-i', '--inputfile', default='input.txt', help='input file')
    parser.add_argument('-o', '--outputfile', default='output.txt', help='output file')
    parser.add_argument('-t', '--threads', default=200, help='threads')
    args = parser.parse_args()

    INPUTFILE=args.inputfile
    OUTPUTFILE=args.outputfile
    MAXPROCESSES=int(args.threads)

    try:
        addresses = open(INPUTFILE, "r").readlines()
    except FileNotFoundError as e:
        print(e)
        exit(e.errno)

    print("Scan in progress...")
    pool = Pool(processes=MAXPROCESSES)
    pool.map(scanether, addresses)
    print("Scan finished.")

输出如下: enter image description here

2 个答案:

答案 0 :(得分:1)

问题是您正在使用一些不在函数范围内的变量:

def scanether(balance):
    try:
        # Convert private key to address and print the result
        eth_address = ethereum.utils.privtoaddr(INPUTFILE)
        ...

        # Check if the result is > 0
        if ('result' != 0) in r.eth_balance: 
            print("[*] Address with balance found: " + eth_address_hex + priv)
            # Write match to OUTPUTFILE
            fHandle = open(OUTPUTFILE,'a')
            ...

    except Exception as e:
        return

这里INPUTFILEOUTPUTFILE不在范围内,它将引发捕获的异常,然后函数仅返回...

您需要将它们作为参数传递:

def scanether(balance, INPUTFILE, OUTPUTFILE):
    ...


...

    print("Scan in progress...")
    pool = Pool(processes=MAXPROCESSES)
    def scanether_wrapper(address, ifile=INPUTFILE, ofile=OUTPUTFILE):
        return scanether(address, ifile, ofile)
    pool.map(scanether_wrapper, addresses)
    print("Scan finished.")

答案 1 :(得分:0)

您可以使用以下代码作为参考。在处理响应之前,应确保所发出的请求是否成功。

请参考以下代码以供进一步参考。

length

在处理响应之前,请始终检查状态码是否为200。

谢谢!