I'm building a simple email verifier and I need to know how to turn an ip address string into ints. I need (example Ip address) [a.b.c.d] to all be their own integer so that I may see if each is in the range of 0-255. The problem is that there are dots that separate them and I don't know how to just convert the individual sections.
-
3Take a look at the output of `'1.2.3.4'.split('.')`. – Charles Duffy Jan 28 '15 at 01:19
-
I've never seen anyone use email addresses that are numeric ip addresses. Well, I'll admit to using `root@127.0.0.1` on a few sites... to sign up for their spam. Good filters will try to catch this sort of thing. – Paul Jan 28 '15 at 01:21
-
@Paul It's true that you never really see this but you still could do this, I considered this possibility before – jamylak Jan 28 '15 at 01:42
-
possible duplicate of [How to validate IP address in Python?](http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python) – JCotton Jan 28 '15 at 04:09
3 Answers
As Charles Duffy suggested in the comments, the string.split() method will split a string into several substrings, returning a list of all substrings.
If you want to split at each 'dot', then use string.split('.'), otherwise it will split at whitespace.
For example:
address = ['10.1.0.1']
address_split = address.split('.')

- 9,310
- 3
- 28
- 40
What you need is to use the split
method of string and judge every number is between [0,255]:
>>> a = '127.0.0.1'
>>> all(map(lambda a : 0 <= a <= 255, [int(i) for i in a.split('.')]))
True
>>> b = '256.1.2.3'
>>> all(map(lambda a : 0 <= a <= 255, [int(i) for i in b.split('.')]))
False
First use split() to get every part of an IP address and you get a list:
>>> a = '127.0.0.1'
>>> alist = a.split('.')
>>> alist
['127', '0', '0', '1']
Now, use list comprehension to get the number of of a alsit:
>>> num_list = [int(i) for i in alist]
>>> num_list
[127, 0, 0, 1]
I don't use try except the ValueError cause your input is IP, if not you can follow other answer.
Then, use map to judge if every number in num_list is in [0,255], it take an anonymous function and the list you want to judge. It return the bool value of every result of the element in num_list by judged the anonymous function:
>>> map(lambda num: 0 <= num <= 255, num_list)
[True, True, True, True]
Last, try all() function it will return True if all element in a list is True, otherwise False:
>>> all(map(lambda num: 0 <= num <= 255, num_list))
True

- 3,694
- 3
- 21
- 30
-
I believe this is what I need. Can you explain how each part of it works? – Crunch Jan 28 '15 at 01:45
-
I really love you for this :) Thanks for the awesome explanation as well! – Crunch Jan 28 '15 at 02:24
Building slightly on Andrew Guy's suggestion:
try:
address_octets = [int(x) for x in address.split('.')]
except ValueError, e:
pass
# fallback from the assumption that it's an IPv4 string
# (Whatever that means for your code)
assert len(address_octets) == 4
# (or use a conditional and fallback as described above)
... note that I have no idea how you'll want to handle the parsing failures and I'm not entirely sure that the only exception you could get would be ValueError
.

- 17,054
- 13
- 68
- 116