Skip to content

Commit

Permalink
Add IPv6 support.
Browse files Browse the repository at this point in the history
Authored by mordervomubel ( https://github.com/mordervomubel )
GitHub issue webpy/webpy webpy#70
  • Loading branch information
zamabe committed Dec 12, 2012
1 parent ae36fdf commit 7348edd
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion web/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

__all__ = [
"validipaddr", "validipport", "validip", "validaddr",
"validipaddr", "validip6addr", "validipport", "validip", "validaddr",
"urlquote",
"httpdate", "parsehttpdate",
"htmlquote", "htmlunquote", "websafe",
Expand All @@ -13,6 +13,28 @@
import urllib, time
try: import datetime
except ImportError: pass
import re
import socket

def validip6addr(address):
"""
Returns True if `address` is a valid IPv6 address.
>>> validip6addr('::')
True
>>> validip6addr('aaaa:bbbb:cccc:dddd::1')
True
>>> validip6addr('1:2:3:4:5:6:7:8:9:10')
False
>>> validip6addr('12:10')
False
"""
try:
socket.inet_pton(socket.AF_INET6, address)
except socket.error:
return False

return True

def validipaddr(address):
"""
Expand Down Expand Up @@ -59,6 +81,18 @@ def validip(ip, defaultaddr="0.0.0.0", defaultport=8080):
addr = defaultaddr
port = defaultport

#Matt Boswell's code to check for ipv6 first
match = re.search(r'^\[([^]]+)\](?::(\d+))?$',ip) #check for [ipv6]:port
if match:
if validip6addr(match.group(1)):
if match.group(2):
if validipport(match.group(2)): return (match.group(1),int(match.group(2)))
else:
return (match.group(1),port)
else:
if validip6addr(ip): return (ip,port)
#end ipv6 code

ip = ip.split(":", 1)
if len(ip) == 1:
if not ip[0]:
Expand Down

0 comments on commit 7348edd

Please sign in to comment.