1.8.0-unstablegeth标志和--rpcapi "admin,eth,net,web3,debug,shh"运行--shhfrom web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))
print ("web3 =", web3.version.api) #4.2.1
print(web3.shh) #error occurs当我试图运行上面的代码时,我有以下错误:
Traceback (most recent call last):
File "dd.py", line 11, in <module>
print(web3.shh)
AttributeError: 'Web3' object has no attribute 'shh'问:如何修复此错误?
发布于 2018-06-25 09:15:48
web3.py==5.x.x
web3.geth.shh对象公开与shh_命名空间下的RPC交互的方法。
web3.py==4.x.x=>我们必须添加以下代码行以修复web3.py的错误:
from web3.shh import Shh
Shh.attach(web3, "shh")=>我们还必须为web3.js添加以下行。而不是使用shh。
var Shh = require('web3-shh');
var shh = new Shh(Shh.givenProvider || 'http://localhost:8545'); web3.py的下列解决方案:from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))
from web3.shh import Shh # Added line
Shh.attach(web3, "shh") # Added line
# Now we call web3.shh as we would like:
# Example
print(web3.shh.version) #5.0
kId = web3.shh.newKeyPair();
print(kId)
print(web3.shh.getPrivateKey(kId))
...web3.js的下列解决方案:$ npm install web3-shh示例代码:
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
if(!web3.isConnected()){console.log("notconnected"); process.exit();}
var Shh = require('web3-shh');
// "Shh.providers.givenProvider" will be set if in an Ethereum supported browser.
var shh = new Shh(Shh.givenProvider || 'http://localhost:8545');
var kId = shh.newSymKey().then(console.log); //returns a valid value.发布于 2018-06-25 09:11:34
https://ethereum.stackexchange.com/questions/51992
复制相似问题