首页 > 编程语言 >利用python收发MQ

利用python收发MQ

时间:2024-04-20 09:03:43浏览次数:20  
标签:name headers python send topic 收发 MQ msg conn

一、收发MQ基础

引入stomp包,

# -*- coding: utf-8 -*-
import stomp
import time,random
from cmd.util import timePaser

port = 700



ip='10.13.69.16'
passcode='8JiPUndN'
queue_name = '/topic/SampleQueue'
topic_name = '/topic/cweb.topic.receive.msg'

listener_name = 'SampleListener'
class SampleListener(object):
    def on_message(self, headers, message):
        print ('headers: %s' % headers)
        print ('message: %s' % message)

# 推送到队列queue
def send_to_queue(msg):
    conn = stomp.Connection10([(ip, port)])
    conn.start()
    conn.connect()
    conn.send(queue_name, msg)
    conn.disconnect()

# 推送到主题
def send_to_topic(msg):
    headers = {'type': 'bytesMessage'}
    conn = stomp.Connection([(ip, port)])
    conn.start()
    conn.connect(username='admin', passcode=passcode, wait=True, )
    conn.send(destination=topic_name, body=msg, headers=headers) # 发送消息
    # print ("$$$ send one message")
    conn.disconnect()
##从队列接收消息
def receive_from_queue():
    conn = stomp.Connection10([(ip, port)])
    conn.set_listener(listener_name, SampleListener())
    conn.start()
    conn.connect()
    conn.subscribe(queue_name)
    time.sleep(1)  # secs
    conn.disconnect()

##从主题接收消息
def receive_from_topic():
    conn = stomp.Connection([(ip, port)])
    conn.set_listener(listener_name, SampleListener())
    conn.start()
    conn.connect(username='admin', passcode=passcode, wait=True, headers={'tcpNoDelay': 'true'})
    conn.subscribe(topic_name,id="", ack='auto')
    while 1:
        # send_to_topic('topic')
        time.sleep(3)  # secs
    conn.disconnect()```


 

二、构造随机报文 

 

```python
def sendTodoRand():
    for i in range(1,3):
        randomId=str(random.randrange(1000,9999));msgId=randomId+str(time.time())

        msg = '''{"msgId":"''' \
              +msgId+ '''","msgStatus":"'''+str(random.randrange(1,4))+'''","msgTitle":"待办事项标题'''\
              +randomId+'''","msgTime":"'''+timePaser.nowTime()+'''","extendStr":{"showFlag":1,"picUrl":"","extendJson":"{\\\"extend1\\\":\\\"人数:100\\\"}","extendNoShow":""}}]}}
            '''
        print(msgId);print(msg)
        send_to_topic(msg)```


 

 


![](https://mutouzuo.oss-cn-hangzhou.aliyuncs.com/my/mudouzuo1.png)

标签:name,headers,python,send,topic,收发,MQ,msg,conn
From: https://www.cnblogs.com/bigleft/p/18147145

相关文章

  • 涛思数据 TDengine 征稿— 利用python脚本做TDengine性能测试
    【本文正在参与“拥抱开源|涛思数据TDengine有奖征稿】https://marketing.csdn.net/p/0ada836ca30caa924b9baae0fd33857c设备上传的数据具有很明显的物联网特点,通常是时序性的,按时间先后顺序上报,而且写入后,几乎不会修改,主要是查询和统计。针对这些特点,时序数据库TDengine将......
  • 【开源】使用Python+Flask+Mysql快速开发一个用户增删改查系统
    项目演示项目本身很简单,增删改查是几乎所有系统的骨架。正所谓万丈高楼平地起,学会了增删改查,航母就指日可待了:),光速入门,直接看演示图:项目地址https://github.com/mudfish/python-flask-user-crudFlask框架介绍说白了就是一个Web框架,能够让你快速开发出Pythonweb应用。简单易......
  • MQTT协议
    一、MQTT协议简介MQTT(MessageQueuingTelemetryTransport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级"通讯协议,该协议构建于TCP/IP协议上,由IBM在1999年发布。MQTT协议是为工作在低带宽、不可靠的网络的远程传感器和控制设备通讯而设计的协议。二......
  • python --二分法学习
    deffound_number(need_vaule,l):print(l)mid_index=len(l)//2mid_value=l[mid_index]print("mid_valueis%s"%(mid_value))ifmid_value>need_vaule:l=l[:mid_index]print('needtofind1')......
  • springboot java调用flask python写的
    服务a用flask,服务b用的springboot,服务a写的接口,用python很容易就调通了,java来调,坑有点多1、url最后的斜杠必须两边对应上,否则flask会先308,而且contenttype[text/html;charset=utf-8],连对应的HttpMessageConverter都没有org.springframework.web.client.RestClientException:......
  • python 正则表达式匹配
    re模块: 案例:     python的贪婪和非贪婪 r的作用: ......
  • python 修改jenkins的配置文件
    python有jenkins获取配置文件的api,也有修改配置文件的api,下面介绍下如果修改jenkinsjob的配置文件内容:importreimporttimeimportjenkinsjenkins_url="http://xxx.com/jenkins"username="zhangsan"token="1.......de"jenkins=jenkins.Jenkins(jenkins_url,us......
  • STM32、ESP8266与MQTT连接阿里云物联网的串口通信异常解析
    STM32、ESP8266与MQTT协议连接阿里云物联网平台时常见的串口通信异常介绍在构建物联网应用时,STM32、ESP8266与MQTT协议的结合是实现设备与网络间稳定通信的关键。然而,在连接阿里云物联网平台的过程中,串口通信异常成为了一个常见的挑战。本文将探讨这些异常现象及其可能的原因,并给......
  • python多继承构造方法参数报错
    各路大神,今天下午在学习Python3.12多继承的时候,有个构造方法一直报错,希望大家能帮忙瞅瞅,求求了~~~~~~~代码如下:点击查看代码classRectangle:def__init__(self,width,height):self.width=widthself.height=heightdefarea(self):......
  • python minio
    fromminioimportMiniofile_name='3e09ca66d9444906935b0171e26891f1.mp4'file_path=r'E:\集成资料\视频素材'barrel="testdata"defupload_file():#创建minio客户端client=Minio(endpoint="xxx.xxx.xxx.xxx:xxxxx"......