Python类继承,缺少属性

时间:2014-10-30 01:57:32

标签: python

我正在学习Python,在我的学习之路上的Python脚本是运行远程SSH命令 脚本本身运行正常,但是当我想要实验类继承功能时它会中断。

原始脚本pyssh.py:

#!/usr/bin/env python
import sys
import socket
import paramiko
#=================================
# Class: PySSH
#=================================
class PySSH:


    def __init__ (self):
        self.ssh = None
        self.transport = None  

    def disconnect (self):
        if self.transport is not None:
           self.transport.close()
        if self.ssh is not None:
           self.ssh.close()

    def connect(self,hostname,username,password,port=22):
        self.hostname = hostname
        self.username = username
        self.password = password

        self.ssh = paramiko.SSHClient()
        #Don't use host key auto add policy for production servers
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.load_system_host_keys()
        try:
            self.ssh.connect(hostname,port,username,password)
            self.transport=self.ssh.get_transport()
        except (socket.error,paramiko.AuthenticationException) as message:
            print "ERROR: SSH connection to "+self.hostname+" failed: " +str(message)
            sys.exit(1)
        return  self.transport is not None

    def runcmd(self,cmd,sudoenabled=False):
        if sudoenabled:
            fullcmd="echo " + self.password + " |   sudo -S -p '' " + cmd
        else:
            fullcmd=cmd
        if self.transport is None:
            return "ERROR: connection was not established"
        session=self.transport.open_session()
        session.set_combine_stderr(True)
        #print "fullcmd ==== "+fullcmd
        if sudoenabled:
            session.get_pty()
        session.exec_command(fullcmd)
        stdout = session.makefile('rb', -1)
        #print stdout.read()
        output=stdout.read()
        session.close()
        return output

#===========================================
# MAIN
#===========================================        
if __name__ == '__main__':
    hostname = 'server1'
    username = 'admin'
    password = 'Password123'
    ssh = PySSH()
    ssh.connect(hostname,username,password)
    output=ssh.runcmd('date')
    print output
    output=ssh.runcmd('service sshd status',True)
    print output
    ssh.disconnect()

现在我想通过拧新脚本pyssh2.sh

来测试类继承
#!/usr/bin/env python
from pyssh import PySSH
class PySSHT(PySSH):
    def connect(self,hostname):
        self.ssh = PySSH()
        self.username = 'admin'
        self.password = 'Password1'
        self.ssh.connect(hostname,self.username,self.password)

host = 'server1'
ssh = PySSHT()
ssh.connect(host)
output=ssh.runcmd('date')
print output
output=ssh.runcmd('service sshd status',True)
print output
ssh.disconnect()

它给出了这个错误,看起来在ssh.connect()中分配的self.transport的值在runcmd()中丢失了。是否有一些我遗失的简单的类继承规则?

[root@server  ~]# ./pyssh2.py
ERROR: connection was not established
ERROR: connection was not established
Traceback (most recent call last):
  File "./pyssh2.py", line 17, in <module>
    ssh.disconnect()
  File "/root/pyssh.py", line 19, in disconnect
    self.ssh.close()
AttributeError: PySSH instance has no attribute 'close

1 个答案:

答案 0 :(得分:0)

看起来你的问题在这里:

class PySSHT(PySSH):
    def connect(self,hostname):
        self.ssh = PySSH()   # <=== problem here?
        self.username = 'admin'
        self.password = 'Password1'
        self.ssh.connect(hostname,self.username,self.password)

在第一个代码块中,您将self.ssh设置为paramiko.SSHClient() 在第二个块(上面)中,您将其设置为PySSH

我假设paramiko.SSHClient有一个.close方法。 PySSH没有。

你不应该把自己设置为PySSH我不认为。 更好的方法是这样的:

class PySSHT(PySSH):
   def connect(self, hostname):
       PySSH.connect(self, hostname = hostname, username = 'admin', password = 'Password1')
       return

此外,您应该阅读旧样式与Python中的新样式类。 PySSH应该声明为:

class PySSH(object):