抱歉,您误会了。114.143.51.197没有PTR记录…因此
socket.gethostbyaddr()应该引发错误…您当然需要
try/
except子句,用于
socket.herror
>>> def lookup(addr):... try:... return socket.gethostbyaddr(addr)... except socket.herror:... return None, None, None...>>> name,alias,addresslist = lookup('4.2.2.2')>>> print namevnsc-bak.sys.gtei.net>>> name,alias,addresslist = lookup('114.143.51.197')>>> print nameNone>>>
DNS反向查找114.143.51.197 …请注意,它没有有效的
PTR记录
[mpenning@Bucksnort ~]$ dig @8.8.8.8 -x 114.143.51.197; <<>> DiG 9.6-ESV-R4 <<>> @8.8.8.8 -x 114.143.51.197; (1 server found);; global options: +cmd;; Got answer:;; ->>HEADER<<- oppre: QUERY, status: NXDOMAIN, id: 4735;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0;; QUESTION SECTION:;197.51.143.114.in-addr.arpa. IN PTR;; AUTHORITY SECTION:114.in-addr.arpa. 1800 IN SOA ns1.apnic.net. read-txt-record-of-zone-first-dns-admin.apnic.net. 17812 7200 1800 604800 172800;; Query time: 182 msec;; SERVER: 8.8.8.8#53(8.8.8.8);; WHEN: Tue Nov 22 05:11:36 2011;; MSG SIZE rcvd: 134[mpenning@Bucksnort ~]$ pythonPython 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)[GCC 4.3.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import socket>>> socket.gethostbyaddr('114.143.51.197')Traceback (most recent call last): File "<stdin>", line 1, in <module>socket.herror: (1, 'Unknown host')>>>
这就是有效
PTR记录的样子……
[mpenning@Bucksnort ~]$ dig -x 4.2.2.2; <<>> DiG 9.6-ESV-R4 <<>> -x 4.2.2.2;; global options: +cmd;; Got answer:;; ->>HEADER<<- oppre: QUERY, status: NOERROR, id: 61856;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 3, ADDITIONAL: 1;; QUESTION SECTION:;2.2.2.4.in-addr.arpa. IN PTR;; ANSWER SECTION:2.2.2.4.in-addr.arpa. 86400 IN PTR vnsc-bak.sys.gtei.net.;; AUTHORITY SECTION:2.4.in-addr.arpa. 86400 IN NS dnsauth2.sys.gtei.net.2.4.in-addr.arpa. 86400 IN NS dnsauth1.sys.gtei.net.2.4.in-addr.arpa. 86400 IN NS dnsauth3.sys.gtei.net.;; ADDITIonAL SECTION:dnsauth1.sys.gtei.net. 1800 IN A 4.2.49.2;; Query time: 308 msec;; SERVER: 127.0.0.1#53(127.0.0.1);; WHEN: Tue Nov 22 05:10:16 2011;; MSG SIZE rcvd: 158[mpenning@Bucksnort ~]$ pythonPython 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)[GCC 4.3.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import socket>>> socket.gethostbyaddr('4.2.2.2')('vnsc-bak.sys.gtei.net', [], ['4.2.2.2'])>>>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)