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

此页面是否是列表页或首页?未找到合适正文内容。

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

标签:classcodingvdaandpython reend终端functionret

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 : b4zinga@outlook.com
# 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:

作者: 番茄花园

为您推荐

返回顶部