gpt4 book ai didi

python - 如何在 Google App Engine 上运行用 Python 编写的 MQTT 订阅者脚本

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:02 24 4
gpt4 key购买 nike

我为 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

但是当我尝试将其部署到 Google Cloud 上时,出现“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.) 在 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 文档 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>

关于python - 如何在 Google App Engine 上运行用 Python 编写的 MQTT 订阅者脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46501924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com