# -*- coding: utf-8 -*- # Author: Ztj from common import channel channel.exchange_declare(exchange='exchange_topic_exchange', exchange_type='topic') result = channel.queue_declare(exclusive=True) queue_name = result.method.queue channel.queue_bind(exchange='exchange_topic_exchange', queue=queue_name, routing_key='one.*') def callback(ch, method, properties, body): print(" [x] Received %r" % body) channel.basic_consume(callback, queue=queue_name, no_ack=True) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
# -*- coding: utf-8 -*- # Author: Ztj from common import channel channel.queue_declare(queue='hello_queue') def callback(ch, method, properties, body): print(" [x] Received %r" % body) channel.basic_consume(callback, queue='hello_queue', no_ack=True) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
# -*- coding: utf-8 -*- # Author: Ztj from common import channel channel.queue_declare(queue='persistent_queue') def callback(ch, method, properties, body): print(" [x] Received %r" % body) ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_qos(prefetch_count=1) channel.basic_consume(callback, queue='persistent_queue') print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
import re import requests import parsel from urllib.parse import urljoin from common import channel, queue from common import detail def callback(ch, method, properties, body): url = str(body, "utf8") print(url) resp = requests.get(url) sel = parsel.Selector(resp.content.decode("utf8")) the_time = sel.css(".news_date::text").extract_first() pub_time = re.search("(\d+-\d+-\d+)", the_time).group(1) # 为保持文章排版和样式,保留标签 contents = sel.css(".news_content p").extract() content = "\n".join(contents) # 将文章数据存入 MongoDB detail.insert_one({"pubTime": pub_time, "url": url, "content": content}) channel.basic_consume( queue=queue, on_message_callback=callback, auto_ack=True) channel.start_consuming()
# -*- 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 from common import channel, pika channel.queue_declare(queue='rpc_queue') def on_request(ch, method, props, body): print(" [x] body - %r" % body) response = 'reply' ch.basic_publish( exchange='', routing_key=props.reply_to, properties=pika.BasicProperties(correlation_id=props.correlation_id), body=response) ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_qos(prefetch_count=1) channel.basic_consume(on_request, queue='rpc_queue') print(" [x] Awaiting RPC requests") channel.start_consuming()