forked from rabbitinaction/sourcecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_server.py
More file actions
45 lines (39 loc) · 1.49 KB
/
rpc_server.py
File metadata and controls
45 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
###############################################
# RabbitMQ in Action
# Chapter 4.3.3 - RPC Server
#
# Requires: pika >= 0.9.5
#
# Author: Jason J. W. Williams
# (C)2011
###############################################
import pika, json
#/(apiserver.0) Establish connection to broker
creds_broker = pika.PlainCredentials("rpc_user", "rpcme")
conn_params = pika.ConnectionParameters("localhost",
virtual_host = "/",
credentials = creds_broker)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
#/(apiserver.1) Declare Exchange & "ping" Call Queue
channel.exchange_declare(exchange="rpc",
type="direct",
auto_delete=False)
channel.queue_declare(queue="ping", auto_delete=False)
channel.queue_bind(queue="ping",
exchange="rpc",
routing_key="ping")
#/(apiserver.2) Wait for RPC calls and reply
def api_ping(channel, method, header, body):
"""'ping' API call."""
channel.basic_ack(delivery_tag=method.delivery_tag)
msg_dict = json.loads(body)
print "Received API call...replying..."
channel.basic_publish(body="Pong!" + str(msg_dict["time"]),
exchange="",
routing_key=header.reply_to)
channel.basic_consume(api_ping,
queue="ping",
consumer_tag="ping")
print "Waiting for RPC calls..."
channel.start_consuming()