[英]Is there a way to run Lua scripts in Google App Engine (Python)?
[英]How to run MQTT subscriber scripts written in python on google app engine
我为 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: No module named 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.) 在 Cloud shell 上 pip install paho-mqtt
***OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/paho_mqtt-1.3.0.dist-info'***
现在如何进行?
Google 的文档解释了如何在本地lib
目录下安装库。
- 创建一个目录来存储您的第三方库,例如 lib/。
mkdir lib
- 使用带有 -t 标志的 pip(版本 6 或更高版本)将库复制到您在上一步中创建的文件夹中。 例如:
pip install -t lib/ <library_name>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.