rabbitmq (hello word)

send.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pika
import sys
import ssl

username = 'yang'  # 指定远程rabbitmq的用户名密码
pwd = 'yang'
port = 15672
port = 5672
# port = 5671
host = "192.168.128.132"
# host = "localhost"
user_pwd = pika.PlainCredentials(username, pwd)

# s_conn = pika.BlockingConnection(
#     pika.ConnectionParameters(host=host, port=port, virtual_host='/', credentials=user_pwd))  # 创建连接

s_conn = pika.BlockingConnection(
    pika.ConnectionParameters(host=host, port=port, credentials=user_pwd))  # 创建连接

chan = s_conn.channel()  # 在连接上创建一个频道

chan.queue_declare(queue='hello')  # 声明一个队列,生产者和消费者都要声明一个相同的队列,用来防止万一某一方挂了,另一方能正常运行
chan.basic_publish(exchange='',  # 交换机
                   routing_key='hello',  # 路由键,写明将消息发往哪个队列,本例是将消息发往队列hello
                   body='python you known yangxiaodong ni kuku')  # 生产者要发送的消息

s_conn.close()  # 当生产者发送完消息后,可选择关闭连接

receive.py

import pika

username = "yang"
pwd = "yang"
host = "192.168.128.132"
port = 5672
user_pwd = pika.PlainCredentials(username, pwd)
connection = pika.BlockingConnection(pika.ConnectionParameters(host=host, port=port, credentials=user_pwd))

channel = connection.channel()
channel.queue_declare(queue="hello")

channel.queue_declare(queue='hello')


def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)


channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值