首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python从web3.shh获取消息

如何使用Python从web3.shh获取消息
EN

Ethereum用户
提问于 2018-05-16 16:21:48
回答 1查看 644关注 0票数 3

下午好。我现在正在努力实现“状态”。我使用图书馆Web3.shh。我发送了这条消息,并给出了“真”的答案,但我不能接收到其他地址上的所有信息。在Status的文件和文件的“状态”中,我没有找到任何答案。告诉我怎么才能正确地从盖斯那里提取信息?下面是代码:

代码语言:javascript
复制
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')
EN

回答 1

Ethereum用户

发布于 2018-05-21 07:17:25

我找到了解决办法。在正式文档'WEB3.PY -> SHH‘中,除了指出过滤器应该在并行进程中启动之外,一切都描述得很好。那些。必须将文件发送和接收到两个文件中,+在无限循环中运行该文件,并以一定频率侦听过滤器。代码本身会告诉所有人))

代码语言:javascript
复制
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')

文件筛选器->:

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/48667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档