[英]How to call a python class function from another file
I have this class in my parser.py file我的 parser.py 文件中有这个 class
class HostInfo(object):
def __init__(self, host_id):
self.osclass = []
self.osmatch = []
self.osfingerprint = []
self.portused = []
self.ports = []
self.extraports = []
self.tcpsequence = {}
self.hostnames = []
self.tcptssequence = {}
self.ipidsequence = {}
self.trace = {'port': '', 'proto': '', 'hop': []}
self.status = {}
self.address = []
self.hostscript = []
# Umit extension
self.id = host_id
self.comment = ''
# XXX this structure it not being used yet.
self.nmap_host = {
'status': {'state': '', 'reason': ''},
'smurf': {'responses': ''},
'times': {'to': '', 'srtt': '', 'rttvar': ''},
'hostscript': [],
'distance': {'value': ''},
'trace': {'port': '', 'proto': '', 'hop': []},
'address': [],
'hostnames': [],
'ports': [],
'uptime': {'seconds': '', 'lastboot': ''},
'tcpsequence': {'index': '', 'values': '', 'class': ''},
'tcptssequence': {'values': '', 'class': ''},
'ipidsequence': {'values': '', 'class': ''},
'os': {}
}
after that it defined a function which trying to find an host id from a xml file之后它定义了一个 function 试图从 xml 文件中查找主机 ID
def get_id(self):
try:
return self._id
except AttributeError:
raise Exception("Id is not set yet.")
def set_id(self, host_id):
try:
self._id = int(host_id)
except (TypeError, ValueError):
raise Exception("Invalid id! It must represent an integer, "
"received %r" % host_id)
Now i want to use call this get_id
function from an another file.I tried so many time but it shows an error ie module can't be import现在我想从另一个文件中调用这个get_id
function。我试了很多次,但它显示一个错误,即模块无法导入
from parser import HostInfo
obj = HostInfo(<whatever host_id you need here>)
obj.get_id
this is the way, how are you actually doing it?就是这样,你实际上是怎么做的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.