import os
import web
import traceback
import datetime
from functools import wraps
### Url mappings
urls = [
'/', 'index',
]
def addfolderhandler(f,root):
u = os.path.join(root,f)
u=u.strip("/").strip("\\")
ident=u.lstrip("C:/Users/James/Desktop/it").replace("/","SLASH").replace("\\","SLASH").replace(".","DOT").strip() #replace invalid python expressions with valid ones
globals()[ident] = "<h1>Directory listing for:%s</h1><hr>"%u+''.join(["<a href="+i+">"+i.lstrip(root)+"</a><br/>" for i in os.listdir(u)])+"<i>Python Server</i>"
if ident=="":
return
try:
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/it").replace("\\","/"),ident+"server")) #use u not ident because ident has stuff like SLASH and DOT
except:
print"WARNING:Cannot Make Folder Handler for:"+ident
def addfilehandler(f,root):
u = os.path.join(root,f)
u=u.strip("/").strip("\\")
ident=u.lstrip("C:/Users/James/Desktop/it").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/it").replace("\\","/"),ident+"server")) #use u not ident because ident has stuff like SLASH and DOT
def init():
global urls
for root, dirs, files in os.walk("C:/Users/James/Desktop/it"):
if "web" in dirs:
dirs.remove("web")
for f in dirs:
addfolderhandler(f,root)
for f in files:
if "server.py" in f:
continue
elif "maintence.py" in f:
continue
elif "log.txt" in f:
continue
addfilehandler(f,root)
urls = tuple(urls)
init()
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()
def notfound():
return web.notfound("<!DOCTYPE html><html><link rel=\"icon\" href=\"favicon.ico\" type=\"image/x-icon\"/><b><h1>404 Not Found</h1></b><hr/>Python Server</html>")
app = web.application(urls, globals())
app.notfound = notfound
if __name__ == '__main__':
app.run()
While executing, the nothingness variable encounters a error with exec.
When I printed the variable, I got a newline.
I got nothing for the variable type.