ipv6 validation

J

jiri.juranek

Hello,
is there any common function for validation if string contains valid ip
address(both ipv4 and ipv6)? Or does sb wrote some regular expression
for this?
thanks
J
 
R

Roy Smith

Hello,
is there any common function for validation if string contains valid ip
address(both ipv4 and ipv6)? Or does sb wrote some regular expression
for this?
thanks
J

Look at socket.inet_pton(). First check to make sure ipv6 is supported on
your platform, then pass your string to inet_pton() inside of a try block
to catch socket.error. It would have been nicer is a more specific
exception was thrown, but this seems to work. For example:
Traceback (most recent call last):

Be aware that IPv6 support is messy on Windows. For example, if you're
running Win 2003 (or XP, I would guess), the OS does support IPv6 (and thus
socket.has_ipv6 will probably bet set to True) but the IPv6 libraries don't
actually get loaded until you configure an IPv6 address on some interface.
This means things like inet_pton() will fail, which is truly bletcherous
and evil.

Writing a regex to recognize valid IPv6 presentation strings is not
trivial. Keep in mind that you're allowed exactly 0 or 1 "::" occurrances,
and things like "ffff::192.168.11.1" are legal (I don't remember if I got
the semantics right there, but the syntax is legal).
 
J

jiri.juranek

thanks a lot for this solution.
Next thing: how may i find out that that address is multicast one? is
there some easy possibility or i have to use regex now?

thanks
 
C

Christos Georgiou

thanks a lot for this solution.
Next thing: how may i find out that that address is multicast one? is
there some easy possibility or i have to use regex now?

To quote a Google reply:

"IPv6 multicast addresses are distinguished from unicast addresses by the
value of the high-order octet of the addresses: a value of 0xFF (binary
11111111) identifies an address as a multicast address; any other value
identifies an address as a unicast address."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,493
Members
48,160
Latest member
KieranKisc

Latest Threads

Top