日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 连接 rabbitMQ以及rabbitMQssl注意事项,password

發布時間:2024/7/5 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 连接 rabbitMQ以及rabbitMQssl注意事项,password 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
pip3 install pika==1.1.0

官方對于pika有如下介紹#
Since threads aren’t appropriate to every situation, it doesn’t require threads.
Pika core takes care not to forbid them, either.
The same goes for greenlets, callbacks, continuations, and generators.
An instance of Pika’s built-in connection adapters isn’t thread-safe, however.
線程并不適用于每種場景, 因此并不要求使用線程。 但是pika并不禁用線程, 對于

greenlets, callbacks也一樣。 一個pika建立的連接并不是線程安全的

因此在多線程中共享一個pika連接不是線程安全的, 當然也有一種使用:

with one exception: you may call the connection method add_callback_threadsafe from
another thread to schedule a callback within an active pika connection.
使用add_callback_threadsafe方法callback 一個pika連接從另外一個線程中

pika提供建立連接方式:#

pika.adapters.asyncio_connection.AsyncioConnection - 用于python 3 AsyncIO的I/O異步模式pika.BlockingConnection - 同步模式, 簡單易用pika.SelectConnection - 沒有第三方依賴包的異步模式pika.adapters.tornado_connection.TornadoConnection - 基于Tornado 的異步IO請求模式pika.adapters.twisted_connection.TwistedProtocolConnection - 基于Twisted’的異步IO請求模式

二、普通版rabbitmq#
環境說明
操作系統 ip 主機名 配置 rabbitmq版本

centos 6.9 192.168.31.7 mq_01 1核2g 3.8.2centos 6.9 192.168.31.216 mq_02 1核2g 3.8.2centos 6.9 192.168.31.214 mq_03 1核2g 3.8.2ubuntu 16.04 192.168.31.229 mq_client 1核2g N/A

注意:3臺mq服務器,已經開啟ssl。開啟ssl之后,使用明文傳輸和使用ssl傳輸,都是可以的。
生產者#
producer.py

import pikaauth = pika.PlainCredentials('admin', 'admin123') connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.31.7', '5672', '/', auth)) channel = connection.channel()channel.queue_declare(queue='TEST01')channel.basic_publish(exchange='',routing_key='TEST01',body='Hello World!') print(" [x] Sent 'Hello World!'") connection.close()

執行腳本#

python3 produce.py

[x] Sent ‘Hello World!’
消費者#
consumer.py

import pikaauth = pika.PlainCredentials('admin', 'admin123') connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.31.7',port=5672,virtual_host= '/', credentials=auth)) channel = connection.channel()channel.queue_declare(queue='TEST01')def callback(ch, method, properties, body):print(" [x] Received %r" % body)channel.basic_consume(on_message_callback=callback,queue='TEST01',auto_ack=True)

print(’ [*] Waiting for messages. To exit press CTRL+C’)
channel.start_consuming()
執行腳本#

# python3 consumer.py [*] Waiting for messages. To exit press CTRL+C[x] Received b'Hello World!'

使用CTRL+C 結束腳本
三、SSL版rabbitmq#
客戶端采用的是ubuntu 16.04,因為發現centos 7.6編譯python 3.7時,導入ssl報錯,無法解決。
ubuntu 16.04可以解決導入ssl模塊問題。
ubuntu 16.04安裝python3-openssl
apt-get install -y python3-openssl

生產者#
下載github示例代碼,編輯配置文件

git clone https://github.com/Nepitwin/RabbitSSLcd RabbitSSL-master/Pythonvi configuration.py

修改黃色部分

import pika import sslssl_opts = {"ca_certificate": "ca_certificate.pem","client_certificate": "client_certificate.pem","client_key": "client_key.pem","cert_reqs": ssl.CERT_REQUIRED,"ssl_version": ssl.PROTOCOL_TLSv1_2 }rabbit_opts = {"host": "192.168.31.7","port": 5671,"user": "admin","password": "admin123", }rabbit_queue_opts = {"queue": "python_ssl","message": "Hello SSL World :)" }context = ssl.create_default_context(cafile=ssl_opts["ca_certificate"])

忽略證書驗證

context = ssl._create_unverified_context()context.load_cert_chain(ssl_opts["client_certificate"], ssl_opts["client_key"]) ssl_options = pika.SSLOptions(context, rabbit_opts["host"]) parameters = pika.ConnectionParameters(host=rabbit_opts["host"],port=rabbit_opts["port"],credentials=pika.PlainCredentials(rabbit_opts["user"], rabbit_opts["password"]),ssl_options=ssl_options) 注意:由于證書是不受信任的,因此必須要關閉證書驗證,否則會運行報錯,提示證書驗證失敗! 執行腳本#

**

context.load_cert_chain(ssl_opts["client_certificate"], ssl_opts["client_key"]) 一定要添加參數password,否則代碼就會一直卡在等你輸入密碼。終端會提醒你輸入密碼還好,直接運行代碼是不提醒的。

context.load_cert_chain(ssl_opts["client_certificate"], ssl_opts["client_key"], *password='bunnies'*)

**

python3 rabbitssl_send.py

[x] Sent 'Hello SSL World :)!'

消費者#

執行腳本

python3 rabbitssl_consume.py

[*] Waiting for messages. To exit press CTRL+C[x] Received b'Hello SSL World :)'

使用CTRL+C 結束腳本

總結

以上是生活随笔為你收集整理的python 连接 rabbitMQ以及rabbitMQssl注意事项,password的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。