# _*_ coding: UTF-8 _*_
# fill map to redis
from redis_helper import RedisHelper
import traceback
import sys
import getopt
import json
REDIS_HOST = "192.168.35.241"
REDIS_PORT = 6388
MAPS_NAME = "hack_name"
JSON_FILE = "test.json" #{'jd':'1', 'youku':'2'}
# print(sys.argv)
def usage():
print(
"""
Usage:sys.args[0] [option]
-h or --host:Redis主机号
-p or --port:Redis端口号
-n or --name:表名称,redis map_key
-f or --file: json文件,redis map_value 例如:{'jd':'1', 'youku':'2'}
for example: python fill_redis.py -h 127.0.0.1 -p 6379 -n hack_name -f test.json
"""
)
def main():
if len(sys.argv) == 1:
usage()
sys.exit()
try:
opts, args = getopt.getopt(sys.argv[1:], "h:p:n:f:",
["help", "output="]) # sys.argv[1:] 过滤掉第一个参数(它是脚本名称,不是参数的一部分)
except getopt.GetoptError:
print("argv error,please input")
for cmd, arg in opts: # 使用一个循环,每次从opts中取出一个两元组,赋给两个变量。cmd保存选项参数,arg为附加参数。接着对取出的选项参数进行处理。
if cmd in ("-h", "--host"):
REDIS_HOST = arg
print(arg)
elif cmd in ("-p", "--port"):
REDIS_PORT = arg
print(arg)
elif cmd in ("-n", "--name"):
MAPS_NAME = arg
print(arg)
elif cmd in ("-f", "--file"):
json_file = arg
print(arg)
try:
str_json = ""
with open(JSON_FILE, 'r') as load_f:
str_json = load_f.read()
print(str_json)
dict_value = json.loads(str_json)
print(dict_value)
r = RedisHelper(REDIS_HOST, REDIS_PORT)
r.map_set(MAPS_NAME, dict_value)
print(r.map_get(MAPS_NAME))
# r.map_set("map1", {'jd':'1', 'youku':'2'})
# r.map_set("map2", {'test1':'1', 'test2':'2'})
# print(r.map_get('map1'))
# print(r.map_get('map2'))
except :
print(traceback.format_exc())
if __name__ == '__main__':
main()
fill_redis.zip_python_redis_redis Python
版权申诉
68 浏览量
2022-09-15
02:03:38
上传
评论
收藏 1KB ZIP 举报
weixin_42651887
- 粉丝: 75
- 资源: 1万+
最新资源
- 2022NOC软件创意编程赛项真题python小学高年级-决赛(有解析)
- mathml转换latex需要的xsl文件
- 2022NOC软件创意编程赛项真题图形化小学高年级-决赛赛(有解析)
- gbase驱动下载gbase-connector-java-8.3.81.53驱动下载
- 2022NOC软件创意编程赛项真题图形化小学低年级-决赛赛(有解析)
- InsightFace从青铜到王者,超大规模人脸识别的优雅解法
- python后端开发spider框架详解
- 基于 STM32 与 ESP8266 的智能家居系统源码.zip
- 毕业设计:基于SSM的mysql-个性化点餐配送系统(源码 + 数据库 + 说明文档)
- 基于matlab的鱼苗计数识别(GUI界面).zip代码57
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈