Conversion from IP string to integer, and backward in Python

后端 未结 11 883
囚心锁ツ
囚心锁ツ 2020-12-07 22:34

i have a little problem with my script, where i need to convert ip in form \'xxx.xxx.xxx.xxx\' to integer representation and go back from this form.

def ipto         


        
11条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 22:57

    Python3 oneliner (based on Thomas Webber's Python2 answer):

    sum([int(x) << 8*i for i,x in enumerate(reversed(ip.split('.')))])
    

    Left shifts are much faster than pow().

提交回复
热议问题