summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorUnbit <info@unbit.it>2013-09-19 03:55:27 +0200
committerUnbit <info@unbit.it>2013-09-19 03:55:27 +0200
commit81d20edc4cf3415142cbee22b003019979d992b1 (patch)
tree100a0075b971d02831d315cce76462e718713bd1 /examples
parenta9fc6adccff40b0ea7c5100ba0571dce9d2b96a3 (diff)
downloaduwsgi-81d20edc4cf3415142cbee22b003019979d992b1.tar.gz
more cleanup
Diffstat (limited to 'examples')
-rw-r--r--examples/info_uwsgi.php26
-rw-r--r--examples/staticfilesnmp.py13
-rw-r--r--examples/uwsgistatus.py79
-rw-r--r--examples/welcome.py146
-rw-r--r--examples/welcome3.py57
5 files changed, 321 insertions, 0 deletions
diff --git a/examples/info_uwsgi.php b/examples/info_uwsgi.php
new file mode 100644
index 00000000..4158d3c2
--- /dev/null
+++ b/examples/info_uwsgi.php
@@ -0,0 +1,26 @@
+uWSGI version <b><?=uwsgi_version()?></b><br/>
+workerd id: <b><?=uwsgi_worker_id()?></b><br/>
+master pid: <b><?=uwsgi_masterpid()?></b><br/>
+
+uri: <b><?= $_SERVER['REQUEST_URI'] ?></b><br/>
+docroot: <b><?= $_SERVER['DOCUMENT_ROOT'] ?></b><br/>
+PATH_INFO: <b><?= $_SERVER['PATH_INFO'] ?></b><br/>
+
+<? uwsgi_signal(17) ?>
+
+<? uwsgi_setprocname("test test test"); ?>
+
+rpc result:<br/>
+<? echo uwsgi_rpc("", "hello", "one", "two", "three"); ?>
+
+<?
+ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+ echo uwsgi_cache_update('foobar', $_POST['cache_val']);
+ }
+
+?>
+cache value: <?= uwsgi_cache_get('foobar') ?><br/>
+<form method="POST">
+ <input type="text" name="cache_val" value="<?=uwsgi_cache_get('foobar')?>"/>
+ <input type="submit" value="cache set" />
+</form>
diff --git a/examples/staticfilesnmp.py b/examples/staticfilesnmp.py
new file mode 100644
index 00000000..315e85c9
--- /dev/null
+++ b/examples/staticfilesnmp.py
@@ -0,0 +1,13 @@
+import uwsgi
+from os import path
+
+uwsgi.snmp_set_counter64(1, 0) # Number of requests
+uwsgi.snmp_set_counter64(2, 0) # Number of bytes
+
+def application(environ, start_response):
+ size = path.getsize('logo_uWSGI.png')
+ start_response('200 OK', [('Content-Type', 'image/png'), ('Content-Length', str(size))] )
+ fd = open('logo_uWSGI.png','r')
+ uwsgi.snmp_incr_counter64(1)
+ uwsgi.snmp_incr_counter64(2, size)
+ return environ['wsgi.file_wrapper'](fd, 4096)
diff --git a/examples/uwsgistatus.py b/examples/uwsgistatus.py
new file mode 100644
index 00000000..f04dee7f
--- /dev/null
+++ b/examples/uwsgistatus.py
@@ -0,0 +1,79 @@
+import uwsgi
+
+import time
+import sys
+import os
+
+
+def application(env, start_response):
+ print env
+ start_response('200 OK', [('Content-Type', 'text/html')])
+
+ yield '<h1>uWSGI %s status</h1>' % uwsgi.version
+ yield 'masterpid: <b>' + str(uwsgi.masterpid()) + '</b><br/>'
+
+ yield 'started on: <b>' + time.ctime(uwsgi.started_on) + '</b><br/>'
+
+ yield 'buffer size: <b>' + str(uwsgi.buffer_size) + '</b><br/>'
+
+ yield 'total_requests: <b>' + str(uwsgi.total_requests()) + '</b><br/>'
+
+ yield 'log size: <b>' + str(uwsgi.logsize()) + '</b><br/>'
+
+ yield 'workers: <b>' + str(uwsgi.numproc) + '</b><br/>'
+
+ yield "cwd: <b>%s</b><br/>" % os.getcwd()
+
+ try:
+ yield "mode: <b>%s</b><br/>" % uwsgi.mode
+ except:
+ pass
+
+ try:
+ yield "pidfile: <b>%s</b><br/>" % uwsgi.pidfile
+ except:
+ pass
+
+ yield "<h2>Hooks</h2>"
+
+ for h in range(0,255):
+ if uwsgi.has_hook(h):
+ yield "%d<br/>" % h
+
+ yield '<h2>dynamic options</h2>'
+
+ yield '<b>logging</b>: ' + str(uwsgi.get_option(0)) + '<br/>'
+ yield '<b>max_requests</b>: ' + str(uwsgi.getoption(1)) + '<br/>'
+ yield '<b>socket_timeout</b>: ' + str(uwsgi.getoption(2)) + '<br/>'
+ yield '<b>memory_debug</b>: ' + str(uwsgi.getoption(3)) + '<br/>'
+ yield '<b>master_interval</b>: ' + str(uwsgi.getoption(4)) + '<br/>'
+ yield '<b>harakiri</b>: ' + str(uwsgi.getoption(5)) + '<br/>'
+ yield '<b>cgi_mode</b>: ' + str(uwsgi.get_option(6)) + '<br/>'
+ yield '<b>threads</b>: ' + str(uwsgi.get_option(7)) + '<br/>'
+ yield '<b>process_reaper</b>: ' + str(uwsgi.get_option(8)) + '<br/>'
+
+ yield '<table border="1">'
+ yield '<th>worker id</th><th>pid</th><th>in request</th><th>requests</th><th>running time</th><th>address space</th><th>rss</th>'
+
+ workers = uwsgi.workers();
+
+ yield '<h2>workers</h2>'
+
+ for w in workers:
+ #print w
+ #print w['running_time']
+ if w is not None:
+ yield '<tr><td>'+ str(w['id']) +'</td><td>' + str(w['pid']) + '</td><td>' + str(w['pid']) + '</td><td>' + str(w['requests']) + '</td><td>' + str(w['running_time']) + '</td><td>' + str(w['vsz']) + '</td><td>' + str(w['rss']) + '</td></tr>'
+ print w
+
+ yield '</table>'
+
+ yield "<h2>PYTHONPATH</h2>"
+
+ yield "<ul>"
+ for p in sys.path:
+ yield "<li>%s</li>" % p
+
+ yield "</ul>"
+
+ yield "<i>%s</i>" % str(os.uname())
diff --git a/examples/welcome.py b/examples/welcome.py
new file mode 100644
index 00000000..dc898292
--- /dev/null
+++ b/examples/welcome.py
@@ -0,0 +1,146 @@
+import uwsgi
+import os
+import gc
+import sys
+from uwsgidecorators import *
+print(sys.version)
+print(sys.version_info)
+if 'set_debug' in gc.__dict__:
+ gc.set_debug(gc.DEBUG_SAVEALL)
+
+print(os.environ)
+print(sys.modules)
+print(sys.argv)
+
+try:
+ if sys.argv[1] == 'debug':
+ DEBUG = True
+ else:
+ raise
+except:
+ DEBUG = False
+
+
+def after_request_hook():
+ print("request finished")
+
+uwsgi.after_req_hook = after_request_hook
+
+@rpc('hello')
+def hello_rpc(one, two, three):
+ arg0 = one[::-1]
+ arg1 = two[::-1]
+ arg2 = three[::-1]
+ return "!%s-%s-%s!" % (arg1, arg2, arg0)
+
+@signal(17)
+def ciao_mondo(signum):
+ print("Hello World")
+
+def xsendfile(e, sr):
+ sr('200 OK', [('Content-Type', 'image/png'), ('X-Sendfile', os.path.abspath('logo_uWSGI.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 ''
+
+def serve_options(e, sr):
+ sr('200 OK', [('Content-Type', 'text/html')])
+ for opt in range(0,256):
+ yield "<b>%d</b> = %d<br/>" % (opt, uwsgi.get_option(opt))
+
+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])
+
+routes = {}
+routes['/xsendfile'] = xsendfile
+routes['/logo'] = serve_logo
+routes['/config'] = serve_config
+routes['/options'] = serve_options
+
+@postfork
+def setprocname():
+ if uwsgi.worker_id() > 0:
+ uwsgi.setprocname("i am the worker %d" % uwsgi.worker_id())
+
+def application(env, start_response):
+ try:
+ uwsgi.mule_msg(env['REQUEST_URI'], 1)
+ except:
+ pass
+
+ req = uwsgi.workers()[uwsgi.worker_id()-1]['requests']
+
+ uwsgi.setprocname("worker %d managed %d requests" % (uwsgi.worker_id(), req))
+
+ try:
+ gc.collect(2)
+ except:
+ pass
+ if DEBUG:
+ print(env['wsgi.input'].fileno())
+
+ if env['PATH_INFO'] in routes:
+ return routes[env['PATH_INFO']](env, start_response)
+
+
+ if DEBUG:
+ print(env['wsgi.input'].fileno())
+
+ try:
+ gc.collect(2)
+ except:
+ pass
+
+ if DEBUG:
+ print(len(gc.get_objects()))
+
+ 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>'
+ 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)
+
+ output = """
+<img src="/logo"/> version %s running on %s (remote user: %s)<br/>
+<hr size="1"/>
+
+Configuration<br/>
+<iframe src="/config"></iframe><br/>
+
+<br/>
+
+Dynamic options<br/>
+<iframe src="/options"></iframe><br/>
+
+<br/>
+Workers and applications<br/>
+<table border="1">
+<tr>
+<th>wid</th><th>pid</th><th>status</th><th>running time</th><th>average</th><th>tx</th><th>apps</th>
+</tr>
+%s
+</table>
+
+ """ % (uwsgi.version, uwsgi.hostname, env.get('REMOTE_USER','None'), workers)
+
+ start_response('200 OK', [('Content-Type', 'text/html'), ('Content-Length', str(len(output)) )])
+
+ #return bytes(output.encode('latin1'))
+ return output
+
+
+
+
+
diff --git a/examples/welcome3.py b/examples/welcome3.py
new file mode 100644
index 00000000..5b3608e1
--- /dev/null
+++ b/examples/welcome3.py
@@ -0,0 +1,57 @@
+import uwsgi
+import os
+
+def xsendfile(e, sr):
+ sr('200 OK', [('Content-Type', 'image/png'), ('X-Sendfile', os.path.abspath('logo_uWSGI.png'))])
+ return b''
+
+def serve_logo(e, sr):
+ sr('200 OK', [('Content-Type', 'image/png')])
+ return uwsgi.sendfile('logo_uWSGI.png')
+
+def serve_options(e, sr):
+ sr('200 OK', [('Content-Type', 'text/html')])
+ for opt in range(0,256):
+ body = "{opt} = {optvalue}<br/>".format(opt=opt, optvalue=uwsgi.get_option(opt))
+ yield bytes(body.encode('ascii'))
+
+def serve_config(e, sr):
+ 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'))
+
+routes = {}
+routes['/xsendfile'] = xsendfile
+routes['/logo'] = serve_logo
+routes['/config'] = serve_config
+routes['/options'] = serve_options
+
+def application(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')])
+
+ body = """
+<img src="/logo"/> version {version}<br/>
+<hr size="1"/>
+
+Configuration<br/>
+<iframe src="/config"></iframe><br/>
+
+<br/>
+
+Dynamic options<br/>
+<iframe src="/options"></iframe><br/>
+
+ """.format(version=uwsgi.version.decode('ascii'))
+
+ return bytes(body.encode('ascii'))
+
+
+
+
+
+