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'

Edited 3 Years Ago by james.lu.75491856

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.

Edited 3 Years Ago by Gribouillis

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()
This question has already been answered. Start a new discussion instead.