下午好。我现在正在努力实现“状态”。我使用图书馆Web3.shh。我发送了这条消息,并给出了“真”的答案,但我不能接收到其他地址上的所有信息。在Status的文件和文件的“状态”中,我没有找到任何答案。告诉我怎么才能正确地从盖斯那里提取信息?下面是代码:
from web3 import shh
from web3 import HTTPProvider, Web3
status_host = 'https://host:port''
privatekey = '0x....7f2afa922ffdbeac65867f544a....'
connect = Web3(HTTPProvider(status_host))
print('connect status ===> ', connect.isConnected())
ms = shh.Shh(connect)
print('info ===>>>> ', ms.info)
id = ms.addPrivateKey(key=privatekey)
print('id ===>>>> ', id)
user_address = ms.getPublicKey(id)
print('user_address ===>>> ', user_address)
privkey = ms.getPrivateKey(id)
print("privkey => ", privkey)
topic = Web3.toHex(b'AS')
print("topic => ", topic)
text = 'test message'
address_to = '0x0487be55c072702a0e4da72158a7432281e8c26aca9501cd0bfeea726dc85f2611e96884e8fc4807c95c04c04af3387b83350a27cc18b96c37543e0f9a41ae47b5'
mes_send = ms.post(
{
'ttl': 20,
'payload': Web3.toHex(text=text),
'pubKey': address_to,
'topic': topic,
'powTarget': 2.5,
'powTime': 2,
}
)
if mes_send == True:
print('Status message => Send')
else:
print('Message not send')发布于 2018-05-21 07:17:25
我找到了解决办法。在正式文档'WEB3.PY -> SHH‘中,除了指出过滤器应该在并行进程中启动之外,一切都描述得很好。那些。必须将文件发送和接收到两个文件中,+在无限循环中运行该文件,并以一定频率侦听过滤器。代码本身会告诉所有人))
from web3 import shh
from web3 import HTTPProvider, Web3
status_host = 'http://****:9090'
privatekey = '0x5eea64de8906369612593480b7f2a...'
connect = Web3(HTTPProvider(status_host))
print('connect status ===> ', connect.isConnected())
sh_con = shh.Shh(connect)
print('info ===>>>> ', sh_con.info)
id = sh_con.addPrivateKey(key=privatekey)
print('id ===>>>> ', id)
user_pubkey = sh_con.getPublicKey(id)
print('user_pubkey ===>>> ', user_pubkey)
topic = Web3.toHex(b'ESSC')
print("topic => ", topic)
text = b'First test'
sumkey = sh_con.addSymKey(privatekey)
print('sumkey ====>>>> ', sumkey)
address_to = '0x043c03b3b0a0cd00...'
mes_send = sh_con.post(
{
'ttl': 40,
'powTarget': 11,
'powTime': 5,
'payload': Web3.toHex(text),
'topic': topic,
'pubKey': address_to,
'sig': user_pubkey,
}
)
if mes_send:
print('Message Send')
else:
print('Message not send')文件筛选器->:
class Daemon:
def __init__(self):
self.private_key = '0x36...'
host = 'http://****:9090'
self.new_id = '13b994193505744d...'
self.connection = shh.Shh(Web3(HTTPProvider(host)))
def ms_filter(self):
message_filter = self.connection.newMessageFilter({'privateKeyID': self.new_id})
return message_filter
def in_daemon(self):
filter_id = self._create_message_filter().filter_id
while True:
messages = self.connection.getMessages(filter_id)
if messages != []:
message = messages[0]
mes_pars = message['payload']
mes_from = message['recipientPublicKey']
topic = message['topic']
timestamp = message['timestamp']
print('from user => {}'.format(Web3.toHex(mes_from)))
print('message => {}'.format(Web3.toText(mes_pars)))
print('topic => {}'.format(Web3.toText(topic)))
print('timestamp => {}'.format(timestamp))
time.sleep(0.3)
threads = []
daemon = Thread(target=Daemon().in_daemon())
threads.append(daemon)
daemon.start()https://ethereum.stackexchange.com/questions/48667
复制相似问题