2014-04-29 15 views
0

我正在嘗試編寫腳本來將現有的數據庫移植到Firebase中。我的數據存儲在JSON中,我認爲我可以拉JSON,然後將其作爲數據發送到我的Firebase的POST。Python中的REST調用

def Post_And_Recieve_JSON(url, data, headers): 
    print("Compiling query...") 
    Post_And_Recieve_JSON.url = url 
    Post_And_Recieve_JSON.headers = headers 
    Post_And_Recieve_JSON.data = (data) 

    print("Sending request...") 
    request = urllib.request.Request(url=url, data=data,headers=headers) 
    print("Recieving response...") 
    response = urllib.request.urlopen(request) 

    print("Reading response...") 
    response_data = response.read() 
    print("Converting into usable format...") 
    response_data_JSON = json.loads(response_data.decode(encoding='UTF-8')) 

    return response_data_JSON 

for all_users in existing_database: 
    full_data.append(Post_And_Recieve_JSON(...) 

for item in full_data: 
    url = 'firebaseurlhere ' + item['profileId'] + '.json' 
    data = json.dumps(item).encode('ascii') 
    Post_And_Recieve_JSON(url, data, headers) 

其中full_data是JSON對象的列表我已經從現有的數據庫中正確取出。

我得到 「http.client.BadStatusLine: ''」

+0

我想使用firebase python幫手,http://ozgur.github.io/python-firebase/,但是這是爲python2.7。我需要從python3的urllib和urllib.request拉我的原始數據。 – MZA

回答

0

我用在這裏找到火力蟒蛇LIB解決了這個:http://ozgur.github.io/python-firebase/

我用PIP3安裝它。我只希望我可以像我做其他REST調用一樣完成它,而不需要一個新的lib。

full_data = [] 
from firebase import * 
firebase = firebase.FirebaseApplication('https://secret_url.firebaseio.com/', None) 

for id in id_list: 
    print(str(id)) 
    url = 'from_url' 
    try: 
     result = firebase.put('/users/ ' + str(id) + '/', data=Post_And_Recieve_JSON(url, None, headers)["payload"], name='Data') 
    except: 
     print('Skipping a user')