python第五十七天------補上筆記
阿新 • • 發佈:2017-07-20
3-0 back pre 服務端 exchange locking ble star fanout
direct_client:廣播接收
1 #!/usr/bin/env python 2 3 #_*_coding:utf-8_*_ 4 5 import pika,time,sys 6 7 connection = pika.BlockingConnection(pika.ConnectionParameters( 8 ‘localhost‘)) 9 channel = connection.channel() 10 11 channel.exchange_declare(exchange=‘direct_logs‘,#定義一個接收的頻道 12View Codetype=‘direct‘) 13 14 reult=channel.queue_declare(exclusive=True)#隨機生成唯一的隊列名,會在消息接收後自動刪除 15 queuename=reult.method.queue#隊列名 自動生成 16 17 18 severities = sys.argv[1:] 19 if not severities: 20 sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])#啟動接收的消息級別21 sys.exit(1) 22 23 for severity in severities:#循環接收各級別的消息 24 channel.queue_bind(exchange=‘direct_logs‘, 25 queue=queuename, 26 routing_key=severity) 27 28 def callback(ch, method, properties, body):#回調函數 29 print(‘接收消息中…………‘) 30 #time.sleep(5)31 print(" [x] Received %r" % body.decode()) 32 ch.basic_ack(delivery_tag=method.delivery_tag) 33 34 35 channel.basic_qos(prefetch_count=1)#同時只處理一個消息 36 channel.basic_consume(callback,#接收到消息調用回調函數 callback 37 queue=queuename, 38 #no_ack=True 39 ) 40 41 print(‘ [*] 接收消息中. To exit press CTRL+C‘) 42 43 channel.start_consuming()#啟動消息接收
rabbitmq_server_direct 服務端 廣播
1 #!/usr/bin/env python 2 #{data} {time} 3 #_*_coding:utf-8_*_ 4 5 import pika,sys,time 6 connection = pika.BlockingConnection(pika.ConnectionParameters( 7 ‘localhost‘)) 8 channel = connection.channel()#管道 9 10 11 12 severity = sys.argv[1] if len(sys.argv) > 1 else ‘info‘#啟動參數 默認無參數為 info 級別 13 msg=‘‘.join(sys.argv[2:]) or ‘info:消息默認發送………‘#啟動參數 為空,發默認消息 14 for i in range(10): 15 time.sleep(1) 16 channel.basic_publish(exchange=‘direct_logs‘,#綁定頻道 17 routing_key=severity,#默認的消息隊列級別 18 body=msg+str(i), 19 #properties=pika.BasicProperties(delivery_mode=2)#持久化 廣播不能使用 20 ) 21 print(msg,severity) 22 connection.close() 23 #channel.close()View Code
rabbitmq_server:
View Code
rabbitmq_server_2: 消息持久化
1 import pika 2 connection = pika.BlockingConnection(pika.ConnectionParameters( 3 ‘localhost‘)) 4 channel = connection.channel()#管道 5 6 #聲明queue 7 #channel.queue_declare(queue=‘hello‘)#隊列名 hello 8 channel.queue_declare(queue=‘hello‘,durable=True)#隊列名 hello,持久化隊列 9 10 for i in range(10): 11 12 channel.basic_publish(exchange=‘‘, 13 routing_key=‘hello‘, 14 body=‘Hello World!%s‘%i, 15 properties=pika.BasicProperties(delivery_mode=2)) 16 print(" [x] Sent ‘Hello World!‘",i) 17 connection.close()View Code
rabbitmq_server_fanout. fanout 模式發送端
1 import pika,sys,time 2 connection = pika.BlockingConnection(pika.ConnectionParameters( 3 ‘localhost‘)) 4 channel = connection.channel()#管道 5 6 #聲明queue 廣播模式不用聲明隊列 7 #channel.queue_declare(queue=‘hello‘)#隊列名 hello 8 #channel.queue_declare(queue=‘hello‘,durable=True)#隊列名 hello,持久化隊列 9 10 argv=input(‘輸入消息‘) 11 msg=‘‘.join(sys.argv[1:]) or ‘info:消息默認發送………‘ 12 for i in range(10): 13 time.sleep(1) 14 channel.basic_publish(exchange=‘logs‘,#綁定頻道 15 #routing_key=‘hello‘, 16 routing_key=‘‘, 17 body=msg+str(i), 18 #properties=pika.BasicProperties(delivery_mode=2)#持久化 廣播不能使用 19 ) 20 print(msg,i) 21 #connection.close()View Code
rabbitmq_client_fanout fanout 接收端
1 #!/usr/bin/env python 2 #{data} {time} 3 4 #_*_coding:utf-8_*_ 5 6 import pika,time 7 8 connection = pika.BlockingConnection(pika.ConnectionParameters( 9 ‘localhost‘)) 10 channel = connection.channel() 11 #channel.queue_declare(queue=‘hello2‘)#服務端與客戶端的設置需一致,不然會報錯 12 #channel.queue_declare(queue=‘hello2‘,durable=True)#隊列名 hello,持久化隊列 13 channel.exchange_declare(exchange=‘logs‘,#綁定頻道 14 type=‘fanout‘)#接收類型 15 reult=channel.queue_declare(exclusive=True)#隨機生成唯一的隊列名,會在消息接收後自動刪除 16 queuename=reult.method.queue#隊列名 自動生成 17 channel.queue_bind(exchange=‘logs‘,#先要綁定頻道 18 queue=queuename 19 ) 20 21 22 def callback(ch, method, properties, body):#回調函數 23 print(‘接收消息中…………‘) 24 #time.sleep(5) 25 print(" [x] Received %r" % body.decode()) 26 ch.basic_ack(delivery_tag=method.delivery_tag) 27 28 29 channel.basic_qos(prefetch_count=1)#同時只處理一個消息 30 channel.basic_consume(callback,#接收到消息調用回調函數 callback 31 queue=queuename, 32 #no_ack=True 33 ) 34 35 print(‘ [*] 接收消息中. To exit press CTRL+C‘) 36 37 channel.start_consuming()#啟動消息接收View Code
python第五十七天------補上筆記