【问题标题】:How to Retrieve Data from Firebase using Python如何使用 Python 从 Firebase 中检索数据
【发布时间】:2016-07-31 09:33:31
【问题描述】:

我是 Python 新手,我想使用 Python 连接到 Firebase。我可以使用 put()patch() 成功添加和修改 Firebase,但我找不到从 Firebase 检索数据的方法。

代码:

import serial
import time
import requests
import json

firebase_url = 'https://testing1639.firebaseio.com'

#Connect to Serial Port for communication
ser = serial.Serial('/dev/ttyUSB0', 9600)

#Setup a loop to send temperature values at fixed intervals in seconds
fixed_interval = 2

while 1:
    try:
         #Temperature value obtained from Arduino + LM35 Temp Sensor
         temperature_c = ser.readline()
         #Current time and date
         time_hhmmss = time.strftime('%H:%M:%S')
         date_mmddyyyy = time.strftime('%d/%m/%Y')

         #Current location name
         temperature_location = 'Mumbai-Kandivali' ;

         print temperature_c + ',' + time_hhmmss + ',' + date_mmddyyyy + ',' + temperature_location

         #Insert record
         data = {'date':date_mmddyyyy,'time':time_hhmmss,'value':temperature_c}

         result = requests.post(firebase_url + '/' + temperature_location + '/temperature.json', data=json.dumps(data))

         #Insert record
         print 'Record inserted. Result Code = ' + str(result.status_code) + ',' + result.text
         time.sleep(fixed_interval)

    except IOError:
        print('Error! Something went wrong.')
        time.sleep(fixed_interval)

如何修改它以检索数据?

【问题讨论】:

  • 有几个常用的库。请参阅 ozgur.github.io/python-firebasegithub.com/mikexstudios/python-firebase。两者都在这里提到firebase.com/docs/rest/quickstart.html。如果您对他们有疑问,请发布您尝试过的内容,我们可以提供更好的帮助。
  • 我会试试看。非常感谢您的回复
  • 嗨,你有没有让你的 python 脚本在没有库的情况下工作?这些库的问题是它们已经过时了。现在,firebase 技术人员表示他们对那个特定的库有疑问。所以我认为直接从python访问他们的rest api会更安全。

标签: python get firebase python-requests patch


【解决方案1】:

我有类似的东西正在工作

firebase_url = 'https://xyz.firebaseio.com/'

client_name = '孟买-坎迪瓦利'

类 MainPage(webapp.RequestHandler):

def get(self):
    self.response.headers['Content-Type'] = 'application/json'     
    url = firebase_url + '/' + client_name + '/temperature.json'        
    json_object = json.load(urllib2.urlopen(url))
    json.dump(json_object,  self.response.out ) #this will go to frontend

希望有帮助...但是我正在努力在 python 中获取“import firebase”然后执行操作

【讨论】:

    【解决方案2】:

    安装

    pip install firebase
    

    Python 版本

    Firebase 是为 python 3 及更高版本编写的,无法在 python 2 上正常工作。

    将 Firebase 添加到您的应用程序

    您的 Google 的 Firebase 配置数据可以在 Firebase > 设置 > 项目设置 滚动到底部 > 添加到网络应用 > 配置

    仅用于基于用户的身份验证,我们可以创建以下配置:

    from firebase import Firebase
    
    config = {
      "apiKey": "apiKey",
      "authDomain": "projectId.firebaseapp.com",
      "databaseURL": "https://databaseName.firebaseio.com",
      "storageBucket": "projectId.appspot.com"
    }
    
    firebase = Firebase(config)
    

    认证

    sign_in_with_email_and_password() 方法将返回用户数据,包括可用于遵守安全规则的令牌。

    以下每个方法都接受用户令牌:get()push()set()update()remove()stream()

    # Get a reference to the auth service
    auth = firebase.auth()
    
    # Log the user in
    user = auth.sign_in_with_email_and_password(email, password)
    
    # Get a reference to the database service
    db = firebase.database()
    
    # data to save
    data = {
        "name": "Joe Tilsed"
    }
    
    # Pass the user's idToken to the push method
    results = db.child("users").push(data, user['idToken'])
    

    数据库

    您可以使用child() 方法构建数据路径。

    db = firebase.database()
    db.child("users").child("Joe")
    

    保存数据

    要使用唯一的、自动生成的、基于时间戳的密钥保存数据,请使用push() 方法。

    data = {"name": "Joe Tilsed"}
    db.child("users").push(data)
    

    要创建您自己的密钥,请使用set() 方法。下例中的键是“Joe”。

    data = {"name": "Joe Tilsed"}
    db.child("users").child("Joe").set(data)
    
    更新

    要更新现有条目的数据,请使用update() 方法。

    db.child("users").child("Joe").update({"name": "Joe W Tilsed"})
    
    消除

    要删除现有条目的数据,请使用remove() 方法。

    db.child("users").child("Joe").remove()
    

    检索数据

    查询返回一个 FirebaseResponse 对象。对这些对象调用 val() 会返回查询数据。

    users = db.child("users").get()
    print(users.val())
    
    >> {"Joe": {"name": "Joe Tilsed"}, "Syd": {"name": "Sydney Cox"}}
    
    钥匙

    调用key()会返回查询数据的键。

    user = db.child("users").get()
    print(user.key())
    
    >> users
    
    每个

    返回一个对象列表,您可以在每个对象上调用val()key()

    all_users = db.child("users").get()
    for user in all_users.each():
        print(user.key())
    
        >> Joe 
    
        print(user.val())   
    
        >> {name": "Joe Tilsed"}
    
    得到

    要从路径返回数据,只需调用get() 方法。

    all_users = db.child("users").get()
    

    希望这会有所帮助。完整文档:https://pypi.org/project/firebase/

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多