我正在尝试编写一些代码来区域传输DNS区域,然后解析记录并将它们放入数据库中。我之前从未在python中使用过类,我认为这可以像学习如何使用它们一样好。不知道我在这里做错了什么。这是在Python 2.6版上运行的。我能得到一些指导吗?很难理解如何使用课程。
代码:
import pymysql
import time
import dns.zone
import dns.query
from pprint import pprint
from dns.exception import DNSException
from dns.rdataclass import *
from dns.rdatatype import *
a_records = {}
cname_records = {}
... # excluding db connection credentials
cur = conn.cursor()
try:
zone = dns.zone.from_xfr(dns.query.xfr(ns, domain))
print "zone xferred"
except DNSException, e:
print e.__class__, e
def main():
compile_records()
for key, value in a_records.iteritems():
sqlfoo.select_a(domain, key, value)
for key, value in cname_records.iteritems():
sqlfoo.select_cname(domain, key, value)
def compile_records():
for (name, ttl, rdata) in zone.iterate_rdatas('A'):
a_records[name.to_text()] = rdata.to_text()
for (name, ttl, rdata) in zone.iterate_rdatas('CNAME'):
cname_records[name.to_text()] = rdata.to_text()
class sqlfoo:
def __init__(self, domain, subdomain, ip, cname):
self.domain = domain
self.subdomain = subdomain
self.ip = ip
self.cname = cname
self.results = results
def select_a(self, domain, subdomain, ip):
build_select = """
SELECT domain, subdomain, cname, ip
FROM zones
WHERE domain = %s
AND subdomain = %s
AND ip = %s;
"""
select_params = [ domain, subdomain, ip ]
cur.execute(build_select, select_params)
results = cur.fetchall()
if not any(results): # if the row isn't found, add it
print domain, subdomain, ip
self.insert_a(domain, subdomain, ip)
def select_cname(self, domain, subdomain, cname):
build_select = """
SELECT domain, subdomain, cname, ip
FROM zones
WHERE domain = %s
AND subdomain = %s
AND cname = %s;
"""
select_params = [ domain, subdomain, cname ]
cur.execute(build_select, select_params)
results = cur.fetchall()
if not any(results): # if the row isn't found, add it
print domain, subdomain, cname
self.insert_cname(domain, subdomain, cname)
def insert_a(domain, subdomain, ip):
build_insert = """
INSERT INTO zones
(Id,
domain,
record_type,
subdomain,
cname,
first_seen)
VALUES
(NULL, %s, `A`, %s, %s, %s);
"""
insert_params = [ domain, subdomain, ip, current_epoch ]
cur.execute(build_insert, insert_params)
def insert_cname(domain, subdomain, ip):
build_insert = """
INSERT INTO zones
(Id,
domain,
record_type,
subdomain,
cname,
first_seen)
VALUES
(NULL, %s, `CNAME`, %s, %s, %s);
"""
insert_params = [ domain, subdomain, cname, current_epoch ]
cur.execute(build_insert, insert_params)
main()
cur.close()
conn.close()
当我运行代码时,我得到以下输出。
(venv)[user@server ]$ python zone-etl.py
zone xferred
Traceback (most recent call last):
File "zone-etl.py", line 125, in <module>
main()
File "zone-etl.py", line 42, in main
sqlfoo.select_a(domain, key, value)
TypeError: unbound method select_a() must
be called with sqlfoo instance as first argument (got str instance instead)
答案 0 :(得分:2)
您需要创建类的实例才能使用这些方法。 这是一个例子:
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
emp = Employee("Aaron", 5000)
emp.displayEmployee()
答案 1 :(得分:1)
您必须在使用之前实例化您的课程。
当您致电sqlfoo
时,您应该执行以下操作:
sqlfoo_instance = sqlfoo(domain_value, subdomain_value, ip_value, cname_value)
...
for key, value in a_records.iteritems():
sqlfoo.select_a(domain, key, value)
for key, value in cname_records.iteritems():
sqlfoo.select_cname(domain, key, value)
...
在你的情况下,我建议你先熟悉一下面向对象编程,因为你在那里做的只是创建更多的样板代码来做与使用过程式编程相同的事情。因此,您将获得OOP的好处。