Permalink
Browse files

Add IPv6 support.

Authored by mordervomubel ( https://github.com/mordervomubel )
GitHub issue webpy/webpy #70
  • Loading branch information...
1 parent ae36fdf commit 7348edd7834ae3d51e0fb59b58931f947343fde0 zamabe committed Dec 12, 2012
Showing with 35 additions and 1 deletion.
  1. +35 −1 web/net.py
View
@@ -4,7 +4,7 @@
"""
__all__ = [
- "validipaddr", "validipport", "validip", "validaddr",
+ "validipaddr", "validip6addr", "validipport", "validip", "validaddr",
"urlquote",
"httpdate", "parsehttpdate",
"htmlquote", "htmlunquote", "websafe",
@@ -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):
"""
@@ -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]:

1 comment on commit 7348edd

eslizn commented on 7348edd Jan 26, 2013

Traceback (most recent call last):
File "boot.py", line 15, in
app.run()
File "D:\PHPnow\htdocs\web\application.py", line 313, in run
return wsgi.runwsgi(self.wsgifunc(*middleware))
File "D:\PHPnow\htdocs\web\wsgi.py", line 55, in runwsgi
server_addr = validip(listget(sys.argv, 1, ''))
File "D:\PHPnow\htdocs\web\net.py", line 108, in validip
if validip6addr(ip): return (ip,port)
File "D:\PHPnow\htdocs\web\net.py", line 33, in validip6addr
socket.inet_pton(socket.AF_INET6, address)
AttributeError: 'module' object has no attribute 'inet_pton'

?

python 2.7.3 web.py 0.35

Please sign in to comment.