Skip to content
This repository
  • 3 commits
  • 1 file changed
  • 1 comment
  • 3 contributors
Dec 12, 2012
zamabe Add IPv6 support.
Authored by mordervomubel ( https://github.com/mordervomubel )
GitHub issue webpy/webpy #70
7348edd
Matthew of Boswell Added doctest for validip and validaddr 411908a
Dec 13, 2012
Aaron Swartz Merge pull request #196 from mordervomubel/master
ipv6 doctest for validip and validaddr
703404e

Showing 1 changed file with 53 additions and 2 deletions. Show diff stats Hide diff stats

  1. 55  web/net.py
55  web/net.py
@@ -4,7 +4,7 @@
4 4
 """
5 5
 
6 6
 __all__ = [
7  
-  "validipaddr", "validipport", "validip", "validaddr", 
  7
+  "validipaddr", "validip6addr", "validipport", "validip", "validaddr",
8 8
   "urlquote",
9 9
   "httpdate", "parsehttpdate", 
10 10
   "htmlquote", "htmlunquote", "websafe",
@@ -13,6 +13,28 @@
13 13
 import urllib, time
14 14
 try: import datetime
15 15
 except ImportError: pass
  16
+import re
  17
+import socket
  18
+
  19
+def validip6addr(address):
  20
+    """
  21
+    Returns True if `address` is a valid IPv6 address.
  22
+
  23
+        >>> validip6addr('::')
  24
+        True
  25
+        >>> validip6addr('aaaa:bbbb:cccc:dddd::1')
  26
+        True
  27
+        >>> validip6addr('1:2:3:4:5:6:7:8:9:10')
  28
+        False
  29
+        >>> validip6addr('12:10')
  30
+        False
  31
+    """
  32
+    try:
  33
+        socket.inet_pton(socket.AF_INET6, address)
  34
+    except socket.error:
  35
+        return False
  36
+
  37
+    return True
16 38
 
17 39
 def validipaddr(address):
18 40
     """
@@ -55,10 +77,37 @@ def validipport(port):
55 77
     return True
56 78
 
57 79
 def validip(ip, defaultaddr="0.0.0.0", defaultport=8080):
58  
-    """Returns `(ip_address, port)` from string `ip_addr_port`"""
  80
+    """
  81
+    Returns `(ip_address, port)` from string `ip_addr_port`
  82
+    >>> validip('1.2.3.4')
  83
+    ('1.2.3.4', 8080)
  84
+    >>> validip('80')
  85
+    ('0.0.0.0', 80)
  86
+    >>> validip('192.168.0.1:85')
  87
+    ('192.168.0.1', 85)
  88
+    >>> validip('::')
  89
+    ('::', 8080)
  90
+    >>> validip('[::]:88')
  91
+    ('::', 88)
  92
+    >>> validip('[::1]:80')
  93
+    ('::1', 80)
  94
+
  95
+    """
59 96
     addr = defaultaddr
60 97
     port = defaultport
61 98
     
  99
+    #Matt Boswell's code to check for ipv6 first
  100
+    match = re.search(r'^\[([^]]+)\](?::(\d+))?$',ip) #check for [ipv6]:port
  101
+    if match:
  102
+        if validip6addr(match.group(1)):
  103
+            if match.group(2):
  104
+                if validipport(match.group(2)): return (match.group(1),int(match.group(2)))
  105
+            else:
  106
+                return (match.group(1),port)
  107
+    else:
  108
+        if validip6addr(ip): return (ip,port)
  109
+    #end ipv6 code
  110
+
62 111
     ip = ip.split(":", 1)
63 112
     if len(ip) == 1:
64 113
         if not ip[0]:
@@ -90,6 +139,8 @@ def validaddr(string_):
90 139
         ('127.0.0.1', 8080)
91 140
         >>> validaddr('127.0.0.1:8000')
92 141
         ('127.0.0.1', 8000)
  142
+        >>> validip('[::1]:80')
  143
+        ('::1', 80)
93 144
         >>> validaddr('fff')
94 145
         Traceback (most recent call last):
95 146
             ...

Showing you all comments on commits in this comparison.

Eslizn

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

Something went wrong with that request. Please try again.