2017-11-11 120 views
2

我是Python的新手,嘗試使用類繼承,並且一直未能圍繞共享變量進行包裝。我有兩個班,到目前爲止,ScanPingPython類的繼承和使用變量

scan.py

class Scan(object): 
    """ Super class for scans """ 
    identifier = str(random.getrandbits(128)) 
    timestamp = int(time.time()) 
    results_dir = "/tmp/{}/".format(identifier) 
    total_hosts = 0 

    def __init__(self, target_hosts=None, target_ports=None): 
     self.__target_hosts = target_hosts 
     self.__target_ports = target_ports 
     self.scan_string = "-sT -O --script auth,vuln" 

    @property 
    def target_hosts(self): 
     return self.__target_hosts 

    @target_hosts.setter 
    def target_hosts(self, hosts): 
     """ Sets target hosts for scan """ 
     """ Nmap Expects to be single-spaced '1 2 3' separated """ 
     self.__target_hosts = hosts.replace(", ", " ") 

ping.py

import nmap 
from .scan import Scan 

class Ping(Scan): 
    """ Ping sweep """ 

    def __init__(self, ping_string, hosts): 
     super(Scan, self).__init__() 
     self.ping_string = ping_string 
     self.hosts = hosts 

在我的腳本,幾乎調用了一切,我試圖:

from models.scan import Scan 
from models.ping import Ping 

s = Scan() 
hosts = "192.168.0.0/24" 
s.target_hosts = hosts 
pinger = Ping(ping_string, s.target_hosts) 

此行沒有任何意義,我...如果平安從掃描繼承,爲什麼當我打電話s.targets_hosts做到這一點唯一的工作?我不能從Ping這個類別撥打target_hosts,例如Ping.target_hosts

+0

在你的最後一個問題,你的意思是「叫'target_hosts'從'Ping' *** ***例如像'P =平(); p.target_hosts '?」 ''不同於'。 ' - 兩個都是有效的,但是非常不同。 – jedwards

+0

對象實例和類有區別...請閱讀更多。 s是類Scan的一個實例。您通過s = Scan()啓動它。您也可以使用Scan類作爲單例對象,但不依賴於__init__方法。 –

回答

0

什麼可能使這很難理解是,這是一個奇怪的例子。在您的示例中,實例所需的hosts參數的正確輸入需要來自只能從Ping(或其父代Scan)的實例訪問的屬性。

任何具有self作爲參數的方法(或屬性)都依賴於需要首先創建的該類的特定實例。如果有staticmethodclassmethod,它們將直接從課程中調用。

您只能獲取和設置target_hosts從類(在這種情況下,無論是ScanPing)的具體實例。如果您撥打Scan.target_hostsPing.target_hosts,它將返回類似<property at 0x51cd188>的內容。這基本上是從班級返回一個不可用的功能。它的意思是,「班級詞典中包含有關如何從<class>的實例中返回一些有用內容的說明。」

如果您創建Ping或Scan的實例,您現在可以訪問您的target_hosts屬性。

>>> scan = Scan() 
>>> scan.target_hosts = 'host1, host2, host3' 
>>> scan.target_hosts 
'host1 host2 host3' 
>>> ping = Ping('stuff', 'nonsense') 
>>> ping.hosts 
'nonsense' 
>>> ping.target_hosts = 'host4, host5, host6' 
>>> ping.target_hosts 
'host4 host5 host6' 

您可以使用虛擬Ping實例運行腳本。這應該工作。

from models.scan import Scan 
from models.ping import Ping 

dummy = Ping('ignore', 'this') 
hosts = "192.168.0.0/24" 
dummy.target_hosts = hosts 
pinger = Ping(ping_string, dummy.target_hosts) 

或者,如果ScanstaticmethodPing可以使用它。

class Scan(object): 
    """ Super class for scans """ 
    identifier = str(random.getrandbits(128)) 
    timestamp = int(time.time()) 
    results_dir = "/tmp/{}/".format(identifier) 
    total_hosts = 0 

    def __init__(self, target_hosts=None, target_ports=None): 
     self.__target_hosts = target_hosts 
     self.__target_ports = target_ports 
     self.scan_string = "-sT -O --script auth,vuln" 

    @staticmethod 
    def prep_hosts(hosts): 
     return hosts.replace(", ", " ") 

    ... 

然後

from models.scan import Scan 
from models.ping import Ping 

hosts = "192.168.0.0/24" 
input_hosts = Ping.prep_hosts(hosts) # or Scan.prep_hosts(hosts) 
pinger = Ping(ping_string, input_hosts)