Commit 5415c30e by 岳巧源

add first commit

parents
Showing with 158 additions and 0 deletions
import http
import os
import pika
import requests
class Producer:
"""init rabbitmq connection and push message"""
def __init__(self, config: dict):
credentials = pika.PlainCredentials(
username=config["rabbit_mq_user"],
password=config["rabbit_mq_password"],
)
self.conn = pika.BlockingConnection(
pika.ConnectionParameters(
host=config["rabbit_mq_host"],
port=config["rabbit_mq_port"],
credentials=credentials
)
)
channel = self.conn.channel()
channel.queue_declare(queue=config["rabbit_mq_queue_producer"], durable=True)
self.channel = channel
def __del__(self):
try:
self.channel.close()
self.conn.close()
except Exception as e:
print(e)
class HandleClient:
def __init__(self):
pass
def __del__(self):
"""remove the tmp xls file"""
try:
os.remove(self.path)
except Exception as e:
pass
def process(self, url, supplier_id, sku_file_id, config: dict):
try:
response = requests.get(url)
if response.status_code == http.HTTPStatus.OK:
tmp_file_name = self._get_file_name(url)
absolute_path = config.get("tmp_xls_file_dir") + tmp_file_name
self.path = absolute_path
with open(absolute_path, 'wb') as f:
f.write(response.content)
self.resolve(absolute_path, supplier_id, sku_file_id)
except Exception as e:
print(e)
def resolve(self, file_name, supplier_id, sku_file_id):
pass
def _get_file_name(self, url) -> str:
separator = "/"
return url.split(sep=separator)[-1]
"""
python script that handle and process the xls file
"""
import sys
import pika
from handle import HandleClient
config_dev = {
"rabbit_mq_host": "192.168.1.237",
"rabbit_mq_queue_consumer": "sku_manage_file_upload",
"rabbit_mq_queue_producer": "europa_erp_sku_queue",
"rabbit_mq_port": 5672,
"rabbit_mq_user": "huntadmin",
"rabbit_mq_password": "jy2y2900",
"rabbit_mq_exchange": "europa_erp_parse_file_exchange",
"rabbit_mq_routing_key": "europa_erp_sku_routing",
"tmp_xls_file_dir": "/data/golang/src/europa-erp-go/scripts/data_server/tmp/"
}
config_prod = {
"rabbit_mq_host": "119.23.79.136",
"rabbit_mq_queue_consumer": "sku_manage_file_upload",
"rabbit_mq_queue_producer": "europa_erp_sku_queue",
"rabbit_mq_port": 5672,
"rabbit_mq_user": "ans2024",
"rabbit_mq_password": "ans2024123",
"rabbit_mq_exchange": "europa_erp_parse_file_exchange",
"rabbit_mq_routing_key": "europa_erp_sku_routing",
"tmp_xls_file_dir": "/data/golang/src/europa-erp-go/scripts/data_server/tmp/"
}
config = {}
def callback(ch, method, properties, body):
"""
handle and process the message from rabbitmq
"""
try:
data = eval(body.decode())
if "file_address" in data and \
"supplier_id" in data:
url = str(data["file_address"]).replace("\\", "")
supplier_id = data["supplier_id"]
sku_file_id = data["sku_file_id"]
client = HandleClient()
client.process(url, supplier_id, sku_file_id, config)
except Exception as e:
print(e)
ch.basic_ack(delivery_tag=method.delivery_tag)
def listen_from_mq():
"""
the version of pika will be different in dev environment and prod environment.
dev environment: pika version is "0.x"
prod environment: pika version is "1.x"
"""
credentials = pika.PlainCredentials(
username=config.get("rabbit_mq_user"),
password=config.get("rabbit_mq_password"),
)
conn = pika.BlockingConnection(
pika.ConnectionParameters(
host=config.get("rabbit_mq_host"),
port=config.get("rabbit_mq_port"),
credentials=credentials,
)
)
channel = conn.channel()
channel.queue_declare(
queue=config.get("rabbit_mq_queue_consumer"),
durable=True
)
if pika.__version__[0] == "1":
channel.basic_consume(
queue=config.get("rabbit_mq_queue_consumer"),
on_message_callback=callback,
)
else:
channel.basic_consume(callback, queue=config.get("rabbit_mq_queue_consumer"))
channel.start_consuming()
if __name__ == '__main__':
environment = sys.argv[1]
if environment == "dev":
config = config_dev
elif environment == "prod":
config = config_prod
else:
sys.exit("param error!")
listen_from_mq()
print("environment: {}, listen from mq: {}".format(environment, config.get("rabbit_mq_host")))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment