python操作rabbitmq

1、安装pika

C:\Python27\Lib>pip install pika
Collecting pika
c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py
:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Na
me Indication) extension to TLS is not available on this platform. This may caus
e the server to present an incorrect TLS certificate, which can cause validation
 failures. You can upgrade to a newer version of Python to solve this. For more
information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissi
ngwarning.
  SNIMissingWarning
c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py
:122: InsecurePlatformWarning: A true SSLContext object is not available. This p
revents urllib3 from configuring SSL appropriately and may cause certain SSL con
nections to fail. You can upgrade to a newer version of Python to solve this. Fo
r more information, see https://urllib3.readthedocs.io/en/latest/security.html#i
nsecureplatformwarning.
  InsecurePlatformWarning
  Downloading pika-0.10.0-py2.py3-none-any.whl (92kB)
    44% |██████████████▏                 | 40kB 6.8kB/s eta 0:00:
    55% |█████████████████▋              | 51kB 5.5kB/s eta 0:
    66% |█████████████████████▏          | 61kB 6.6kB/s et
    77% |████████████████████████▊       | 71kB 7.2kB/s
    88% |████████████████████████████▎   | 81kB 8.1
    99% |███████████████████████████████▊| 92kB
    100% |████████████████████████████████| 102k
B 9.6kB/s
Installing collected packages: pika
Successfully installed pika-0.10.0

2.实例操作

rabbitmq服务就已经启动好了。接下来看下python编写的实例。实例的内容就是从send.py发送“Hello World!”到rabbitmq,receive.py从rabbitmq接收send.py发送的信息。

创建rabbitmqsend.py

import pika

# 连接到rabbitmq远程服务器
credentials = pika.PlainCredentials('aaa', 'aaa')
connection = pika.BlockingConnection(pika.ConnectionParameters('10.83.16.91', 5672, '/', credentials))
channels = connection.channel()

# 声明消息队列hello

channels.queue_declare(queue='hello')

# 发送消息到上面声明的hello队列,其中exchange表示交换器,能精确指定消息应该发送到哪个队列,routing_key设置为队列的名称,body就是发送的内容,具体发送细节暂时先不关注。
for i in range(0, 10):
    aaa = 'hello world' + str(i)
    channels.basic_publish(exchange='', routing_key='hello', body=aaa)
connection.close()

创建rabbitmqreceive.py

import pika
# 连接到rabbitmq远程服务器
credentials = pika.PlainCredentials('aaa', 'aaa')
connection = pika.BlockingConnection(pika.ConnectionParameters('10.83.16.91', 5672, '/', credentials))
channels = connection.channel()

#声明消息队列hello

channels.queue_declare(queue='hello')

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


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

print ' [*] Waiting for messages. To exit press CTRL+C'

channels.start_consuming()

[*] Waiting for messages. To exit press CTRL+C [x] Received 'hello world0' [x] Received 'hello world1' [x] Received 'hello world2' [x] Received 'hello world3' [x] Received 'hello world4' [x] Received 'hello world5' [x] Received 'hello world6' [x] Received 'hello world7' [x] Received 'hello world8' [x] Received 'hello world9'

如果启动两个接受消费者那么:

[*] Waiting for messages. To exit press CTRL+C [x] Received 'hello world0' [x] Received 'hello world2' [x] Received 'hello world4' [x] Received 'hello world6' [x] Received 'hello world8'

[*] Waiting for messages. To exit press CTRL+C [x] Received 'hello world1' [x] Received 'hello world3' [x] Received 'hello world5' [x] Received 'hello world7' [x] Received 'hello world9'


评论(0 ) 点赞(16)


暂未登录,请登录之后发表评论。 QQ