Conversion from IP string to integer, and backward in Python

后端 未结 11 884
囚心锁ツ
囚心锁ツ 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 23:11

    You lose the left-zero-padding which breaks decoding of your string.

    Here's a working function:

    def inttoip(ip):
        return socket.inet_ntoa(hex(ip)[2:].zfill(8).decode('hex'))
    

提交回复
热议问题