[Django]-How can I convert a string to ip address in python

7👍

Python has an ipaddress module which can handle this sort of thing. It was added to the standard library in Python 3, but should be installable for earlier versions, too.

>>> import ipaddress
>>> print(ipaddress.ip_address(1778952384))
IPv4Address('106.8.168.192')
>>> print(str(ipaddress.ip_address(1778952384)))
'106.8.168.192'

You may also be asking about splitting the string (on b',' since it’s a bytestring), getting the right field, and converting it to an int:

data = b'363,3,1778952384,7076'
ipaddress.ip_address(int(data.split(b',')[2]))

Leave a comment