1. 程式人生 > >redis-cli.py python redis-cli redis管理終端

redis-cli.py python redis-cli redis管理終端

def 本機 fault ket mark mini decode out down

Python redis-cli.py Python3 redis-cli 命令行管理工具

由於最近測試redis未授權訪問漏洞,發現本機沒有安裝redis,不能運行redis-cli,於是自己寫了一個簡單的redis-cli終端,功能並不完整,只是支持簡單的登陸等操作。

mini redis-cli tools

經過我的測試,執行簡單的命令還是可以的。

代碼已經上傳到github https://github.com/b4zinga/PythonTools/blob/master/redis-cli.py

# !/usr/bin/env python
# coding  : utf-8
# Date    : 2018-03-20 13:45:00
# Author : b4zinga # Email : [email protected] # Function: mini redis-cli tools import socket import sys import re socket.setdefaulttimeout(3) def sendCommand(host, port): sock = socket.socket() try: sock.connect((host,port)) except Exception as err: print(err) sys.exit(0
) while True: cmd = input(‘>‘) if cmd == ‘exit‘: break cmd = makeCmd(cmd) try: sock.send(cmd.encode()) while True: recv = sock.recv(1024) print(handleRecv(recv)) if len(recv)<
1024: # 循環接收1024, 如果長度小於1024則默認後面已經無內容,break break except Exception as err: print(err) sock.close() def makeCmd(cmd): command = "*" cmd = cmd.split() command = command + str(len(cmd)) + \r\n for c in cmd: command = command + ‘$‘ + str(len(c)) + \r\n + c + \r\n return command def handleRecv(recvdate): recvdate = recvdate.decode() if recvdate.startswith(‘*‘): recvdate=recvdate[2:].strip(\r\n) recvdate = re.sub(‘\$\d+\\r\\n‘, ‘‘, recvdate) return recvdate if __name__ == ‘__main__‘: usage=""" ==================================================== mini redis-cli tools. Usage: redis-cli.py <IP> <port> Default ip 127.0.0.1 Default port 6379 Example: redis-cli.py 127.0.0.1 6379 redis-cli.py 127.0.0.1 \n redis-cli.py 192.168.1.133 6378 ==================================================== """ if ‘-h‘ in sys.argv or ‘--help‘ in sys.argv or len(sys.argv) > 3: print(usage) sys.exit(0) if len(sys.argv) == 1: sys.argv.append(‘127.0.0.1‘) if len(sys.argv) == 2: sys.argv.append(6379) sendCommand(sys.argv[1], int(sys.argv[2]))

redis-cli.py python redis-cli redis管理終端