简体   繁体   English

使用python相反地读取IP地址

[英]read an ip address conversely using python

I want to write a short script using python that read my ip address conversely , so when I write 127.0.0.1 I should find as a result 1.0.0.127 .我想使用 python 编写一个简短的脚本,反过来读取我的 ip 地址,所以当我写127.0.0.1我应该找到结果1.0.0.127 any help please任何帮助请

Try this尝试这个

ip = '127.0.0.1'

ip = ip.split('.')

ip.reverse()

print('.'.join(ip))

If you want to preserve the original ip.如果要保留原来的ip。 it's very easy since strings are immutable in python assign it to a new variable and just call reversed on it instead of calling the list's reverse() so you don't alter the list also (if you want that)这很容易,因为字符串在 python 中是不可变的,将它分配给一个新变量,只需在它上面调用reversed而不是调用列表的reverse()所以你也不要改变列表(如果你想要的话)

ip = '127.0.0.1'

new = ip.split('.')

new = reversed(new)

print('.'.join(new))

print(ip)

You can use this :你可以使用这个:

def reverseIP(ip):
    ip = ip.split(".")
    ip.reverse()
    return '.'.join(ip)

Example :例子 :

print(reverseIP("127.0.0.1")) # Prints 1.0.0.127

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM