From ccdb5b444a644c3941e1618cbae194ddb65de1f4 Mon Sep 17 00:00:00 2001 From: dev <dev@welterde.de> Date: Wed, 26 Mar 2008 18:19:41 +0000 Subject: [PATCH] added netdb-serving-site --- netdb.i2p2/app.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 netdb.i2p2/app.py diff --git a/netdb.i2p2/app.py b/netdb.i2p2/app.py new file mode 100644 index 000000000..b7efe6fbb --- /dev/null +++ b/netdb.i2p2/app.py @@ -0,0 +1,56 @@ +from werkzeug import BaseRequest, BaseResponse, ETagResponseMixin, escape, run_simple, SharedDataMiddleware +from werkzeug.exceptions import HTTPException +import os +import sha +from time import time +from random import choice + +class Request(BaseRequest): + """Useful subclass of the default request that knows how to build urls.""" + + def __init__(self, environ): + BaseRequest.__init__(self, environ) + + +class Response(BaseResponse, ETagResponseMixin): + """Subclass of base response that has a default mimetype of text/html.""" + default_mimetype = 'text/html' + + +def app(environ, start_response): + """The WSGI application that connects all together.""" + req = Request(environ) + path = req.path[1:].lower() + if path == '': + # page + page = u'<html><head><title>NetDB</title></head><body><ul>%s</ul></body></html>' + + # generate links + entries = os.listdir('netdb') + if len(entries) > 150: + # select some randomly + new = [] + for i in range(100): + while True: + sel = choice(entries) + if sel not in new: + new.append(sel) + break + entries = new + res = '' + for entry in entries: + res += '<li><a href="%s">%s</a></li>' % (entry, entry) + resp = Response(page % res, mimetype='text/html') + elif path == 'robots.txt': + dat = u"User-agent: *\nDisallow: /routerInfo-*.dat$\n" + resp = Response(dat, mimetype='text/plain') + else: + # load file + f = open(path, 'rb') + resp = Response(f.read(), mimetype='application/octet-stream') + f.close() + resp.add_etag() + return resp(environ, start_response) + +if __name__ == '__main__': + run_simple('localhost', 5007, app) -- GitLab