python用paho写了client端,可是连接不到apollo,可能有什么原因?

发布于 2022-09-07 03:45:36 字数 2763 浏览 32 评论 0

python代码

import os
import os.path
import time
import paho.mqtt.client as mqtt
import csv
import json

def ParsFile(filename):
    file_object = open(filename)
    try:
        if os.path.splitext(filename)[1]==".txt":
            for line in file_object:
                print(line)
                client.publish(topic="message", payload=line)
                time.sleep(1)
        else:
            with open(filename) as csvfile:
                reader = csv.DictReader(csvfile)
                line = {}
                for row in reader:
                    #print(row)
                    line['Time'] = row['Time']
                    line['Sno'] = row['Sno']
                    line['UpRate'] = row['UpRate']
                    line['Lng'] = row['Lng']
                    line['Lat'] = row['Lat']
                    line['Alt'] = row['Alt']
                    line['Nsize'] = row['Nsize']
                    line['Lac'] = row['Lac']
                    line['CID'] = row['CID']
                    line['RSRP0'] = row['RSRP0']
                    line['SINR0'] = row['SINR0']
                    line['RSRQ0'] = row['RSRQ0']
                    line['RSSI0'] = row['RSSI0']
                    Nsize = int(line['Nsize'])
                    for i in range(1, Nsize+1):
                        line['RSRP'+str(i)] = row['RSRP'+str(i)]
                    print(line)
                    client.publish(topic="message", payload=json.dumps(line))
                    line.clear()
                    time.sleep(1)
    finally:
        file_object.close()
    # this folder is custom
rootdir="C:\VLAB-1"

def WaikFiles(rootdir, client):
    for parent,dirnames,filenames in os.walk(rootdir):
        for filename in filenames:
            print("parent folder is:" + parent)
            print("filename with full path:"+ os.path.join(parent,filename))
            ParsFile(os.path.join(parent,filename))

 # The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))



if __name__ == "__main__":
    client = mqtt.Client()
    client.username_pw_set(username="admin",password="password")
    client.on_connect = on_connect

    client.connect("127.0.0.1", 61613, 60)
    client.loop_start()

    #while True:
    WaikFiles(rootdir, client)

    #while True:
    #    time.sleep(1)
    #   client.publish(topic="message", payload="hello")

apollo偶尔报错

clipboard.png

没有connector

clipboard.png

可是啊,在另一台pc上是没问题有connector :(

提前谢过!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文