python 操作redis 的脚本

python

需要向认证服务缓存sp 操作 黑名单的各种操作

#!/usr/bin/python

# -*- coding: UTF-8 -*-

import redis

import datetime

import sys

import os

import time

#IPS-LOCK-smtp-ip

#IPS-LOCK-web-183.14.29.22 

def usage():
     program_name=sys.argv[0]
     print "操作sp的IP黑名单 ---------"
     print "其中 ipfile的格式为每一行一个ip EX是 key lifetime ttl"
     print program_name+" set|get|ttl|del all|web|smtp|pop|imap ip|ipfile"
     print program_name+" set|get|ttl|del all|web|smtp|pop|imap ip|ipfile EX"
     sys.exit(0)

def is_file(pathfile):
     return os.path.isfile(pathfile)

def timestamp_toString(sp):
     return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sp))

def datetime_toTimestamp(dt):
     return time.mktime(dt.timetuple())

#操作逻辑函数

def fun(redis_c,operation,extime=60,*list_key):
     try:   
         list_key=list(list_key)
        
         datetime_n=datetime.datetime.now()
         nowtime=datetime_n.strftime("%Y-%m-%d %H:%M:%S")
         time_n=datetime_toTimestamp(datetime_n)+extime
         end_strtime=timestamp_toString(time_n)
         #print "xxx %s %s %s %d,%d" %(nowtime,end_strtime,str(extime),datetime_toTimestamp(datetime_n),time_n)
        
         for key in list_key:
             if operation == "get":   
                 GetKey=conn_redis.get(key)
                 #print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                 print "get "+key+" rc="+str(GetKey)
             elif operation == "ttl":
                 ttl=conn_redis.ttl(key)
                 print "ttl "+key+" rc="+str(ttl)
             elif operation == "del":
                 del_ret=conn_redis.delete(key)
                 print "delete "+key+" rc="+str(del_ret)
             elif operation == "set":
                 uip = key.split('-')[-1] #IPS-LOCK-web-183.14.29.22 
                 value = "FROM "+nowtime+" "+end_strtime+" %s sys_duration=%d" % (key,extime)
                 rc=redis_c.setex(key,value,extime)
                 print "set "+key+" rc="+str(rc)

    except Exception,e:
         print "Exception:"+str(e)
        

if len(sys.argv) < 4:
    
     usage()

else:
     operation=sys.argv[1]
     op_type=sys.argv[2]
     op_ip=sys.argv[3]
     op_ipfile=None
     list_key=[]
     if is_file(op_ip): #如果是ip文件
             op_ipfile=op_ip
             fdhh=open(op_ipfile,"rb")
             line = fdhh.readline()
             while line:
                 line= line.strip()
                 if op_type == "all":
                     list_key.append('IPS-LOCK-web-'+line)
                     list_key.append('IPS-LOCK-pop-'+line)
                     list_key.append('IPS-LOCK-smtp-'+line)
                     list_key.append('IPS-LOCK-imap-'+line)
                 else:
                     list_key.append('IPS-LOCK-'+op_type+'-'+line)
                 line=fdhh.readline()

    else:
         if op_type == "all":
                     list_key.append('IPS-LOCK-web-'+op_ip)
                     list_key.append('IPS-LOCK-pop-'+op_ip)
                     list_key.append('IPS-LOCK-smtp-'+op_ip)
                     list_key.append('IPS-LOCK-imap-'+op_ip)
         else:
             list_key.append('IPS-LOCK-'+op_type+'-'+op_ip)

    if operation == "get" or operation == "ttl" or operation == "del" or operation == "set" :
         if operation == "set":
             if len(sys.argv) != 5:
                  useage()

        else:
             if len(sys.argv) != 4:
                 usage()
         conn_pool=redis.ConnectionPool(password=xxx ,host='172.16.183.aaaa',port=6379)
         conn_redis=redis.Redis(connection_pool=conn_pool)

   
            
         if operation == "set":
                 fun(conn_redis,operation,600,*list_key)
         else:
             print list_key
             fun(conn_redis,operation,0,*list_key)

    else:
         usage()   

#connection redis

#conn_redis=redis.Redis(host='172.16.183.xxx',port=6379,password=aaaaa)

#connection pool

#GetKey=conn_redis.get('IPS-LOCK-web-114.99.23.182')

#conn_redis.setex('IPS-LOCK-web-183.14.29.188','web-114.99.23.182 1628036',1628036)

以上是 python 操作redis 的脚本 的全部内容, 来源链接: utcz.com/z/387472.html

回到顶部