Python连接服务器在redis中新建key

#encoding:utf-8

import paramiko
import os
import time

class Oper_server:
    def __init__(self,ip):
        ssh = paramiko.SSHClient()
        # 自动添加主机秘钥
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname='xxxx.xxxxx.res', port=2222, username='username', password='123456',
                    timeout=10)
        invoke = ssh.invoke_shell()
        self.invoke=invoke
        self.invoke.send(ip + '\r')
        self.invoke.send('5\r')

    def server_command(self,*args):
        result = ""
        #逐个执行多个命令
        for i in range(len(args)):
            #print(args[i])
            self.invoke.send(args[i] + '\r')
            time.sleep(4)
        recv_ready_status = self.invoke.recv_ready()
        #print(recv_ready_status)  # 有接收状态返回True
        while recv_ready_status:
            log = self.invoke.recv(65535).decode()
            result += log
            time.sleep(0.5)
            recv_ready_status = self.invoke.recv_ready()
        print(result)
        self.invoke.close()
        return result

if __name__=="__main__":
    #ip='127.0.0.1'
    #get 请求
    #shell_command="curl -i 'wwwbaidu.com'"
    #post 请求
    #shell_command='''curl -v -H 'Content-type:applation/json' -X POST -d '{"name":"lili"}' http://127.0.0.1:8888/login'''
    ip="127.0.0.1"
    a=Oper_server(ip)
    shell_command1="redis-cli -c -h 127.0.0.1 -p 5521"
    shell_command2="set mykey myvalue"
    shell_command3="del mykey"
    a.server_command(shell_command1,shell_command2,shell_command3)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容