By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

ipv6 validation

P: n/a
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

Mar 18 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <11**********************@g10g2000cwb.googlegroups .com>,
ji**********@kctdata.cz wrote:
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:
socket.has_ipv6 True socket.inet_pton(socket.AF_INET6, "8001::1244") '\x80\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x12D' socket.inet_pton(socket.AF_INET6, "8001:xyz:1244") Traceback (most recent call last):
File "<stdin>", line 1, in ?
socket.error: illegal IP address string passed to inet_pton


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).
Mar 18 '06 #2

P: n/a
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

Mar 30 '06 #3

P: n/a
On 30 Mar 2006 11:40:08 -0800, rumours say that ji**********@kctdata.cz
might have written:
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."
--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
Apr 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.