re-common 10.0.37__py3-none-any.whl → 10.0.39__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- re_common/baselibrary/__init__.py +4 -4
- re_common/baselibrary/baseabs/__init__.py +6 -6
- re_common/baselibrary/baseabs/baseabs.py +26 -26
- re_common/baselibrary/database/mbuilder.py +132 -132
- re_common/baselibrary/database/moudle.py +93 -93
- re_common/baselibrary/database/msqlite3.py +194 -194
- re_common/baselibrary/database/mysql.py +169 -169
- re_common/baselibrary/database/sql_factory.py +26 -26
- re_common/baselibrary/mthread/MThreadingRun.py +486 -486
- re_common/baselibrary/mthread/MThreadingRunEvent.py +349 -349
- re_common/baselibrary/mthread/__init__.py +2 -2
- re_common/baselibrary/mthread/mythreading.py +695 -695
- re_common/baselibrary/pakge_other/socks.py +404 -404
- re_common/baselibrary/readconfig/config_factory.py +18 -18
- re_common/baselibrary/readconfig/ini_config.py +317 -317
- re_common/baselibrary/readconfig/toml_config.py +49 -49
- re_common/baselibrary/temporary/envdata.py +36 -36
- re_common/baselibrary/tools/all_requests/aiohttp_request.py +118 -118
- re_common/baselibrary/tools/all_requests/httpx_requet.py +102 -102
- re_common/baselibrary/tools/all_requests/mrequest.py +412 -412
- re_common/baselibrary/tools/all_requests/requests_request.py +81 -81
- re_common/baselibrary/tools/batch_compre/bijiao_batch.py +31 -31
- re_common/baselibrary/tools/contrast_db3.py +123 -123
- re_common/baselibrary/tools/copy_file.py +39 -39
- re_common/baselibrary/tools/db3_2_sizedb3.py +102 -102
- re_common/baselibrary/tools/foreachgz.py +39 -39
- re_common/baselibrary/tools/get_attr.py +10 -10
- re_common/baselibrary/tools/image_to_pdf.py +61 -61
- re_common/baselibrary/tools/java_code_deal.py +139 -139
- re_common/baselibrary/tools/javacode.py +79 -79
- re_common/baselibrary/tools/mdb_db3.py +48 -48
- re_common/baselibrary/tools/merge_file.py +171 -171
- re_common/baselibrary/tools/merge_gz_file.py +165 -165
- re_common/baselibrary/tools/mhdfstools/down_hdfs_files.py +42 -42
- re_common/baselibrary/tools/mhdfstools/hdfst.py +42 -42
- re_common/baselibrary/tools/mhdfstools/up_hdfs_files.py +38 -38
- re_common/baselibrary/tools/mongo_tools.py +50 -50
- re_common/baselibrary/tools/move_file.py +170 -170
- re_common/baselibrary/tools/move_mongo/mongo_table_to_file.py +63 -63
- re_common/baselibrary/tools/move_mongo/move_mongo_table.py +354 -354
- re_common/baselibrary/tools/move_mongo/use_mttf.py +18 -18
- re_common/baselibrary/tools/move_mongo/use_mv.py +93 -93
- re_common/baselibrary/tools/mpandas/mpandasreadexcel.py +125 -125
- re_common/baselibrary/tools/mpandas/pandas_visualization.py +7 -7
- re_common/baselibrary/tools/myparsel.py +104 -104
- re_common/baselibrary/tools/rename_dir_file.py +37 -37
- re_common/baselibrary/tools/sequoiadb_utils.py +398 -398
- re_common/baselibrary/tools/split_line_to_many.py +25 -25
- re_common/baselibrary/tools/stringtodicts.py +33 -33
- re_common/baselibrary/tools/workwechant_bot.py +84 -84
- re_common/baselibrary/utils/baseaiohttp.py +296 -296
- re_common/baselibrary/utils/baseaiomysql.py +87 -87
- re_common/baselibrary/utils/baseallstep.py +191 -191
- re_common/baselibrary/utils/baseavro.py +19 -19
- re_common/baselibrary/utils/baseboto3.py +291 -291
- re_common/baselibrary/utils/basecsv.py +32 -32
- re_common/baselibrary/utils/basedict.py +133 -133
- re_common/baselibrary/utils/basedir.py +241 -241
- re_common/baselibrary/utils/baseencode.py +351 -351
- re_common/baselibrary/utils/baseencoding.py +28 -28
- re_common/baselibrary/utils/baseesdsl.py +86 -86
- re_common/baselibrary/utils/baseexcel.py +264 -264
- re_common/baselibrary/utils/baseexcept.py +109 -109
- re_common/baselibrary/utils/basefile.py +654 -654
- re_common/baselibrary/utils/baseftp.py +214 -214
- re_common/baselibrary/utils/basegzip.py +60 -60
- re_common/baselibrary/utils/basehdfs.py +135 -135
- re_common/baselibrary/utils/basehttpx.py +268 -268
- re_common/baselibrary/utils/baseip.py +87 -87
- re_common/baselibrary/utils/basejson.py +2 -2
- re_common/baselibrary/utils/baselist.py +32 -32
- re_common/baselibrary/utils/basemotor.py +190 -190
- re_common/baselibrary/utils/basemssql.py +98 -98
- re_common/baselibrary/utils/baseodbc.py +113 -113
- re_common/baselibrary/utils/basepandas.py +302 -302
- re_common/baselibrary/utils/basepeewee.py +11 -11
- re_common/baselibrary/utils/basepika.py +180 -180
- re_common/baselibrary/utils/basepydash.py +143 -143
- re_common/baselibrary/utils/basepymongo.py +230 -230
- re_common/baselibrary/utils/basequeue.py +22 -22
- re_common/baselibrary/utils/baserar.py +57 -57
- re_common/baselibrary/utils/baserequest.py +279 -279
- re_common/baselibrary/utils/baseset.py +8 -8
- re_common/baselibrary/utils/basesmb.py +403 -403
- re_common/baselibrary/utils/basestring.py +382 -382
- re_common/baselibrary/utils/basetime.py +320 -320
- re_common/baselibrary/utils/baseurl.py +121 -121
- re_common/baselibrary/utils/basezip.py +57 -57
- re_common/baselibrary/utils/core/__init__.py +7 -7
- re_common/baselibrary/utils/core/bottomutils.py +18 -18
- re_common/baselibrary/utils/core/mdeprecated.py +327 -327
- re_common/baselibrary/utils/core/mlamada.py +16 -16
- re_common/baselibrary/utils/core/msginfo.py +25 -25
- re_common/baselibrary/utils/core/requests_core.py +103 -103
- re_common/baselibrary/utils/fateadm.py +429 -429
- re_common/baselibrary/utils/importfun.py +123 -123
- re_common/baselibrary/utils/mfaker.py +57 -57
- re_common/baselibrary/utils/my_abc/__init__.py +3 -3
- re_common/baselibrary/utils/my_abc/better_abc.py +32 -32
- re_common/baselibrary/utils/mylogger.py +414 -414
- re_common/baselibrary/utils/myredisclient.py +861 -861
- re_common/baselibrary/utils/pipupgrade.py +21 -21
- re_common/baselibrary/utils/ringlist.py +85 -85
- re_common/baselibrary/utils/version_compare.py +36 -36
- re_common/baselibrary/utils/ydmhttp.py +126 -126
- re_common/facade/lazy_import.py +11 -11
- re_common/facade/loggerfacade.py +25 -25
- re_common/facade/mysqlfacade.py +467 -467
- re_common/facade/now.py +31 -31
- re_common/facade/sqlite3facade.py +257 -257
- re_common/facade/use/mq_use_facade.py +83 -83
- re_common/facade/use/proxy_use_facade.py +19 -19
- re_common/libtest/base_dict_test.py +19 -19
- re_common/libtest/baseavro_test.py +13 -13
- re_common/libtest/basefile_test.py +14 -14
- re_common/libtest/basemssql_test.py +77 -77
- re_common/libtest/baseodbc_test.py +7 -7
- re_common/libtest/basepandas_test.py +38 -38
- re_common/libtest/get_attr_test/get_attr_test_settings.py +14 -14
- re_common/libtest/get_attr_test/settings.py +54 -54
- re_common/libtest/idencode_test.py +53 -53
- re_common/libtest/iniconfig_test.py +35 -35
- re_common/libtest/ip_test.py +34 -34
- re_common/libtest/merge_file_test.py +20 -20
- re_common/libtest/mfaker_test.py +8 -8
- re_common/libtest/mm3_test.py +31 -31
- re_common/libtest/mylogger_test.py +88 -88
- re_common/libtest/myparsel_test.py +27 -27
- re_common/libtest/mysql_test.py +151 -151
- re_common/libtest/pymongo_test.py +21 -21
- re_common/libtest/split_test.py +11 -11
- re_common/libtest/sqlite3_merge_test.py +5 -5
- re_common/libtest/sqlite3_test.py +34 -34
- re_common/libtest/tomlconfig_test.py +30 -30
- re_common/libtest/use_tools_test/__init__.py +2 -2
- re_common/libtest/user/__init__.py +4 -4
- re_common/studio/__init__.py +4 -4
- re_common/studio/assignment_expressions.py +36 -36
- re_common/studio/mydash/test1.py +18 -18
- re_common/studio/pydashstudio/first.py +9 -9
- re_common/studio/streamlitstudio/first_app.py +65 -65
- re_common/studio/streamlitstudio/uber_pickups.py +23 -23
- re_common/studio/test.py +18 -18
- re_common/v2/baselibrary/business_utils/BusinessStringUtil.py +219 -219
- re_common/v2/baselibrary/business_utils/baseencodeid.py +100 -100
- re_common/v2/baselibrary/business_utils/full_doi_path.py +116 -116
- re_common/v2/baselibrary/business_utils/rel_tools.py +6 -6
- re_common/v2/baselibrary/decorators/utils.py +59 -59
- re_common/v2/baselibrary/helpers/search_packge/NearestNeighbors_test.py +105 -105
- re_common/v2/baselibrary/helpers/search_packge/fit_text_match.py +253 -253
- re_common/v2/baselibrary/helpers/search_packge/scikit_learn_text_matcher.py +260 -260
- re_common/v2/baselibrary/helpers/search_packge/test.py +1 -1
- re_common/v2/baselibrary/s3object/baseboto3.py +230 -230
- re_common/v2/baselibrary/tools/WeChatRobot.py +95 -95
- re_common/v2/baselibrary/tools/ac_ahocorasick.py +75 -75
- re_common/v2/baselibrary/tools/concurrency.py +35 -35
- re_common/v2/baselibrary/tools/data_processer/base.py +53 -53
- re_common/v2/baselibrary/tools/data_processer/data_processer.py +508 -508
- re_common/v2/baselibrary/tools/data_processer/data_reader.py +187 -187
- re_common/v2/baselibrary/tools/data_processer/data_writer.py +38 -38
- re_common/v2/baselibrary/tools/dict_tools.py +44 -44
- re_common/v2/baselibrary/tools/dolphinscheduler.py +187 -187
- re_common/v2/baselibrary/tools/hdfs_base_processor.py +204 -204
- re_common/v2/baselibrary/tools/hdfs_bulk_processor.py +67 -67
- re_common/v2/baselibrary/tools/hdfs_data_processer.py +338 -338
- re_common/v2/baselibrary/tools/hdfs_line_processor.py +74 -74
- re_common/v2/baselibrary/tools/list_tools.py +69 -69
- re_common/v2/baselibrary/tools/resume_tracker.py +94 -94
- re_common/v2/baselibrary/tools/search_hash_tools.py +54 -54
- re_common/v2/baselibrary/tools/text_matcher.py +326 -326
- re_common/v2/baselibrary/tools/unionfind_tools.py +60 -60
- re_common/v2/baselibrary/utils/BusinessStringUtil.py +196 -196
- re_common/v2/baselibrary/utils/api_net_utils.py +270 -270
- re_common/v2/baselibrary/utils/author_smi.py +361 -361
- re_common/v2/baselibrary/utils/base_string_similarity.py +158 -158
- re_common/v2/baselibrary/utils/basedict.py +37 -37
- re_common/v2/baselibrary/utils/basehdfs.py +163 -163
- re_common/v2/baselibrary/utils/basepika.py +180 -180
- re_common/v2/baselibrary/utils/basetime.py +77 -77
- re_common/v2/baselibrary/utils/db.py +156 -156
- re_common/v2/baselibrary/utils/json_cls.py +16 -16
- re_common/v2/baselibrary/utils/mq.py +83 -83
- re_common/v2/baselibrary/utils/n_ary_expression_tree.py +243 -243
- re_common/v2/baselibrary/utils/string_bool.py +186 -186
- re_common/v2/baselibrary/utils/string_clear.py +246 -246
- re_common/v2/baselibrary/utils/string_smi.py +18 -18
- re_common/v2/baselibrary/utils/stringutils.py +271 -278
- re_common/vip/base_step_process.py +11 -11
- re_common/vip/baseencodeid.py +90 -90
- re_common/vip/changetaskname.py +28 -28
- re_common/vip/core_var.py +24 -24
- re_common/vip/mmh3Hash.py +89 -89
- re_common/vip/proxy/allproxys.py +127 -127
- re_common/vip/proxy/allproxys_thread.py +159 -159
- re_common/vip/proxy/cnki_proxy.py +153 -153
- re_common/vip/proxy/kuaidaili.py +87 -87
- re_common/vip/proxy/proxy_all.py +113 -113
- re_common/vip/proxy/update_kuaidaili_0.py +42 -42
- re_common/vip/proxy/wanfang_proxy.py +152 -152
- re_common/vip/proxy/wp_proxy_all.py +181 -181
- re_common/vip/read_rawid_to_txt.py +91 -91
- re_common/vip/title/__init__.py +5 -5
- re_common/vip/title/transform/TransformBookTitleToZt.py +125 -125
- re_common/vip/title/transform/TransformConferenceTitleToZt.py +139 -139
- re_common/vip/title/transform/TransformCstadTitleToZt.py +195 -195
- re_common/vip/title/transform/TransformJournalTitleToZt.py +203 -203
- re_common/vip/title/transform/TransformPatentTitleToZt.py +132 -132
- re_common/vip/title/transform/TransformRegulationTitleToZt.py +114 -114
- re_common/vip/title/transform/TransformStandardTitleToZt.py +135 -135
- re_common/vip/title/transform/TransformThesisTitleToZt.py +135 -135
- re_common/vip/title/transform/__init__.py +10 -10
- {re_common-10.0.37.dist-info → re_common-10.0.39.dist-info}/LICENSE +201 -201
- {re_common-10.0.37.dist-info → re_common-10.0.39.dist-info}/METADATA +16 -16
- re_common-10.0.39.dist-info/RECORD +248 -0
- {re_common-10.0.37.dist-info → re_common-10.0.39.dist-info}/WHEEL +1 -1
- re_common-10.0.37.dist-info/RECORD +0 -248
- {re_common-10.0.37.dist-info → re_common-10.0.39.dist-info}/top_level.txt +0 -0
|
@@ -1,180 +1,180 @@
|
|
|
1
|
-
import pika
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
# https://blog.csdn.net/songfreeman/article/details/50943603
|
|
5
|
-
class BasePika(object):
|
|
6
|
-
|
|
7
|
-
def __init__(self, username=None, password=None, mqhost=None, virtual_host=None):
|
|
8
|
-
self.username = username
|
|
9
|
-
self.password = password
|
|
10
|
-
self.conn = None
|
|
11
|
-
self.host = mqhost
|
|
12
|
-
self.virtual_host = virtual_host
|
|
13
|
-
self.auto_ack = True
|
|
14
|
-
|
|
15
|
-
def set_default(self):
|
|
16
|
-
self.host = "192.168.31.79"
|
|
17
|
-
self.virtual_host = "vhost_NetDataGather"
|
|
18
|
-
self.username = "vip"
|
|
19
|
-
self.password = "piv$*123"
|
|
20
|
-
|
|
21
|
-
def connect_str(self,amqp_str):
|
|
22
|
-
parameters = pika.URLParameters(amqp_str)
|
|
23
|
-
self.conn = pika.BlockingConnection(parameters)
|
|
24
|
-
|
|
25
|
-
def connect(self):
|
|
26
|
-
"""
|
|
27
|
-
设置用户名 密码 进行连接
|
|
28
|
-
:return:
|
|
29
|
-
"""
|
|
30
|
-
credentials = pika.PlainCredentials(self.username, self.password)
|
|
31
|
-
# parameters = pika.URLParameters('amqp://guest:guest@rabbit-server1:5672/%2F')
|
|
32
|
-
# 可以通过将 heartbeat 设为 0,关闭 rabbitmq 的心跳检测
|
|
33
|
-
parameters = pika.ConnectionParameters(host=self.host,
|
|
34
|
-
virtual_host=self.virtual_host,
|
|
35
|
-
credentials=credentials,
|
|
36
|
-
heartbeat=0)
|
|
37
|
-
self.conn = pika.BlockingConnection(parameters)
|
|
38
|
-
|
|
39
|
-
def close(self):
|
|
40
|
-
# 关闭消息队列
|
|
41
|
-
self.conn.close()
|
|
42
|
-
|
|
43
|
-
def create_channel(self):
|
|
44
|
-
self.channel = self.conn.channel()
|
|
45
|
-
|
|
46
|
-
def __del__(self):
|
|
47
|
-
self.channel.close()
|
|
48
|
-
self.conn.close()
|
|
49
|
-
|
|
50
|
-
def random_queue_declare(self):
|
|
51
|
-
"""
|
|
52
|
-
这样, result.method.queue 包含一个随机的队列名, 比如:看起来像 amq.gen-JzTY20BRgKO-HjmUJj0wLg.
|
|
53
|
-
其次:
|
|
54
|
-
一旦我们断开consumer连接,这个队列名将自动删除。这里有一个标识设置:
|
|
55
|
-
:return:
|
|
56
|
-
"""
|
|
57
|
-
return self.channel.queue_declare("", exclusive=True)
|
|
58
|
-
|
|
59
|
-
def queue_declare(self, queue="hello", durable=False):
|
|
60
|
-
"""
|
|
61
|
-
创建目的地队列hello 取消息时也可以调用
|
|
62
|
-
取消息和发送消息都调用 保证队列存在,也保证了不管服务端还是客户端先启动都有队列
|
|
63
|
-
durable True 为持久化
|
|
64
|
-
:return:
|
|
65
|
-
"""
|
|
66
|
-
return self.channel.queue_declare(queue=queue, durable=durable)
|
|
67
|
-
|
|
68
|
-
def get_queue_size(self, queue="hello"):
|
|
69
|
-
"""
|
|
70
|
-
获取某个队列的长度
|
|
71
|
-
:param queue:
|
|
72
|
-
:return:
|
|
73
|
-
"""
|
|
74
|
-
queue = self.queue_declare(queue=queue, durable=True)
|
|
75
|
-
return queue.method.message_count
|
|
76
|
-
|
|
77
|
-
def get_properties(self):
|
|
78
|
-
"""
|
|
79
|
-
与 queue_declare里的 durable = True 配合使用,
|
|
80
|
-
设置给 easy_send_msg的properties
|
|
81
|
-
:return:
|
|
82
|
-
"""
|
|
83
|
-
return pika.BasicProperties(
|
|
84
|
-
delivery_mode=2, # 设置消息为持久化的
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
def easy_send_msg(self, exchange="", routing_key="hello", body="hello world", properties=None):
|
|
88
|
-
"""
|
|
89
|
-
空字符串标识默认的或者匿名的exchange,如果存在routing_key, 消息路由到routing_key指定的队列中。
|
|
90
|
-
routing_key 标识发送到哪个队列,就是服务器上的队列名
|
|
91
|
-
body 发送的消息
|
|
92
|
-
|
|
93
|
-
basic_publish 如果 exchange 不是"" 但没有绑定队列 消息会消失
|
|
94
|
-
:return:
|
|
95
|
-
"""
|
|
96
|
-
self.channel.basic_publish(exchange=exchange,
|
|
97
|
-
routing_key=routing_key,
|
|
98
|
-
body=body,
|
|
99
|
-
properties=properties)
|
|
100
|
-
|
|
101
|
-
def basic_ack(self, ch, method):
|
|
102
|
-
"""
|
|
103
|
-
callback的消息确认
|
|
104
|
-
:param ch:
|
|
105
|
-
:param method:
|
|
106
|
-
:return:
|
|
107
|
-
"""
|
|
108
|
-
ch.basic_ack(delivery_tag=method.delivery_tag)
|
|
109
|
-
|
|
110
|
-
def callback(self, ch, method, properties, body):
|
|
111
|
-
"""
|
|
112
|
-
从队列接收消息要更复杂一些,它需要为队列订阅一个 callback 函数来进行接收。
|
|
113
|
-
当我们接收一个消息后,这个 callback 函数将会被 pika函数库自动调用,
|
|
114
|
-
在我们的这个实例里面这个函数将用来打印接收的消息内容到屏幕
|
|
115
|
-
:param method:
|
|
116
|
-
:param properties:
|
|
117
|
-
:param body:
|
|
118
|
-
:return:
|
|
119
|
-
"""
|
|
120
|
-
print(type(body))
|
|
121
|
-
print(" [x] Received %r" % body)
|
|
122
|
-
if self.auto_ack is False:
|
|
123
|
-
self.basic_ack(ch, method)
|
|
124
|
-
|
|
125
|
-
def set_get_msg_callback(self, routing_key="hello", callback=None, auto_ack=True):
|
|
126
|
-
"""
|
|
127
|
-
设置取消息的callback
|
|
128
|
-
no_ack 如果设置为True,将使用自动确认模式
|
|
129
|
-
no_ack 如果设置为False,在callback中确认
|
|
130
|
-
:return:
|
|
131
|
-
"""
|
|
132
|
-
self.auto_ack = auto_ack
|
|
133
|
-
if callback is None:
|
|
134
|
-
callback = self.callback
|
|
135
|
-
self.channel.basic_consume(routing_key,
|
|
136
|
-
callback,
|
|
137
|
-
auto_ack=auto_ack)
|
|
138
|
-
|
|
139
|
-
def start_get_msg(self):
|
|
140
|
-
"""
|
|
141
|
-
开始取消息,会循环不停的取消息
|
|
142
|
-
:return:
|
|
143
|
-
"""
|
|
144
|
-
self.channel.start_consuming()
|
|
145
|
-
|
|
146
|
-
def basic_qos(self, prefetch_count=1):
|
|
147
|
-
"""
|
|
148
|
-
可以提前发送几个消息来,当auto_ack=True时无效
|
|
149
|
-
prefetch_count==1 消息未处理完前不要发送信息的消息
|
|
150
|
-
:return:
|
|
151
|
-
"""
|
|
152
|
-
self.channel.basic_qos(prefetch_count=prefetch_count)
|
|
153
|
-
|
|
154
|
-
def exchange_declare(self, exchangename="logs", type="fanout"):
|
|
155
|
-
"""
|
|
156
|
-
fanout exchange非常简单,你从这个名字中就能猜出来,它将从Producer方收到的消息广播给所有他知道的receiver方。而这正是我们的logger记录所需要的消息。
|
|
157
|
-
交换的类型
|
|
158
|
-
直接交换(direct exchange)的路由算法很简单 -- 消息发送到绑定键值(binding key) 刚好完全符合路由键值( routing key) 的消息队列中。
|
|
159
|
-
|
|
160
|
-
消息发送到一个 topic交换不能是一个任意的 routing_key -- 它必须是一个用小数点 分割的单词列表。 这个字符可以是任何单词,但是通常是指定一些连接特定消息的功能。一些有效的路由键(routing key)比如:“stock.usd.nyse",
|
|
161
|
-
topic 是 直接交换的升级版
|
|
162
|
-
|
|
163
|
-
headers Exchange :headers交换器允许你匹配AMQP消息的header而非路由键。除此之外,headers交换器和direct交换器完全一致,但性能会差很多。因此它并不太实用,而且几乎再也用不到了。
|
|
164
|
-
exchangename接下来会与队列绑定
|
|
165
|
-
direct , topic , headers 和 fanout
|
|
166
|
-
:return:
|
|
167
|
-
"""
|
|
168
|
-
return self.channel.exchange_declare(exchange=exchangename,
|
|
169
|
-
exchange_type=type)
|
|
170
|
-
|
|
171
|
-
def queue_bind(self, exchange="logs", queue="", routing_key=""):
|
|
172
|
-
"""
|
|
173
|
-
queue 临时队列获取 self.random_queue_declare().method.queue
|
|
174
|
-
:param exchange:
|
|
175
|
-
:param queue:
|
|
176
|
-
:return:
|
|
177
|
-
"""
|
|
178
|
-
self.channel.queue_bind(exchange=exchange,
|
|
179
|
-
queue=queue,
|
|
180
|
-
routing_key=routing_key)
|
|
1
|
+
import pika
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# https://blog.csdn.net/songfreeman/article/details/50943603
|
|
5
|
+
class BasePika(object):
|
|
6
|
+
|
|
7
|
+
def __init__(self, username=None, password=None, mqhost=None, virtual_host=None):
|
|
8
|
+
self.username = username
|
|
9
|
+
self.password = password
|
|
10
|
+
self.conn = None
|
|
11
|
+
self.host = mqhost
|
|
12
|
+
self.virtual_host = virtual_host
|
|
13
|
+
self.auto_ack = True
|
|
14
|
+
|
|
15
|
+
def set_default(self):
|
|
16
|
+
self.host = "192.168.31.79"
|
|
17
|
+
self.virtual_host = "vhost_NetDataGather"
|
|
18
|
+
self.username = "vip"
|
|
19
|
+
self.password = "piv$*123"
|
|
20
|
+
|
|
21
|
+
def connect_str(self,amqp_str):
|
|
22
|
+
parameters = pika.URLParameters(amqp_str)
|
|
23
|
+
self.conn = pika.BlockingConnection(parameters)
|
|
24
|
+
|
|
25
|
+
def connect(self):
|
|
26
|
+
"""
|
|
27
|
+
设置用户名 密码 进行连接
|
|
28
|
+
:return:
|
|
29
|
+
"""
|
|
30
|
+
credentials = pika.PlainCredentials(self.username, self.password)
|
|
31
|
+
# parameters = pika.URLParameters('amqp://guest:guest@rabbit-server1:5672/%2F')
|
|
32
|
+
# 可以通过将 heartbeat 设为 0,关闭 rabbitmq 的心跳检测
|
|
33
|
+
parameters = pika.ConnectionParameters(host=self.host,
|
|
34
|
+
virtual_host=self.virtual_host,
|
|
35
|
+
credentials=credentials,
|
|
36
|
+
heartbeat=0)
|
|
37
|
+
self.conn = pika.BlockingConnection(parameters)
|
|
38
|
+
|
|
39
|
+
def close(self):
|
|
40
|
+
# 关闭消息队列
|
|
41
|
+
self.conn.close()
|
|
42
|
+
|
|
43
|
+
def create_channel(self):
|
|
44
|
+
self.channel = self.conn.channel()
|
|
45
|
+
|
|
46
|
+
def __del__(self):
|
|
47
|
+
self.channel.close()
|
|
48
|
+
self.conn.close()
|
|
49
|
+
|
|
50
|
+
def random_queue_declare(self):
|
|
51
|
+
"""
|
|
52
|
+
这样, result.method.queue 包含一个随机的队列名, 比如:看起来像 amq.gen-JzTY20BRgKO-HjmUJj0wLg.
|
|
53
|
+
其次:
|
|
54
|
+
一旦我们断开consumer连接,这个队列名将自动删除。这里有一个标识设置:
|
|
55
|
+
:return:
|
|
56
|
+
"""
|
|
57
|
+
return self.channel.queue_declare("", exclusive=True)
|
|
58
|
+
|
|
59
|
+
def queue_declare(self, queue="hello", durable=False):
|
|
60
|
+
"""
|
|
61
|
+
创建目的地队列hello 取消息时也可以调用
|
|
62
|
+
取消息和发送消息都调用 保证队列存在,也保证了不管服务端还是客户端先启动都有队列
|
|
63
|
+
durable True 为持久化
|
|
64
|
+
:return:
|
|
65
|
+
"""
|
|
66
|
+
return self.channel.queue_declare(queue=queue, durable=durable)
|
|
67
|
+
|
|
68
|
+
def get_queue_size(self, queue="hello"):
|
|
69
|
+
"""
|
|
70
|
+
获取某个队列的长度
|
|
71
|
+
:param queue:
|
|
72
|
+
:return:
|
|
73
|
+
"""
|
|
74
|
+
queue = self.queue_declare(queue=queue, durable=True)
|
|
75
|
+
return queue.method.message_count
|
|
76
|
+
|
|
77
|
+
def get_properties(self):
|
|
78
|
+
"""
|
|
79
|
+
与 queue_declare里的 durable = True 配合使用,
|
|
80
|
+
设置给 easy_send_msg的properties
|
|
81
|
+
:return:
|
|
82
|
+
"""
|
|
83
|
+
return pika.BasicProperties(
|
|
84
|
+
delivery_mode=2, # 设置消息为持久化的
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def easy_send_msg(self, exchange="", routing_key="hello", body="hello world", properties=None):
|
|
88
|
+
"""
|
|
89
|
+
空字符串标识默认的或者匿名的exchange,如果存在routing_key, 消息路由到routing_key指定的队列中。
|
|
90
|
+
routing_key 标识发送到哪个队列,就是服务器上的队列名
|
|
91
|
+
body 发送的消息
|
|
92
|
+
|
|
93
|
+
basic_publish 如果 exchange 不是"" 但没有绑定队列 消息会消失
|
|
94
|
+
:return:
|
|
95
|
+
"""
|
|
96
|
+
self.channel.basic_publish(exchange=exchange,
|
|
97
|
+
routing_key=routing_key,
|
|
98
|
+
body=body,
|
|
99
|
+
properties=properties)
|
|
100
|
+
|
|
101
|
+
def basic_ack(self, ch, method):
|
|
102
|
+
"""
|
|
103
|
+
callback的消息确认
|
|
104
|
+
:param ch:
|
|
105
|
+
:param method:
|
|
106
|
+
:return:
|
|
107
|
+
"""
|
|
108
|
+
ch.basic_ack(delivery_tag=method.delivery_tag)
|
|
109
|
+
|
|
110
|
+
def callback(self, ch, method, properties, body):
|
|
111
|
+
"""
|
|
112
|
+
从队列接收消息要更复杂一些,它需要为队列订阅一个 callback 函数来进行接收。
|
|
113
|
+
当我们接收一个消息后,这个 callback 函数将会被 pika函数库自动调用,
|
|
114
|
+
在我们的这个实例里面这个函数将用来打印接收的消息内容到屏幕
|
|
115
|
+
:param method:
|
|
116
|
+
:param properties:
|
|
117
|
+
:param body:
|
|
118
|
+
:return:
|
|
119
|
+
"""
|
|
120
|
+
print(type(body))
|
|
121
|
+
print(" [x] Received %r" % body)
|
|
122
|
+
if self.auto_ack is False:
|
|
123
|
+
self.basic_ack(ch, method)
|
|
124
|
+
|
|
125
|
+
def set_get_msg_callback(self, routing_key="hello", callback=None, auto_ack=True):
|
|
126
|
+
"""
|
|
127
|
+
设置取消息的callback
|
|
128
|
+
no_ack 如果设置为True,将使用自动确认模式
|
|
129
|
+
no_ack 如果设置为False,在callback中确认
|
|
130
|
+
:return:
|
|
131
|
+
"""
|
|
132
|
+
self.auto_ack = auto_ack
|
|
133
|
+
if callback is None:
|
|
134
|
+
callback = self.callback
|
|
135
|
+
self.channel.basic_consume(routing_key,
|
|
136
|
+
callback,
|
|
137
|
+
auto_ack=auto_ack)
|
|
138
|
+
|
|
139
|
+
def start_get_msg(self):
|
|
140
|
+
"""
|
|
141
|
+
开始取消息,会循环不停的取消息
|
|
142
|
+
:return:
|
|
143
|
+
"""
|
|
144
|
+
self.channel.start_consuming()
|
|
145
|
+
|
|
146
|
+
def basic_qos(self, prefetch_count=1):
|
|
147
|
+
"""
|
|
148
|
+
可以提前发送几个消息来,当auto_ack=True时无效
|
|
149
|
+
prefetch_count==1 消息未处理完前不要发送信息的消息
|
|
150
|
+
:return:
|
|
151
|
+
"""
|
|
152
|
+
self.channel.basic_qos(prefetch_count=prefetch_count)
|
|
153
|
+
|
|
154
|
+
def exchange_declare(self, exchangename="logs", type="fanout"):
|
|
155
|
+
"""
|
|
156
|
+
fanout exchange非常简单,你从这个名字中就能猜出来,它将从Producer方收到的消息广播给所有他知道的receiver方。而这正是我们的logger记录所需要的消息。
|
|
157
|
+
交换的类型
|
|
158
|
+
直接交换(direct exchange)的路由算法很简单 -- 消息发送到绑定键值(binding key) 刚好完全符合路由键值( routing key) 的消息队列中。
|
|
159
|
+
|
|
160
|
+
消息发送到一个 topic交换不能是一个任意的 routing_key -- 它必须是一个用小数点 分割的单词列表。 这个字符可以是任何单词,但是通常是指定一些连接特定消息的功能。一些有效的路由键(routing key)比如:“stock.usd.nyse",
|
|
161
|
+
topic 是 直接交换的升级版
|
|
162
|
+
|
|
163
|
+
headers Exchange :headers交换器允许你匹配AMQP消息的header而非路由键。除此之外,headers交换器和direct交换器完全一致,但性能会差很多。因此它并不太实用,而且几乎再也用不到了。
|
|
164
|
+
exchangename接下来会与队列绑定
|
|
165
|
+
direct , topic , headers 和 fanout
|
|
166
|
+
:return:
|
|
167
|
+
"""
|
|
168
|
+
return self.channel.exchange_declare(exchange=exchangename,
|
|
169
|
+
exchange_type=type)
|
|
170
|
+
|
|
171
|
+
def queue_bind(self, exchange="logs", queue="", routing_key=""):
|
|
172
|
+
"""
|
|
173
|
+
queue 临时队列获取 self.random_queue_declare().method.queue
|
|
174
|
+
:param exchange:
|
|
175
|
+
:param queue:
|
|
176
|
+
:return:
|
|
177
|
+
"""
|
|
178
|
+
self.channel.queue_bind(exchange=exchange,
|
|
179
|
+
queue=queue,
|
|
180
|
+
routing_key=routing_key)
|
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
from datetime import datetime, timezone, timedelta
|
|
2
|
-
from typing import Optional
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class BaseTime(object):
|
|
6
|
-
@staticmethod
|
|
7
|
-
def get_utc_now() -> datetime:
|
|
8
|
-
"""获取当前UTC时间"""
|
|
9
|
-
return datetime.now(timezone.utc)
|
|
10
|
-
|
|
11
|
-
@staticmethod
|
|
12
|
-
def to_beijing_time(dt: datetime) -> datetime:
|
|
13
|
-
"""将datetime对象转换为北京时间(UTC+8)"""
|
|
14
|
-
return dt.astimezone(timezone(timedelta(hours=8)))
|
|
15
|
-
|
|
16
|
-
@staticmethod
|
|
17
|
-
def get_current_beijing_time() -> datetime:
|
|
18
|
-
"""获取当前北京时间"""
|
|
19
|
-
return BaseTime.to_beijing_time(BaseTime.get_utc_now())
|
|
20
|
-
|
|
21
|
-
@staticmethod
|
|
22
|
-
def get_beijing_time_str(format: str = "%Y-%m-%d %H:%M:%S") -> str:
|
|
23
|
-
"""
|
|
24
|
-
获取当前北京时间的字符串表示
|
|
25
|
-
:param format: 时间字符串的格式 格式为'YYYY-MM-DD HH:MM:SS'
|
|
26
|
-
"""
|
|
27
|
-
beijing_time = BaseTime.get_current_beijing_time()
|
|
28
|
-
return beijing_time.strftime(format)
|
|
29
|
-
|
|
30
|
-
@staticmethod
|
|
31
|
-
def get_current_month_str() -> str:
|
|
32
|
-
"""获取当前月份的字符串表示,格式为'YYYYMM'"""
|
|
33
|
-
beijing_time = BaseTime.get_current_beijing_time()
|
|
34
|
-
return beijing_time.strftime("%Y%m")
|
|
35
|
-
|
|
36
|
-
@staticmethod
|
|
37
|
-
def parse_beijing_time(time_str: str, format: str = "%Y-%m-%d %H:%M:%S") -> datetime:
|
|
38
|
-
"""
|
|
39
|
-
解析时间字符串为北京时间的datetime对象
|
|
40
|
-
|
|
41
|
-
:param time_str: 时间字符串
|
|
42
|
-
:param format: 时间字符串的格式
|
|
43
|
-
:return: 带有时区信息的datetime对象(UTC+8)
|
|
44
|
-
"""
|
|
45
|
-
dt = datetime.strptime(time_str, format)
|
|
46
|
-
return dt.replace(tzinfo=timezone(timedelta(hours=8)))
|
|
47
|
-
|
|
48
|
-
@staticmethod
|
|
49
|
-
def get_time_difference(time_str: str) -> timedelta:
|
|
50
|
-
"""
|
|
51
|
-
计算当前时间与给定时间字符串的时间差
|
|
52
|
-
|
|
53
|
-
:param time_str: 时间字符串,格式为"YYYY-MM-DD HH:MM:SS"
|
|
54
|
-
:return: timedelta对象,表示时间差
|
|
55
|
-
"""
|
|
56
|
-
current_time = BaseTime.get_current_beijing_time()
|
|
57
|
-
target_time = BaseTime.parse_beijing_time(time_str)
|
|
58
|
-
difference = current_time - target_time
|
|
59
|
-
# print(f"时间差: {difference}")
|
|
60
|
-
# print(f"总秒数: {difference.total_seconds()}秒")
|
|
61
|
-
# print(f"天数: {difference.days}天")
|
|
62
|
-
# print(f"小时数: {difference.total_seconds() / 3600:.2f}小时")
|
|
63
|
-
# print(f"分钟数: {difference.total_seconds() / 60:.2f}分钟")
|
|
64
|
-
|
|
65
|
-
return difference
|
|
66
|
-
|
|
67
|
-
@staticmethod
|
|
68
|
-
def is_hour_changed(last_time_str: str) -> bool:
|
|
69
|
-
"""
|
|
70
|
-
检查最后一次记录时间与当前时间是否跨越了小时边界
|
|
71
|
-
|
|
72
|
-
:param last_time_str: 最后一次记录的时间字符串,格式为"YYYY-MM-DD HH:MM:SS"
|
|
73
|
-
:return: True表示跨越了小时边界,False表示仍在同一小时内
|
|
74
|
-
"""
|
|
75
|
-
current_time = BaseTime.get_current_beijing_time()
|
|
76
|
-
last_time = BaseTime.parse_beijing_time(last_time_str)
|
|
77
|
-
return current_time.hour != last_time.hour
|
|
1
|
+
from datetime import datetime, timezone, timedelta
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class BaseTime(object):
|
|
6
|
+
@staticmethod
|
|
7
|
+
def get_utc_now() -> datetime:
|
|
8
|
+
"""获取当前UTC时间"""
|
|
9
|
+
return datetime.now(timezone.utc)
|
|
10
|
+
|
|
11
|
+
@staticmethod
|
|
12
|
+
def to_beijing_time(dt: datetime) -> datetime:
|
|
13
|
+
"""将datetime对象转换为北京时间(UTC+8)"""
|
|
14
|
+
return dt.astimezone(timezone(timedelta(hours=8)))
|
|
15
|
+
|
|
16
|
+
@staticmethod
|
|
17
|
+
def get_current_beijing_time() -> datetime:
|
|
18
|
+
"""获取当前北京时间"""
|
|
19
|
+
return BaseTime.to_beijing_time(BaseTime.get_utc_now())
|
|
20
|
+
|
|
21
|
+
@staticmethod
|
|
22
|
+
def get_beijing_time_str(format: str = "%Y-%m-%d %H:%M:%S") -> str:
|
|
23
|
+
"""
|
|
24
|
+
获取当前北京时间的字符串表示
|
|
25
|
+
:param format: 时间字符串的格式 格式为'YYYY-MM-DD HH:MM:SS'
|
|
26
|
+
"""
|
|
27
|
+
beijing_time = BaseTime.get_current_beijing_time()
|
|
28
|
+
return beijing_time.strftime(format)
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def get_current_month_str() -> str:
|
|
32
|
+
"""获取当前月份的字符串表示,格式为'YYYYMM'"""
|
|
33
|
+
beijing_time = BaseTime.get_current_beijing_time()
|
|
34
|
+
return beijing_time.strftime("%Y%m")
|
|
35
|
+
|
|
36
|
+
@staticmethod
|
|
37
|
+
def parse_beijing_time(time_str: str, format: str = "%Y-%m-%d %H:%M:%S") -> datetime:
|
|
38
|
+
"""
|
|
39
|
+
解析时间字符串为北京时间的datetime对象
|
|
40
|
+
|
|
41
|
+
:param time_str: 时间字符串
|
|
42
|
+
:param format: 时间字符串的格式
|
|
43
|
+
:return: 带有时区信息的datetime对象(UTC+8)
|
|
44
|
+
"""
|
|
45
|
+
dt = datetime.strptime(time_str, format)
|
|
46
|
+
return dt.replace(tzinfo=timezone(timedelta(hours=8)))
|
|
47
|
+
|
|
48
|
+
@staticmethod
|
|
49
|
+
def get_time_difference(time_str: str) -> timedelta:
|
|
50
|
+
"""
|
|
51
|
+
计算当前时间与给定时间字符串的时间差
|
|
52
|
+
|
|
53
|
+
:param time_str: 时间字符串,格式为"YYYY-MM-DD HH:MM:SS"
|
|
54
|
+
:return: timedelta对象,表示时间差
|
|
55
|
+
"""
|
|
56
|
+
current_time = BaseTime.get_current_beijing_time()
|
|
57
|
+
target_time = BaseTime.parse_beijing_time(time_str)
|
|
58
|
+
difference = current_time - target_time
|
|
59
|
+
# print(f"时间差: {difference}")
|
|
60
|
+
# print(f"总秒数: {difference.total_seconds()}秒")
|
|
61
|
+
# print(f"天数: {difference.days}天")
|
|
62
|
+
# print(f"小时数: {difference.total_seconds() / 3600:.2f}小时")
|
|
63
|
+
# print(f"分钟数: {difference.total_seconds() / 60:.2f}分钟")
|
|
64
|
+
|
|
65
|
+
return difference
|
|
66
|
+
|
|
67
|
+
@staticmethod
|
|
68
|
+
def is_hour_changed(last_time_str: str) -> bool:
|
|
69
|
+
"""
|
|
70
|
+
检查最后一次记录时间与当前时间是否跨越了小时边界
|
|
71
|
+
|
|
72
|
+
:param last_time_str: 最后一次记录的时间字符串,格式为"YYYY-MM-DD HH:MM:SS"
|
|
73
|
+
:return: True表示跨越了小时边界,False表示仍在同一小时内
|
|
74
|
+
"""
|
|
75
|
+
current_time = BaseTime.get_current_beijing_time()
|
|
76
|
+
last_time = BaseTime.parse_beijing_time(last_time_str)
|
|
77
|
+
return current_time.hour != last_time.hour
|