Created
December 20, 2019 11:55
-
-
Save onnet/831cce2e11e4aa74663df72ea142ad70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pika | |
import json | |
import requests | |
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.220.16.30')) | |
channel = connection.channel() | |
channel.exchange_declare(exchange='zbrt', exchange_type='topic') | |
result = channel.queue_declare(queue='', exclusive=True) | |
queue_name = result.method.queue | |
channel.queue_bind(exchange='zbrt', routing_key='#', queue=queue_name) | |
def on_request(ch, method, props, body): | |
parsed = json.loads(body) | |
print('Received message:') | |
print("==============================") | |
print(parsed) | |
print("==============================") | |
print(parsed['Data']) | |
print(parsed['Data']['action']) | |
print(parsed['Data']['api_token']) | |
print("==============================") | |
print('IS UGDP response:') | |
print("==============================") | |
response = requests.get( | |
'http://192.168.32.134:8081'+parsed['Data']['api_token'], | |
headers={'content-type': 'application/json', | |
'jwt': 'isugdp_auth_token'}, | |
) | |
print(response.status_code) | |
print(response.raise_for_status()) | |
print(response.headers) | |
print("==============================") | |
json_response = response.json() | |
print(json_response) | |
print("==============================") | |
data = {} | |
response_obj = {} | |
data['resp_status'] = response.status_code | |
data['resp_body'] = response.json() | |
response_obj['Msg-ID'] = parsed['Msg-ID'] | |
response_obj['Event-Category'] = parsed['Event-Category'] | |
response_obj['Event-Name'] = parsed['Event-Name'] | |
response_obj['App-Version'] = parsed['App-Version'] | |
response_obj['App-Name'] = parsed['App-Name'] | |
response_obj['data'] = data | |
response = json.dumps(response_obj) | |
ch.basic_publish(exchange='targeted', | |
routing_key=parsed['Server-ID'], | |
properties=pika.BasicProperties(content_type='application/json'), | |
body=str(response)) | |
ch.basic_ack(delivery_tag=method.delivery_tag) | |
channel.basic_qos(prefetch_count=1) | |
channel.basic_consume(queue=queue_name, on_message_callback=on_request) | |
print(" [x] Awaiting RPC requests") | |
channel.start_consuming() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment