给定本地侧IP地址(Ip):u'1.1.1.1/32‘#unicode格式
如何获得远程侧ip?(将为1.1.1.2)
逻辑:
如果本地ip为偶数,则远程ip将为本地ip +1。
否则,本地ip -1
我试着做这样的事:
ip_temp = int(ip.replace('/32','').split('.')[-1])
if ip_temp % 2 == 0:
remote = ip + 1
else:
remote = ip - 1
remote_ip = <replace last octet with remote>我查看了ipaddress模块,但是找不到任何有用的东西。
发布于 2015-12-01 01:27:27
大多数Python实用程序要求远程IP是一个带有字符串和端口号(整数)的元组。例如:
import socket
address = ('127.0.0.1', 10000)
sock.connect(address)对于您的情况,您拥有所需的大部分逻辑。但是,您需要确定如何处理X.0和X.255的情况。
您想要做的全部代码是:
ip = '1.1.1.1/32'
# Note Drop the cidr notation as it is not necessary for addressing in python
ip_temp = ip.split('/')[0]
ip_temp = ip_temp.split('.')
# Note this does not handle the edge conditions and only modifies the last octet
if int(ip_temp[-1]) % 2 == 0:
remote = int(ip_temp[-1]) + 1
else:
remote = int(ip_temp[-1]) -1
remote_ip = ".".join(ip_temp[:3]) + "." + str(remote)https://stackoverflow.com/questions/34010923
复制相似问题