web3.py return 来自格式奇怪的 get 函数

web3.py return from a get function in strange format

我正在开发 python 应用程序。这个应用程序只是为了从区块链中获取数据。在 web3.js 中一切正常,但我需要在 python 中进行(客户想要一个 python 应用程序)。这一切几乎都很好;该脚本完成了它需要做的事情,但是当调用 get 函数时,我得到了一个奇怪的输出(在 remix 上使用 get 函数或 web3.js whit 我写的 nodeJs api 工作得很好):

D:\pytoh\b_get\Scripts\python.exe C:/Users/Alessandro/PycharmProjects/pytoh/b_get/bget.py
True
[(b'\xe9:=\xd87/\x98\x00\xe4\x89\xe3\x8eb[c\x9cj\xc4\xa3\xa2b\x1d\x9a\xf0%\x1d\xdaB\xb7\xd9\xc4\xe3', 
b'\xe9:=\xd87/\x98\x00\xe4\x89\xe3\x8eb[c\x9cj\xc4\xa3\xa2b\x1d\x9a\xf0%\x1d\xdaB\xb7\xd9\xc4\xe3')]

Process finished with exit code 0

The output that i need is like this (i don't understand why the py output is like that):[0xe93a3dd8372f9800e489e38e625b639c6ac4a3a2621d9af0251dda42b7d9c4e3,0xe93a3dd8372f9800e489e38e625b639c6ac4a3a2621d9af0251dda42b7d9c4e3]

python脚本很简单(我还在写):

import json
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545'))
print(w3.isConnected())
with open("ABI.json") as f:
  info_json = json.load(f)
abi = info_json["output"]["abi"]

 address = "0x1305ef6377fe8cB7C6dD7Eb2B6cAD83A34fC7503"
 get = w3.eth.contract(address=address, abi=abi) 
 result=get.functions.getUser("0xe93a3dd8372f9800e489e38e
                       625b639c6ac4a3a2621d9af0251dda42b7d9c4e3").call()
 print(result)

这是客户写的智能合约:

pragma solidity ^0.4.26;
pragma experimental ABIEncoderV2;

contract crud{
  address owner;
  constructor() public{
  owner = msg.sender;
}
modifier onlyOwner () {
   require(msg.sender == owner);
    _;
}
struct user{
    bytes32 hash_json;
    bytes32 hash_corso;
}
 mapping (bytes32 => user[]) public users;

 
 function setUser(bytes32 _hash, bytes32 _hash_json, bytes32 _hash_corso) onlyOwner  public {
     user memory usr;
     usr.hash_json=_hash_json;
     usr.hash_corso= _hash_corso;
     users[_hash].push(usr);
 }
 
 function getUser(bytes32 _hash) view public returns (user[] memory) {
     return users[_hash];
 }

 }

感谢您的帮助!

结果是字节数组。当您打印它时,它会将控制字符转换为十六进制,但会打印可读字符。

要获得完整的十六进制字符串,试试这个:

result = bytearray([22,32,7,67,14,87,90])  # for testing
hxstr = "".join(["{:02x}".format(v) for v in result])
print("0x"+hxstr)

输出

0x162007430e575a