Validate IPV4 address

This is the place for queries that don't fit in any of the other categories.

Validate IPV4 address

Postby gplayersv » Wed Jan 29, 2014 10:19 am

Hi,

A while back I was looking for a function to check if a string is a valid IPV4 address. After googling I found this to be a good solution:

Code: Select all
def valid_ip(address):
    try:
        socket.inet_aton(address)
        return True
    except:
        return False


Recently I was playing with it and I found that it returns True for strings which don't look like ipv4 addresses:
Code: Select all
>>> import socket
>>> socket.inet_aton("100.300")
'd\x00\x01,'
>>> socket.inet_aton("100.9030443")
'd\x89\xcb+'
>>> socket.inet_aton("888888899")
'4\xfb^C'


Can somebody explain what's happening?
User avatar
gplayersv
 
Posts: 15
Joined: Mon Apr 15, 2013 10:56 am

Re: Validate IPV4 address

Postby hrs » Wed Jan 29, 2014 12:01 pm

According to the documentation "inet_aton() also accepts strings with less than three dots; see the Unix manual page inet(3) for details". This man page talks about Classful networks that I know nothing about. It appears to be some legacy stuff.

An alternative might be
Code: Select all
>>> socket.inet_pton(socket.AF_INET, '1.2.3.4')
'\x01\x02\x03\x04'
>>> socket.inet_pton(socket.AF_INET, '1.2.3')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: illegal IP address string passed to inet_pton


Though the general consensus of the pros around here is to use the twisted module for networking.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Validate IPV4 address

Postby micseydel » Wed Jan 29, 2014 7:07 pm

Twisted would be a huge dependency if you're just trying to valid an IP address without doing any fancy networking. I found this with a quick Google search, and it includes a regex for validating IP addresses. Otherwise I would imagine it would be easy to roll your own, split on dots, convert to ints and check a range. Here's what I'd probably do
Code: Select all
def is_valid_ip4(ip):
    octets = ip.split(".")
    if len(octets) != 4:
        return False
    return all(0 <= int(octet) <= 255 for octet in octets)

There might be more restrictions you are aware of about IPs that I'm not, so this might be incomplete. But I imagine it's nearly complete. Also, it appears there's a built-in way to do it in Python 3.4 and forward but I doubt that's what you're using.
Last edited by micseydel on Wed Jan 29, 2014 10:28 pm, edited 1 time in total.
Reason: Fixed octet misspelling, thanks stranac.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1113
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Validate IPV4 address

Postby gplayersv » Wed Jan 29, 2014 7:31 pm

Thanks for the answers guys!
I understand why inet_aton() behaves as it does now. There's no socket.inet_pton() on windows though so that's not good.
No need for twisted module for what I'm doing but good to know about it.
And I had no idea python had those all()/any() functions :D
User avatar
gplayersv
 
Posts: 15
Joined: Mon Apr 15, 2013 10:56 am

Re: Validate IPV4 address

Postby micseydel » Wed Jan 29, 2014 7:37 pm

Yeah, it's pretty sweet being able to pass a generator to all(). It short circuits as necessary, is clear, reads like English, etc. Glad we could help!
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1113
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Validate IPV4 address

Postby XndrK » Wed Jan 29, 2014 7:38 pm

I'm not familiar with the all() function either, actually. Need to look that up.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 169
Joined: Sat Jun 15, 2013 7:57 pm

Re: Validate IPV4 address

Postby stranac » Wed Jan 29, 2014 10:27 pm

You only spelled octet correctly once in that code, mic.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: Validate IPV4 address

Postby micseydel » Wed Jan 29, 2014 10:28 pm

:oops:
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1113
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests