diff options
author | Daniel Holth <dholth@fastmail.fm> | 2020-06-06 15:26:14 -0400 |
---|---|---|
committer | Daniel Holth <dholth@fastmail.fm> | 2020-06-06 15:26:14 -0400 |
commit | cdea4fb24b074b9b3cc52eb2cb13907cbb793750 (patch) | |
tree | 7a654694b2a0a2ed430bfebbdcec25ed92b61572 /examples | |
parent | a1e1d2234ef3be81cc40d7f0451f25ef47b8d9d9 (diff) | |
download | uwsgi-cdea4fb24b074b9b3cc52eb2cb13907cbb793750.tar.gz |
update welcome.py, welcome3.py examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/welcome.py | 99 | ||||
-rw-r--r-- | examples/welcome3.py | 47 |
2 files changed, 98 insertions, 48 deletions
diff --git a/examples/welcome.py b/examples/welcome.py index 5aa4ca88..ea1ccbfb 100644 --- a/examples/welcome.py +++ b/examples/welcome.py @@ -4,9 +4,13 @@ import gc import sys from uwsgidecorators import rpc, signal, postfork +from os.path import abspath, dirname, join + +logo_png = abspath(join(dirname(__file__), "../logo_uWSGI.png")) + print(sys.version) print(sys.version_info) -if 'set_debug' in gc.__dict__: +if "set_debug" in gc.__dict__: gc.set_debug(gc.DEBUG_SAVEALL) print(os.environ) @@ -14,7 +18,7 @@ print(sys.modules) print(sys.argv) try: - DEBUG = sys.argv[1] == 'debug' + DEBUG = sys.argv[1] == "debug" except IndexError: DEBUG = False @@ -22,10 +26,11 @@ except IndexError: def after_request_hook(): print("request finished") + uwsgi.after_req_hook = after_request_hook -@rpc('hello') +@rpc(b"hello") def hello_rpc(one, two, three): arg0 = one[::-1] arg1 = two[::-1] @@ -39,56 +44,65 @@ def ciao_mondo(signum): def xsendfile(e, sr): - sr('200 OK', [('Content-Type', 'image/png'), ('X-Sendfile', os.path.abspath('logo_uWSGI.png'))]) - return '' + sr( + "200 OK", [("Content-Type", "image/png"), ("X-Sendfile", logo_png),], + ) + return [] def serve_logo(e, sr): # use raw facilities (status will not be set...) - uwsgi.send("%s 200 OK\r\nContent-Type: image/png\r\n\r\n" % e['SERVER_PROTOCOL']) - uwsgi.sendfile('logo_uWSGI.png') - return '' + uwsgi.send( + b"%s 200 OK\r\nContent-Type: image/png\r\n\r\n" + % e["SERVER_PROTOCOL"].encode("latin1") + ) + uwsgi.sendfile(logo_png) + return [] def serve_config(e, sr): - sr('200 OK', [('Content-Type', 'text/html')]) - for opt in uwsgi.opt.keys(): - yield "<b>%s</b> = %s<br/>" % (opt, uwsgi.opt[opt]) + sr("200 OK", [("Content-Type", "text/html")]) + for key in uwsgi.opt.keys(): + opt = uwsgi.opt[key] + if not isinstance(opt, bytes): + opt = str(opt).encode("utf-8") + yield b"<b>%s</b> = %s<br/>" % (key, opt) + routes = {} -routes['/xsendfile'] = xsendfile -routes['/logo'] = serve_logo -routes['/config'] = serve_config +routes["/xsendfile"] = xsendfile +routes["/logo"] = serve_logo +routes["/config"] = serve_config @postfork def setprocname(): if uwsgi.worker_id() > 0: - uwsgi.setprocname("i am the worker %d" % uwsgi.worker_id()) + uwsgi.setprocname(b"i am the worker %d" % uwsgi.worker_id()) def application(env, start_response): try: - uwsgi.mule_msg(env['REQUEST_URI'], 1) + uwsgi.mule_msg(env["REQUEST_URI"], 1) except Exception: pass - req = uwsgi.workers()[uwsgi.worker_id()-1]['requests'] + req = uwsgi.workers()[uwsgi.worker_id() - 1]["requests"] - uwsgi.setprocname("worker %d managed %d requests" % (uwsgi.worker_id(), req)) + uwsgi.setprocname(b"worker %d managed %d requests" % (uwsgi.worker_id(), req)) try: gc.collect(2) except Exception: pass if DEBUG: - print(env['wsgi.input'].fileno()) + print(env["wsgi.input"].fileno()) - if env['PATH_INFO'] in routes: - return routes[env['PATH_INFO']](env, start_response) + if env["PATH_INFO"] in routes: + return routes[env["PATH_INFO"]](env, start_response) if DEBUG: - print(env['wsgi.input'].fileno()) + print(env["wsgi.input"].fileno()) try: gc.collect(2) @@ -98,24 +112,37 @@ def application(env, start_response): if DEBUG: print(len(gc.get_objects())) - workers = '' + workers = "" for w in uwsgi.workers(): apps = '<table border="1"><tr><th>id</th><th>mountpoint</th><th>startup time</th><th>requests</th></tr>' - for app in w['apps']: - apps += '<tr><td>%d</td><td>%s</td><td>%d</td><td>%d</td></tr>' % (app['id'], app['mountpoint'], app['startup_time'], app['requests']) - apps += '</table>' + for app in w["apps"]: + apps += "<tr><td>%d</td><td>%s</td><td>%d</td><td>%d</td></tr>" % ( + app["id"], + app["mountpoint"], + app["startup_time"], + app["requests"], + ) + apps += "</table>" workers += """ <tr> <td>%d</td><td>%d</td><td>%s</td><td>%d</td><td>%d</td><td>%d</td><td>%s</td> </tr> - """ % (w['id'], w['pid'], w['status'], w['running_time']/1000, w['avg_rt']/1000, w['tx'], apps) + """ % ( + w["id"], + w["pid"], + w["status"], + w["running_time"] / 1000, + w["avg_rt"] / 1000, + w["tx"], + apps, + ) output = """ -<img src="/logo"/> version %s running on %s (remote user: %s)<br/> +<img src="{script_name}/logo"/> version %s running on %s (remote user: %s)<br/> <hr size="1"/> Configuration<br/> -<iframe src="/config"></iframe><br/> +<iframe src="{script_name}/config"></iframe><br/> <br/> Workers and applications<br/> @@ -126,9 +153,15 @@ Workers and applications<br/> %s </table> - """ % (uwsgi.version, uwsgi.hostname, env.get('REMOTE_USER', 'None'), workers) + """ % ( + uwsgi.version, + uwsgi.hostname, + env.get("REMOTE_USER", "None"), + workers, + ) - start_response('200 OK', [('Content-Type', 'text/html'), ('Content-Length', str(len(output)))]) + start_response( + "200 OK", [("Content-Type", "text/html"), ("Content-Length", str(len(output)))] + ) - # return bytes(output.encode('latin1')) - return output + return [output.format(script_name=env["SCRIPT_NAME"]).encode("utf-8")] diff --git a/examples/welcome3.py b/examples/welcome3.py index 4d308d17..6273ec89 100644 --- a/examples/welcome3.py +++ b/examples/welcome3.py @@ -1,35 +1,50 @@ import uwsgi import os +from os.path import abspath, dirname, join + +logo_png = abspath(join(dirname(__file__), "../logo_uWSGI.png")) + def xsendfile(e, sr): - sr('200 OK', [('Content-Type', 'image/png'), ('X-Sendfile', os.path.abspath('logo_uWSGI.png'))]) - return b'' + sr( + "200 OK", [("Content-Type", "image/png"), ("X-Sendfile", logo_png),], + ) + return b"" def serve_logo(e, sr): - sr('200 OK', [('Content-Type', 'image/png')]) - return uwsgi.sendfile('logo_uWSGI.png') + sr("200 OK", [("Content-Type", "image/png")]) + return uwsgi.sendfile(logo_png) def serve_config(e, sr): - sr('200 OK', [('Content-Type', 'text/html')]) + sr("200 OK", [("Content-Type", "text/html")]) for opt in uwsgi.opt.keys(): - body = "{opt} = {optvalue}<br/>".format(opt=opt, optvalue=uwsgi.opt[opt].decode('ascii')) - yield bytes(body.encode('ascii')) + + def decode_if_bytes(val): + if isinstance(val, bytes): + return val.decode("ascii") + return val + + body = "{opt} = {optvalue}<br/>".format( + opt=opt.decode("ascii"), optvalue=decode_if_bytes(uwsgi.opt[opt]) + ) + yield bytes(body.encode("ascii")) + routes = {} -routes['/xsendfile'] = xsendfile -routes['/logo'] = serve_logo -routes['/config'] = serve_config +routes["/xsendfile"] = xsendfile +routes["/logo"] = serve_logo +routes["/config"] = serve_config def application(env, start_response): - if env['PATH_INFO'] in routes: - return routes[env['PATH_INFO']](env, start_response) + if env["PATH_INFO"] in routes: + return routes[env["PATH_INFO"]](env, start_response) - start_response('200 OK', [('Content-Type', 'text/html')]) + start_response("200 OK", [("Content-Type", "text/html")]) body = """ <img src="/logo"/> version {version}<br/> @@ -40,6 +55,8 @@ Configuration<br/> <br/> - """.format(version=uwsgi.version.decode('ascii')) + """.format( + version=uwsgi.version + ) - return bytes(body.encode('ascii')) + return [bytes(body.encode("ascii"))] |