2016-04-10 35 views
1

我是Python的新手,我想用Python連接到Firebase。我可以使用put()patch()成功添加和修改Firebase,但我無法找到從Firebase檢索我的數據的方法。如何使用Python從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) 

如何修改它來檢索數據?

+0

有幾個常用的庫。請參閱http://ozgur.github.io/python-firebase/和https://github.com/mikexstudios/python-firebase。這兩個在這裏提到https://www.firebase.com/docs/rest/quickstart.html。如果您遇到問題,請發佈您嘗試的內容,我們可以提供更好的幫助。 –

+0

我會嘗試一下,看看。非常感謝回覆 –

+0

嗨,你有沒有讓你的python腳本在沒有庫的情況下工作?這些圖書館的問題在於它們已經過時。現在,Firebase科技公司表示他們對該特定圖書館有問題。所以我認爲從python直接訪問他們的rest API會更安全。 – marciokoko

回答

0

我有這樣的事情這是工作

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

CLIENT_NAME = '孟買Kandivali'

類的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」,然後執行操作

相關問題