# -*- 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()
Beispiel #2
0
# -*- 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()
Beispiel #3
0
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, 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()
Beispiel #5
0
# -*- 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()