首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python和firebase实时监控

Python和firebase实时监控
EN

Stack Overflow用户
提问于 2022-09-24 10:19:38
回答 1查看 57关注 0票数 0

请使用python监视您的防火墙数据库,以便在添加新元素时执行特定任务。喜欢经常检查数据库中是否有新的或传入的元素;检查是否向数据库中添加了一个新元素。

EN

回答 1

Stack Overflow用户

发布于 2022-10-09 15:10:33

您可以使用listenfirebase-admin方法来侦听对火基实时数据库的特定字段所做的更改。

以下是完整的示例:

代码语言:javascript
复制
# Importing required packages
import firebase_admin
from firebase_admin import credentials, db

# Initializing the database with URL
cred = credentials.Certificate('path/to/your/service.json')
firebase_admin.initialize_app(cred,{
    'databaseURL': 'https://YOUR-DATABASE-URL-NAME-HERE.firebaseio.com/'
}) 


def listener(event):
    print(event.event_type)  # Indicates the type of the event done
    print(event.path)  # References the path of the event
    print(event.data)  # Gives the UPDATED DATA, None if deleted

    # Now, executing specific functions based on the new data(UPDATED DATA)

    if str(event.data) == 'hello':
        # /// Execute your code here ///
        print('Data updated as ', str(event.data))

    elif str(event.data) == None:
        # /// Execute your code here ///
        print('Your data has been deleted!')

    # Follow these steps

db.reference('path/to/monitor').listen(listener) # It calls the above listener method(It continuosly listens for the data changes in your database)
# Everytime the data changes, listener function will be called
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73836443

复制
相关文章

相似问题

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