我为MQTT订阅者编写了一个python脚本,它在本地运行得很好。
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
但是当我尝试在谷歌云上部署它时,我得到 “ImportError:没有名为paho.mqtt.client的模块” 错误。
然后我尝试了以下解决方案,但收到了错误
1。)在app.yaml
中声明paho-mqtt库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 install paho-mqtt on Cloud shell
***OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/paho_mqtt-1.3.0.dist-info'***
现在怎么办?
答案 0 :(得分:1)
Google的文档explains如何在本地lib
目录下安装库。
- 创建一个目录来存储您的第三方库,例如lib /.
醇>
mkdir lib
- 使用带有-t标志的pip(版本6或更高版本)将库复制到您在上一步中创建的文件夹中。对于 例如:
醇>
pip install -t lib/ <library_name>