找到你要的答案

Q:Python, how do I drop the first octet in reverse zone? fun with strings

Q:Python,我怎么反区八第一滴?有趣的字符串

I need to create a reverse DNS entry for IPv4.

The ip address is 108.61.190.64

But the reverse DNS entry is = each octet in reverse order with the last octet dropped

i.e.

$ORIGIN 190.61.108.IN-ADDR.ARPA.

or

12.34.56.78 ->  78.56.34.12

Is there a Python tool for this task? [::-1] does a complete reverse and not just rearranging the octets.

P.S. I have the same reverse issue for IPv6 but no octet is to be dropped.

我需要创建一个IPv4反向DNS条目。

IP地址是108.61.190.64

但反向的DNS条目=逆序每个字节与去年八下降

$ORIGIN 190.61.108.IN-ADDR.ARPA.

12.34.56.78 ->  78.56.34.12

Is there a Python tool f或 this task? [::-1] does a complete reverse and not just rearranging the octets.

P.S. I have the same reverse issue f或 IPv6 but no octet is to be dropped.

answer1: 回答1:

I guess you can do something similar to this:

>>> ip = '12.34.56.78'
>>> reversed = '.'.join(ip.split('.')[::-1])
>>> reversed
'78.56.34.12'

And if you want to drop the last one:

>>> ip = '12.34.56.78'
>>> reversed = '.'.join(ip.split('.')[::-1][:-1])
>>> reversed
'78.56.34'

我想你可以做一些类似的事情:

>>> ip = '12.34.56.78'
>>> reversed = '.'.join(ip.split('.')[::-1])
>>> reversed
'78.56.34.12'

如果你想放弃最后一个:

>>> ip = '12.34.56.78'
>>> reversed = '.'.join(ip.split('.')[::-1][:-1])
>>> reversed
'78.56.34'
python  dns  ip  reverse