阿里云ODPS做IP段转换的python UDF

单个IP转成十进制

from odps.udf import annotate@annotate(“string->bigint”)class ip2int(object):defevaluate(self, ip):try:returnreduce(lambda x, y:(x << 8) + y,map(int, ip.split(‘.’)))except:return 0“

一个IP段的网络地址和广播地址转成十进制

“`

from odps.udf import annotateimport ipaddress@annotate(“string->string”)class ipnet2range(object):defevaluate(self, ip):try:net=ipaddress.ip_network(ip)returnstr(int(net.network_address)) +’,’+ str(int(net.broadcast_address))except:ipaddr=ipaddress.IPv4Interface(ip)net=ipaddr.networkreturnstr(int(net.network_address)) +’,’ + str(int(net.broadcast_address))

“`

参考
1.https://help.aliyun.com/document_detail/97075.html?spm=a2c4g.11186623.2.21.7c582098UhsRjh#concept-q25-ygl-5fb
2. https://help.aliyun.com/document_detail/97075.html?spm=a2c4g.11186623.4.4.47804662WwU7Py