Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
岳巧源
/
sku_upload_script
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5415c30e
authored
Sep 09, 2024
by
岳巧源
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
add first commit
parents
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
158 additions
and
0 deletions
handle.py
server.py
handle.py
0 → 100644
View file @
5415c30e
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
]
server.py
0 → 100644
View file @
5415c30e
"""
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"
)))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment