# -*- coding: utf-8 -*- # Author: Ztj import time from common import channel, connection, pika channel.queue_declare(queue='persistent_queue') body = 'Hello World! - %s' % time.time() channel.basic_publish( exchange='', routing_key='persistent_routing_key', body='Hello World! - %s' % time.time(), properties=pika.BasicProperties(delivery_mode=2, ), ) print(" [x] Sent '%s'" % body) connection.close()
# -*- coding: utf-8 -*- # Author: Ztj import time from common import channel, connection channel.exchange_declare(exchange='exchange_fanout_exchange', exchange_type='fanout') body = 'Hello World! - %s' % time.time() channel.basic_publish( exchange='exchange_fanout_exchange', routing_key='', body=body, ) print(" [x] Sent '%s'" % body) connection.close()
import requests import parsel from urllib.parse import urljoin from common import channel, queue urls = [ "https://www.phei.com.cn/xwxx/index_{}.shtml".format(i) for i in range(1, 46) ] urls.append("https://www.phei.com.cn/xwxx/index.shtml") for url in urls: # 翻页爬取 resp = requests.get(url) sel = parsel.Selector(resp.content.decode("utf8")) li = sel.css(".web_news_list ul li.li_b60") for news in li: link = news.css("a:first-child::attr('href')").extract_first() full_link = urljoin(url, link) # 拼接完整 URL # 将新闻资讯详情页 URL 发布到 RabbitMQ 队列 channel.queue_declare(queue=queue) channel.basic_publish(exchange='', routing_key=queue, body='{}'.format(full_link)) print("[x] Sent '{}'".format(urljoin(url, link)))
# -*- coding: utf-8 -*- # Author: Ztj import time from common import channel, connection channel.queue_declare(queue='hello_queue') body = 'Hello World! - %s' % time.time() channel.basic_publish( exchange='', routing_key='hello_routing_key', body=body, ) print(" [x] Sent '%s'" % body) connection.close()
# -*- coding: utf-8 -*- # Author: Ztj import time import uuid from common import channel, pika, connection result = channel.queue_declare(exclusive=True) callback_queue_name = result.method.queue def on_response(ch, method, properties, body): print(" [x] on_response %r" % body) channel.basic_consume(on_response, queue=callback_queue_name, no_ack=True) channel.basic_publish( exchange='', routing_key='rpc_queue', body='send', properties=pika.BasicProperties( reply_to=callback_queue_name, correlation_id=str(uuid.uuid4()), ), ) time.sleep(3) connection.process_data_events()
# -*- coding: utf-8 -*- # Author: Ztj import time from common import channel, connection # 定义交换机 channel.exchange_declare(exchange='exchange_direct_exchange', exchange_type='direct') body = 'Hello World! - %s' % time.time() # 发布消息 channel.basic_publish( exchange='exchange_direct_exchange', routing_key='info', body=body, ) print(" [x] Sent '%s'" % body) connection.close()