def __init__(self, cell): WPApp.__init__(self, Handler) self.Cell = cell
# strict_handler.py from webpie import WPApp, WPHandler class StrictHandler(WPHandler): _Methods = ["hello"] # 1 def password(self, realm, user): # 2 return "H3llo-W0rld" def hello(self, request, relpath): try: user, password = relpath.split("/", 1) except: return 400 # 3 if password == self.password("realm", user): return "Hello, World!\n" else: return "Unauthorized\n", 401 WPApp(StrictHandler).run_server(8080)
def __init__(self, handler_class): WPApp.__init__(self, handler_class) self.Counter = 0
def __init__(self, handler, path, prefix): WPApp.__init__(self, handler, prefix=prefix) self.DataSource = DataSource(path)
def __init__(self, mon, **args): WPApp.__init__(self, Handler, **args) self.Monitor = mon
from webpie import WPApp, WPHandler, HTTPServer, HTTPSServer class H(WPHandler): def redirect1(self, request, relpath, to=None): return "redirect", 301, {"Location": to} def redirect2(self, request, relpath, to=None): print("app: request.path_url:", request.path_url) self.redirect(to) WPApp(H).run_server(8888)
filtered.append( (s[self.ICAO24], (s[self.CALL] or "").strip(), s[self.LON], s[self.LAT], alt, s[self.TRACK], s[self.VELOCITY], s[self.VERTICAL_RATE])) #pprint.pprint(filtered) outbuf = io.StringIO() csv_writer = csv.writer(outbuf) csv_writer.writerow([timestamp]) for row in filtered: print(row) csv_writer.writerow(row) return outbuf.getvalue() data = data_csv application = app = WPApp(Handler) if __name__ == "__main__": import getopt, sys opts, args = getopt.getopt(sys.argv[1:], "p:") opts = dict(opts) port = int(opts.get("-p", 8888)) print("Running at port", port) app.run_server(port)
from webpie import WPApp def build_response(request, relpath, **args): out_list = [] items = { 'i': 200, 'd': args, 's': "string", 'b': b"bytes", 'l': ["a\n", "b\n", "c\n"], 'g': (x for x in ["aa\n", "bb\n", "cc\n"]), 't': "text/plain" } for c in (relpath or ""): out_list.append(items[c]) if len(out_list) == 1: out = out_list[0] else: out = tuple(out_list) print("returning:", out) return out WPApp(build_response).run_server(8888)
from webpie import WPApp def hello(request, relpath, who="world"): #1 my_name = relpath #2 return f"Hello, {who}! I am {my_name}\n" #3 WPApp(hello).run_server(8080)
from webpie import WPApp, WPHandler import time class MyHandler(WPHandler): def __call__(self, request, relpath, **args): return "__call__(%s, %s)" % (relpath, args) def hello_str(self, request, relpath): return "Hello, World!\n" def hello_bytes(self, request, relpath): return b"Hello, World!\n" def it_str(self, request, relpath): return ("%d = %d\n" % (x, x**2) for x in range(10)) def it_bytes(self, request, relpath): return (b"%d = %d\n" % (x, x**2) for x in range(10)) def list_bytes(self, request, relpath): return [b"%d = %d\n" % (x, x**2) for x in range(10)] WPApp(MyHandler, prefix="/a/b", replace_prefix="/x").run_server(8080)
from webpie import WPApp def echo(request, relpath): return relpath or "", "text/plain" application = WPApp(echo)
def __init__(self, root_class): WPApp.__init__(self, root_class) self.Record = []
def __init__(self, handler, home, cc_path, prefix, wm_path): WPApp.__init__(self, handler, prefix=prefix) self.CCDataSource = CCDataSource(cc_path) self.UMDataSource = UMDataSource(wm_path) self.Home = home
# hello_world.py from webpie import WPApp def hello(request, relpath): return f"Hello, World!\n" WPApp(hello).run_server(9000)
@webmethod(["read","write"]) def read_write(self, request, relpath): return "Read/write access granted\n" @webmethod(["read"]) def read_only(self, request, relpath): return "Read access granted\n" class H_open(WPHandler): def hello(self, request, relpath): return "Hello, World!\n" def wrong(self, request, relpath): return "This should never happen\n" class Top(WPHandler): def __init__(self, *params): WPHandler.__init__(self, *params) self.o = H_open(req, app, path) self.m = H_methods(req, app, path) self.d = H_decorators(req, app, path) self.p = H_permissions(req, app, path) application = WPApp(Top, strict=True) application.run_server(8080)
def __init__(self, handler, dbfilename, **args): WPApp.__init__(self, handler, **args) self.DBFile = dbfilename
# nested_handlers.py from webpie import WPApp, WPHandler import time class HelloHandler(WPHandler): #1 def hello(self, request, relpath): return "Hello, World!\n" class ClockHandler(WPHandler): #2 def time(self, request, relpath): return time.ctime() + "\n", "text/plain" #3 class TopHandler(WPHandler): def __init__(self, *params): #4 WPHandler.__init__(self, *params) self.greet = HelloHandler(*params) self.clock = ClockHandler(*params) def version(self, request, relpath): #5 return "1.0.3" WPApp(TopHandler).run_server(8080)
# static_server.py from webpie import WPApp, WPStaticHandler WPApp(WPStaticHandler).handler_options("./static", cache_ttl=300).run_server(8080)
def __init__(self, handler, home, cc_path, prefix, wm_path): WPApp.__init__(self, handler, prefix=prefix) self.DataViewer = DataViewer(cc_path) self.WMDataSource = WMDataSource(wm_path) self.Home = home
# http_server.py from webpie import HTTPServer, WPHandler, WPApp import sys, time class TimeHandler(WPHandler): def time(self, relpath, **args): # simple "what time is it?" server return time.ctime(time.time()) app = WPApp(TimeHandler) # create app object port = 8080 srv = HTTPSServer( port, app, # create HTTP server thread - subclass of threading.Thread max_connections=3, max_queued=5 # concurrency contorl ) srv.start() # start the server srv.join() # run forever
def __init__(self, root_class): WPApp.__init__(self, root_class) self.Memory = {}
# hello_world_wsgi.py from webpie import WPApp, WPHandler class Greeter(WPHandler): def hello(self, request, relpath): return "Hello, World!\n" application = WPApp(Greeter)
# hello_world_server.py from webpie import WPApp, WPHandler import time class MyHandler(WPHandler): def hello(self, request, relpath): return "Hello, World!\n" application = WPApp(MyHandler) application.run_server(8080)
# hello_world.py from webpie import WPApp, WPHandler import time class MyHandler(WPHandler): def hello(self, request, relpath): return "Hello, World!\n" def time(self, request, relpath): # 1 return time.ctime()+"\n", "text/plain" # 2 application = WPApp(MyHandler)
def __init__(self, root_class): WPApp.__init__(self, root_class) cfg = DFConfig(os.environ['DFARM_CONFIG']) self.DiskFarmClient = DiskFarmClient(cfg)
def clock(self, request, relpath, format="number"): t = time.time() if format=="number": return str(t)+"\n", "text/plain" elif format=="text": return time.ctime(t)+"\n", "text/plain" elif format == "json": return '{"clock":%f}\n' % (t,), "text/json" else: return "Unknown format %s\n" % (format,), 400 def time(self, request, relpath): # 1 t = datetime.now() if relpath == "year": return str(t.year)+"\n" elif relpath == "month": return str(t.month)+"\n" elif relpath == "day": return str(t.day)+"\n" elif relpath == "hour": return str(t.hour)+"\n" elif relpath == "minute": return str(t.minute)+"\n" elif relpath == "second": return str(t.second)+"\n" else: return str(t)+"\n" WPApp(TimeHandler).run_server(8080)
def date(self, request, relpath, part=None): # # will respond to .../date?part=(year|month|day) # today = date.today() if part == "year": return str(today.year) + "\n" elif part == "month": return str(today.month) + "\n" elif part == "day": return str(today.day) + "\n" else: return str(today) + "\n" def time(self, request, relpath): # # will respond to .../time/(hour|minute|second) # t = datetime.now() if relpath == "hour": return str(t.hour) + "\n" elif relpath == "minute": return str(t.minute) + "\n" elif relpath == "second": return str(t.second) + "\n" else: return str(t.time()) + "\n" WPApp(DateTimeHandler).run_server(8080)
def __init__(self, my_name, handler): WPApp.__init__(self, handler) self.MyName = my_name
def time(self, request, relpath): return time.ctime()+"\n", "text/plain" def time_response(self, request, relpath): return Response(time.ctime()+"\n", content_type="text/plain") def time_generator(self, request, relpath, n=5): n = int(n) return (time.sleep(1.0) or "{}: {}\n".format(i, time.ctime()) for i in range(n)) def time_headers(self, request, relpath): return "OK", { "X-Time": time.ctime(), "X-Clock": str(time.time()), "Content-type": "text/plain" } def time_json(self, request, relpath, **extra): data = {} data.update(extra) data.update( { "ctime": time.ctime(), "clock": time.time() } ) return json.dumps(data), "text/json" WPApp(MyHandler).run_server(8080)
from webpie import WPApp def env(request, relpath): out = [f"{k}: {v}\n" for k, v in request.environ.items()] return out, "text/plain" application = WPApp(env)