【问题标题】:Having Trouble Returning a value from a smart contract, using flask on python anywhere无法从智能合约中返回值,在任何地方使用 python 上的烧瓶
【发布时间】:2021-09-03 13:40:30
【问题描述】:

我在 pythonanywhere 上使用 flask 和 web3.eth 并连接到一个合约,但是在返回一个智能合约函数的值时遇到问题。没有记录错误。我已经放置了几个打印语句来查看应用程序在哪里停止,并发现它在我调用智能合约函数时停止。

另外我应该提一下,我可以在本地运行这个确切的代码而不会出现问题。

这是最有可能失败的函数:

def getDataFromTokenID(tokenid, contract):
    print('getting uri')
    uri = contract.functions.tokenURI(tokenid).call() # This is where it stops printing
    print('PRINT:',uri)
    html = requests.get(uri)
    name, img_url = html.json()['name'], html.json()['image']
    code = name[-5:]
    return name, img_url, code

上面的函数在如下蓝图中调用:

@TokenInfo.route('/rarity/<int:tokenid>', methods=['GET'])
def sendTokenInfo(tokenid):

    contract_address = ' ' # left empty for posting purposes
    w3 = Web3(Web3.WebsocketProvider(' ')) # left empty purposefully as well
    contract = w3.eth.contract(address=contract_address, abi=contract_abi.abi)

    model = Shape_classifier()
    model.load_state_dict(load(os.getcwd()+'/mysite/app/state_dict.pth'))

    uri = current_app.config['MONGO_URI']
    mongo.init_app(current_app, uri)
    gs = mongo.db.gantomstone_info

    try:
        id_exists = [{"$match": {'_id': tokenid}}, {"$count": "count"}]
        list(gs.aggregate(id_exists))[0]

    except:
        print('getting data from token id')
        name, img_url, serial = getDataFromTokenID(tokenid, contract) ## Stops printing here
        print('opening image')
        img = Image.open(requests.get(img_url, stream=True).raw)
        shape = getImageShape(img, model)
        colors = getColors(getCounts(img))
        rgb_count = getCounts(img)
        serialTF = getCodeInfo(serial)
        to_db = {'_id': tokenid, 'name': name, 'img_url': img_url, 'serial': serial,
                 'shape': shape, 'colors': colors, 'serialTF': serialTF, 'rgb_count': rgb_count}
        gs.insert_one(to_db)

    rarity = getRarity(gs, tokenid)
    gs.update_one({'_id': tokenid}, {
                  '$set': {'rarity_values': rarity}}, upsert=True)

    to_json = list(gs.find({'_id': tokenid}))[0]

    return jsonify(to_json)

我尝试移动合约地址(从 TokenInfo 视图函数和函数文件中)无济于事。我还尝试更改函数输入以接收 get 请求 args 而不是 URL 中的 int,这也没有什么区别。

如果有人知道如何让它工作,那将是惊人的,我将非常感激。

谢谢。

【问题讨论】:

    标签: python flask hosting pythonanywhere web3


    【解决方案1】:

    如果代码使用 websockets,它目前无法在 PythonAnywhere 上的 web 应用程序中运行。

    【讨论】:

      猜你喜欢
      • 2022-11-04
      • 2020-01-13
      • 2020-08-26
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多