import webapp2
import os
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.write( """
<html><head><title>hello</title>
<body>
<form action="/passworded" method="post">
<div><label>password</label><input type="password" id="password" name="password" />
<div><input type="submit"></div>
</form>
</body>
</html>
""");
class Passworded(webapp2.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
parameters = self.request.params # works for both GET and POST
if parameters and parameters[ 'password' ] == 'MyPassword':
directories , files = self.listing_as_directories_and_files(os.getcwd())
for directory in directories:
self.response.write('{} \n'.format(directory))
self.response.write('\n')
for file in files:
self.response.write('{} \n'.format(file))
else:
self.response.write('Passworded')
def listing_as_directories_and_files(self, start):
directory_list = list()
file_list = list()
for current in os.listdir(start):
if(current[0] != '.'): # do not add hidden directories or hidden files
fullPath = os.path.join(start, current)
if(os.path.isdir(fullPath)):
directory_list.append(current)
if( os.path.isfile(fullPath)):
file_list.append(current)
return directory_list , file_list
@staticmethod
def filesystem_listing(start, full_path=False, startswith=None, endswith=None, contains=None,
directories_only=False, files_only=False):
# flist=[os.path.join(start, current) for current in os.listdir(start) if current.endswith(".txt")]
# isfile(), check for empty file? os.stat(), symlink? os.lstat() returns os.path.basename?
results = list()
for current in os.listdir(start):
if startswith and not current.startswith(startswith):
continue
if endswith and not current.endswith(endswith):
continue
if contains and not contains in current:
continue
path = os.path.join(start, current)
if directories_only and not os.path.isdir(path):
continue
if files_only and not os.path.isfile(path):
continue
if full_path:
results.append(path)
else:
results.append(current)
return results
routes = [
('/', MainHandler),
('/passworded', Passworded),
]
config = dict()
config['template.dir'] = 'templates/'
config['assets.dir'] = 'assets/'
config[ 'webapp2_extras.sessions' ] = {'secret_key': 'my-super-secret-key'}
application = webapp2.WSGIApplication(routes=routes, debug=True, config=config)
- - -
import os
import sys
if len(sys.argv) < 2:
print('error: requires another argument: python {} somedirectory'.format(sys.argv[0]))
exit(1)
listing = os.listdir(sys.argv[1])
for i in sorted(listing):
if os.path.isdir(i):
print i
# TODO: if( element[0] != '.' ): # do not add hidden directories