2017-09-30 44 views
1

我已經爲MQTT訂戶編寫了一個python腳本,它在本地完美地工作。如何在Google應用引擎中運行用python編寫的MQTT訂閱者腳本

的Python腳本用戶

import paho.mqtt.client as mqtt 
import os,json 

filePath = "logs.csv" 

def initiateFile(): 
    if (os.path.exists(filePath) == False): 
     fileObj = open(filePath, "w") 
     fileObj.write("Label,x,y,z\n") 

def readFile(): 
    data = open(filePath,'r').read() 
    return data 

def decodeJson(jsonString): 
    jsonObject = json.loads(jsonString) 
    label = jsonObject.keys()[0] 
    x = jsonObject[label]['x'] 
    y = jsonObject[label]['y'] 
    z = jsonObject[label]['z'] 
    return label.encode("utf-8")+","+x+","+y+","+z; 

def writeInFile(newData): 
    oldData = readFile() 
    fileObj = open(filePath, "w") 
    fileObj.write(oldData+newData+"\n") 

def on_connect(client, userdata, flags, rc): 
    print("Connected with result code "+str(rc)+" "+str(client)) 
    client.subscribe("sensors/test") 
    initiateFile() 

def on_message(client, userdata, msg): 
    print msg.payload 
    writeInFile(decodeJson(msg.payload)) 

def on_disconnect(client, userdata, rc): 
    print("Disconnect, reason: " + str(rc)) 
    print("Disconnect, reason: " + str(client)) 

client = mqtt.Client() 
client.username_pw_set(username, password) 
client.connect(broker,port) 
client.on_connect = on_connect 
client.on_message = on_message 
client.loop_forever() 
client.on_disconnect = on_disconnect 

但是當我試圖將其部署到谷歌雲,我得到「導入錯誤:沒有模塊名爲paho.mqtt.client」錯誤。

然後我嘗試以下解決方案,但有錯誤

1)聲明泛美衛生組織,MQTT庫app.yaml中

runtime: python27 
api_version: 1 
threadsafe: true 

handlers: 
- url: /.* 
    script: main.app 

libraries: 
- name: paho-mqtt 
    version: "1.3.0" 

***ERROR: (gcloud.app.deploy) An error occurred while parsing file: [C:\Users\uni5p_000\Desktop\RMIT_Studies\Sem_1\Cloud_Computing\Practical\GOOGLE\python-docs-samples\appengine\standard\hello_world\app.yaml] 
the library "paho-mqtt" is not supported 
    in "C:\Users\uni5p_000\Desktop\RMIT_Studies\Sem_1\Cloud_Computing\Practical\GOOGLE\python-docs-samples\appengine\standard\hello_world\app.yaml", line 11, column 19*** 

2)PIP安裝雲泛美衛生組織,MQTT殼

***OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/paho_mqtt-1.3.0.dist-info'*** 

現在該怎麼辦?

+0

的可能的複製[如何包括在谷歌應用程序引擎的第三方Python庫?](https://stackoverflow.com/questions/14850853/how-to-include-third-party-python-libraries- in-google-app-engine) – hardillb

+0

如果您在標準環境中運行,即使您對安裝和套接字進行了整理,這也不會起作用。標準環境不支持長時間運行的查詢。面向前方的請求只有1分鐘運行,任務隊列限制爲10分鐘。我想你可以不斷連接/斷開由cron –

回答

1

Google文檔explains如何在本地lib目錄下安裝庫。

  1. Create a directory to store your third-party libraries, such as lib/.

mkdir lib

  1. Use pip (version 6 or later) with the -t flag to copy the libraries into the folder you created in the previous step. For example:

pip install -t lib/ <library_name>

+0

觸發的每個請求感謝您的建議,它現在的工作。但是我收到了新的錯誤「FeatureNotEnabledError:一旦在管理控制檯中啓用了計費功能,就會爲此應用程序啓用Socket API。」 –

+0

這個錯誤接近自我解釋。如果此答案回答了原問題,請將其標記爲如此。 – hardillb

相關問題