简体   繁体   English

web3.py 从获取 function 以奇怪的格式返回

[英]web3.py return from a get function in strange format

i'm developing a python app.我正在开发一个 python 应用程序。 This app is just to get data from the blockchain.这个应用程序只是为了从区块链中获取数据。 In web3.js it all works good, but i need to do it in python (the client wants a python app).在 web3.js 中一切正常,但我需要在 python 中进行(客户想要一个 python 应用程序)。 It all works almost good;这一切几乎都很好; The script does what it needs to do, but when calling the get function i get a strange output (using the get function on remix or web3.js whit a nodeJs api that i wrote works just perfect): The script does what it needs to do, but when calling the get function i get a strange output (using the get function on remix or web3.js whit a nodeJs api that i wrote works just perfect):

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] The output that i need is like this (i don't understand why the py output is like that):[0xe93a3dd8372f9800e489e38e625b639c6ac4a3a2621d9af0251dda42b7d9c4e3,0xe93a3dd8372f9800e489e38e625b639c6ac4a3a2621d9af0251dda42b7d9c4e3]

The python script is simple (i'm still writing it): 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)

And this is the smart contract that the client wrote:这是客户写的智能合约:

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];
 }

 }

Thank for your help!感谢您的帮助!

The result is a byte array.结果是一个字节数组。 When you print it, it converts control characters to hex but prints readable characters.当您打印它时,它将控制字符转换为十六进制,但打印可读字符。

To get a full hex string, try this:要获得完整的十六进制字符串,请尝试以下操作:

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

Output Output

0x162007430e575a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM