import os
import web


### Url mappings

urls = [
    '/', 'index',
    '/.*','notfound'
]
for root, dirs, files in os.walk("C:/Users/James/Desktop/server"):
    for f in files:
        u = os.path.join(root,f)
        u=u.strip("/").strip("\\")
        ident=u.lstrip("C:/Users/James/Desktop/server").replace("/","slash").replace("\\","SLASH").replace(".","DOT")
        globals()[ident] = open(u,mode='rb').read()
        exec("""
class %(classname)s:
    def GET(self):
        global %(data)s
        return %(data)s
        """%{"classname":ident+"server","data":ident}, globals(), globals()) #create a class as if were created in the global scope by passing in globals() as locals
        urls.extend((u,ident+"server"))
urls = tuple(urls)
class index:
    def GET(self):
        return "<!DOCTYPE html><html><link rel=\"icon\" href=\"favicon.ico\" type=\"image/x-icon\"/>Hi!</html>"
class favicon:
    def GET(self):
        return open('favicon.ico',mode='rb').read()
class notfound:
    def GET(self):
        return "<!DOCTYPE html><html><link rel=\"icon\" href=\"favicon.ico\" type=\"image/x-icon\"/><b><h1>404 Not Found</h1></b><hr/>Python Server"
print urls
app = web.application(urls, globals())

if __name__ == '__main__':
    app.run()


Traceback (most recent call last):
  File "C:\Users\James\Desktop\server\server.py", line 37, in <module>
    app.run()
  File "C:\Python27\lib\site-packages\web\application.py", line 313, in run
    return wsgi.runwsgi(self.wsgifunc(*middleware))
  File "C:\Python27\lib\site-packages\web\wsgi.py", line 55, in runwsgi
    server_addr = validip(listget(sys.argv, 1, ''))
  File "C:\Python27\lib\site-packages\web\net.py", line 108, in validip
    if validip6addr(ip): return (ip,port)
  File "C:\Python27\lib\site-packages\web\net.py", line 33, in validip6addr
    socket.inet_pton(socket.AF_INET6, address)
AttributeError: 'module' object has no attribute 'inet_pton'

The auto generation seems to beworks correctly. I'm using python 2.7;web.py works for other simple programs.
Full session in interpreter:

('/', 'index', 'C:/Users/James/Desktop/server\\favicon.ico', 'SLASHfaviconDOTicoserver', 'C:/Users/James/Desktop/server\\server.py', 'SLASHserverDOTpyserver')

Traceback (most recent call last):
  File "C:\Users\James\Desktop\server\server.py", line 37, in <module>
    app.run()
  File "C:\Python27\lib\site-packages\web\application.py", line 313, in run
    return wsgi.runwsgi(self.wsgifunc(*middleware))
  File "C:\Python27\lib\site-packages\web\wsgi.py", line 55, in runwsgi
    server_addr = validip(listget(sys.argv, 1, ''))
  File "C:\Python27\lib\site-packages\web\net.py", line 108, in validip
    if validip6addr(ip): return (ip,port)
  File "C:\Python27\lib\site-packages\web\net.py", line 33, in validip6addr
    socket.inet_pton(socket.AF_INET6, address)
AttributeError: 'module' object has no attribute 'inet_pton'
>>> dir()
['SLASHfaviconDOTico', 'SLASHfaviconDOTicoserver', 'SLASHserverDOTpy', 'SLASHserverDOTpyserver', '__builtins__', '__doc__', '__name__', '__package__', 'app', 'dirs', 'f', 'favicon', 'files', 'ident', 'index', 'notfound', 'os', 'root', 'u', 'urls', 'web']
>>> SLASHserverDOTpyserver().GET() #test 
'import os\r\nimport web\r\n\r\n\r\n### Url mappings\r\n\r\nurls = [\r\n    \'/\', \'index\',\r\n]\r\nfor root, dirs, files in os.walk("C:/Users/James/Desktop/server"):\r\n    for f in files:\r\n        u = os.path.join(root,f)\r\n        u=u.strip("/").strip("\\\\")\r\n        ident=u.lstrip("C:/Users/James/Desktop/server").replace("/","slash").replace("\\\\","SLASH").replace(".","DOT")\r\n        globals()[ident] = open(u,mode=\'rb\').read()\r\n        exec("""\r\nclass %(classname)s:\r\n    def GET(self):\r\n        global %(data)s\r\n        return %(data)s\r\n        """%{"classname":ident+"server","data":ident}, globals(), globals())\r\n        urls.extend((u,ident+"server"))\r\nurls = tuple(urls)\r\nclass index:\r\n    def GET(self):\r\n        return "<!DOCTYPE html><html><link rel=\\"icon\\" href=\\"favicon.ico\\" type=\\"image/x-icon\\"/>Hi!</html>"\r\nclass favicon:\r\n    def GET(self):\r\n        return open(\'favicon.ico\',mode=\'rb\').read()\r\nclass notfound:\r\n    def GET(self):\r\n        return "<!DOCTYPE html><html><link rel=\\"icon\\" href=\\"favicon.ico\\" type=\\"image/x-icon\\"/><b><h1>404 Not Found</h1></b><hr/>Python Server"\r\nprint urls\r\napp = web.application(urls, globals())\r\n\r\nif __name__ == \'__main__\':\r\n    app.run()\r\n'

Dani AI

Generated

Short summary: the crash happens because web.net.py calls socket.inet_pton unconditionally, and many Windows/Python 2.7 builds do not provide that function. As pointed out, catching the failure stops the immediate AttributeError; a slightly more robust approach is to prefer inet_pton when present and fall back to a cross‑platform check (getaddrinfo) when it is not. 's later edits (ignoring server scripts and fixing path separators) are sensible safeguards but do not address the underlying validation call in web.py.

A compact, cross‑platform replacement for an IPv6 literal test is shown below. It avoids a bare except and uses getaddrinfo as a fallback when inet_pton is absent:

import socket

def valid_ipv6_literal(address):
    if hasattr(socket, 'inet_pton'):
        try:
            socket.inet_pton(socket.AF_INET6, address)
            return True
        except (socket.error, ValueError):
            return False
    # Fallback for Windows / older Pythons: ask the resolver for AF_INET6 info
    try:
        socket.getaddrinfo(address, None, socket.AF_INET6)
        return True
    except socket.gaierror:
        return False

Other practical notes: avoid generating a separate class per file with exec — use a single catch‑all handler that maps request path to a sanitized filesystem path, set Content-Type via the mimetypes module and web.header, and never serve sensitive files (server.py, .pyc, config files). If modifying library code is undesirable, an immediate workaround is to run web.py with an explicit IPv4 bind (e.g. "127.0.0.1:8080") or upgrade to a Python/web.py version that contains the inet_pton compatibility fixes.

Recommended Answers

All 6 Replies

Use startpage ! Here is a related discussion. You should try and modify net.py according to the last suggestion.

line 32 to line 35, do change like this:

try:
    socket.inet_pton(socket.AF_INET6, address)
except:
    return False

replace socket.inet_pton(socket.AF_INET6, address) by the previous snippet to catch the exception.

Thanks, and now all I need to do know is to ignore server.py and web.py

I also have fixed the backward slash thing, it used to serve with forward slashes, now it does with backward, chrome converts it.

Full working program:

import os
import web


### Url mappings

urls = [
    '/', 'index',
]
for root, dirs, files in os.walk("C:/Users/James/Desktop/server"):
    if "web" in dirs:
    dirs.remove("web")
    for f in files:
        u = os.path.join(root,f)
        u=u.strip("/").strip("\\")
        ident=u.lstrip("C:/Users/James/Desktop/server").replace("/","slash").replace("\\","SLASH").replace(".","DOT") #replace invalid python expressions with valid ones
        globals()[ident] = open(u,mode='rb').read()
        exec("""
class %(classname)s:
    def GET(self):
        global %(data)s
        return %(data)s
        """%{"classname":ident+"server","data":ident}, globals(), globals())  #create a class as if were created in the global scope by passing in globals() as locals
        urls.extend((u.lstrip("C:/Users/James/Desktop/server").replace("\\","/"),ident+"server")) #use u not ident because ident has stuff like SLASH and DOT
urls = tuple(urls)
class index:
    def GET(self):
        return "<!DOCTYPE html><html><link rel=\"icon\" href=\"favicon.ico\" type=\"image/x-icon\"/>Hi!</html>"
class favicon:
    def GET(self):
        return open('favicon.ico',mode='rb').read() 
class notfound:
    def GET(self):
        return "<!DOCTYPE html><html><link rel=\"icon\" href=\"favicon.ico\" type=\"image/x-icon\"/><b><h1>404 Not Found</h1></b><hr/>Python Server"
print urls
app = web.application(urls, globals())

if __name__ == '__main__':
    app.run()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.