summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorCurtis <curtis@tinbrain.net>2015-04-11 22:50:11 +1000
committerCurtis <curtis@tinbrain.net>2015-04-11 22:50:11 +1000
commit057cfe761f24ae61520197de5cc4c80a442d4376 (patch)
tree160870c241f58c5934709057dd097b7d51062697 /examples
parentf6e5db93d8344d7f09ee5304394136d6f5cd7a38 (diff)
downloaduwsgi-057cfe761f24ae61520197de5cc4c80a442d4376.tar.gz
Tidy with flake8
Diffstat (limited to 'examples')
-rw-r--r--examples/flaskpost.py2
-rw-r--r--examples/heavytest.py8
-rw-r--r--examples/mjpeg_stream.py34
-rw-r--r--examples/multiapp.py22
-rw-r--r--examples/simple_app.py8
-rw-r--r--examples/simple_app_wsgi2.py6
-rw-r--r--examples/simple_logger.py3
-rw-r--r--examples/staticfilesnmp.py5
-rw-r--r--examples/taskqueue.py9
-rw-r--r--examples/uwsgirouter.py122
-rw-r--r--examples/uwsgirouter2.py6
-rw-r--r--examples/uwsgirouter3.py27
-rw-r--r--examples/uwsgirouter4.py17
-rw-r--r--examples/uwsgirouter5.py7
-rw-r--r--examples/uwsgistatus.py10
-rw-r--r--examples/welcome.py24
-rw-r--r--examples/welcome3.py10
17 files changed, 169 insertions, 151 deletions
diff --git a/examples/flaskpost.py b/examples/flaskpost.py
index 81206104..c36a0cf0 100644
--- a/examples/flaskpost.py
+++ b/examples/flaskpost.py
@@ -2,10 +2,12 @@ from flask import Flask, request
app = Flask(__name__)
app.debug = True
+
@app.route('/')
def hello_world():
return "<form method='POST' enctype='multipart/form-data' action='/upload'><input type='text' name='pippo'/><input type='file' name='ufile'/><input type='submit' value='upload'/></form>"
+
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
return str(len(request.form.get('pippo', 'boh'))) + request.form.get('pippo', 'boh')
diff --git a/examples/heavytest.py b/examples/heavytest.py
index bcef7b6f..26f582c1 100644
--- a/examples/heavytest.py
+++ b/examples/heavytest.py
@@ -3,12 +3,18 @@ import werkzeug.testapp
uwsgi.cache_set("/cache/get", "HTTP 1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>I am the uWSGI cache</h1>")
+
def app001(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return "PATH_INFO=%s" % env['PATH_INFO']
+
def app002(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return "requests: %d" % uwsgi.total_requests()
-uwsgi.applications = { '':werkzeug.testapp.test_app, '/app001':app001, '/app002':app002 }
+uwsgi.applications = {
+ '': werkzeug.testapp.test_app,
+ '/app001': app001,
+ '/app002': app002
+}
diff --git a/examples/mjpeg_stream.py b/examples/mjpeg_stream.py
index ae9e5474..9ed082c1 100644
--- a/examples/mjpeg_stream.py
+++ b/examples/mjpeg_stream.py
@@ -1,25 +1,23 @@
-import uwsgi
import os
-def application(env, start_response):
- boundary = 'uwsgi_mjpeg_frame'
+def application(env, start_response):
- start_response('200 Ok', [
+ boundary = 'uwsgi_mjpeg_frame'
- ('Cache-Control', 'no-cache'),
- ('Cache-Control', 'private'),
- ('Pragma', 'no-cache'),
- ('Content-Type', 'multipart/x-mixed-replace; boundary=' + boundary),
- ]
- )
+ start_response('200 Ok', [
+ ('Cache-Control', 'no-cache'),
+ ('Cache-Control', 'private'),
+ ('Pragma', 'no-cache'),
+ ('Content-Type', 'multipart/x-mixed-replace; boundary=' + boundary),
+ ])
- yield "--%s\r\n" % boundary
+ yield "--%s\r\n" % boundary
- while 1:
- yield "Content-Type: image/jpeg\r\n\r\n"
- print os.system('screencapture -t jpg -m -T 1 screenshot.jpg')
- f = open('screenshot.jpg')
- yield env['wsgi.file_wrapper'](f)
- yield "\r\n--%s\r\n" % boundary
- #os.system('./isightcapture -w 640 -h 480 screenshot.jpg')
+ while 1:
+ yield "Content-Type: image/jpeg\r\n\r\n"
+ print os.system('screencapture -t jpg -m -T 1 screenshot.jpg')
+ f = open('screenshot.jpg')
+ yield env['wsgi.file_wrapper'](f)
+ yield "\r\n--%s\r\n" % boundary
+ # os.system('./isightcapture -w 640 -h 480 screenshot.jpg')
diff --git a/examples/multiapp.py b/examples/multiapp.py
index 5b2f1125..80b06621 100644
--- a/examples/multiapp.py
+++ b/examples/multiapp.py
@@ -1,22 +1,30 @@
import werkzeug
import uwsgi
-def zero_app(e,s):
- s('200 OK', [ ('Content-Type', 'text/plain') ])
+
+def zero_app(e, s):
+ s('200 OK', [('Content-Type', 'text/plain')])
return "0"
-def not_found(e,s):
- s('404 Not Found', [ ('Content-Type', 'text/plain') ])
+
+def not_found(e, s):
+ s('404 Not Found', [('Content-Type', 'text/plain')])
return ""
def wsgi_app(e, s):
- s('200 OK', [ ('Content-Type', 'text/plain') ])
+ s('200 OK', [('Content-Type', 'text/plain')])
yield "I am a WSGI app\n"
def internal_server_error(e, s):
- s('500 Internal Server Error', [ ('Content-Type', 'text/plain') ])
+ s('500 Internal Server Error', [('Content-Type', 'text/plain')])
return ""
-uwsgi.applications = {'/wsgi':wsgi_app, '/test':werkzeug.test_app, '/zero':zero_app, '/notfound':not_found, '/ise':internal_server_error}
+uwsgi.applications = {
+ '/wsgi': wsgi_app,
+ '/test': werkzeug.test_app,
+ '/zero': zero_app,
+ '/notfound': not_found,
+ '/ise': internal_server_error
+}
diff --git a/examples/simple_app.py b/examples/simple_app.py
index d6ebb7cc..7a5afbb9 100644
--- a/examples/simple_app.py
+++ b/examples/simple_app.py
@@ -3,9 +3,11 @@ import os
print("!!! uWSGI version:", uwsgi.version)
+
def ciao():
print("modifica su /tmp")
+
def ciao2():
print("nuovo uwsgi_server")
print os.getpid()
@@ -23,17 +25,15 @@ counter = 0
uwsgi.post_fork_hook = ciao2
-def application(env, start_response):
+def application(env, start_response):
global counter
-
- #print(env)
start_response('200 Ok', [('Content-type', 'text/plain')])
yield "hello world"
yield "hello world2"
- for i in range(1,1000):
+ for i in range(1, 1000):
yield str(i)
yield "\n"
diff --git a/examples/simple_app_wsgi2.py b/examples/simple_app_wsgi2.py
index 0dffd9c5..ba176e43 100644
--- a/examples/simple_app_wsgi2.py
+++ b/examples/simple_app_wsgi2.py
@@ -1,9 +1,9 @@
def mygen(uri):
- for i in xrange(1,100):
+ for i in xrange(1, 100):
yield "ciao %s<br/>" % uri
-def application(env, start_response = None):
+def application(env, start_response=None):
return '200 OK', [('Content-Type', 'text/html')], "<h1>This is the fastest homepage of the world !!!</h1>"
- #return '200 OK', [('Content-Type', 'text/html')], mygen(env['PATH_INFO'])
+ # return '200 OK', [('Content-Type', 'text/html')], mygen(env['PATH_INFO'])
diff --git a/examples/simple_logger.py b/examples/simple_logger.py
index 49bffc95..082aca99 100644
--- a/examples/simple_logger.py
+++ b/examples/simple_logger.py
@@ -1,6 +1,7 @@
import uwsgi
+
def print_logs(ip, port, message):
- print(ip, port, message)
+ print(ip, port, message)
uwsgi.udp_callable = print_logs
diff --git a/examples/staticfilesnmp.py b/examples/staticfilesnmp.py
index 315e85c9..0eb51872 100644
--- a/examples/staticfilesnmp.py
+++ b/examples/staticfilesnmp.py
@@ -4,10 +4,11 @@ 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')
+ 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/taskqueue.py b/examples/taskqueue.py
index 121da38a..71131759 100644
--- a/examples/taskqueue.py
+++ b/examples/taskqueue.py
@@ -4,22 +4,24 @@ import uwsgi
CONSUMERS = 4
+
def consumer(q):
while True:
item = q.get()
print(item)
- #... DO A HEAVY TASK HERE ...
+ # ... DO A HEAVY TASK HERE ...
q.task_done()
+
def spawn_consumers():
global q
q = Queue.Queue()
for i in range(CONSUMERS):
- t = Thread(target=consumer,args=(q,))
+ t = Thread(target=consumer, args=(q,))
t.daemon = True
t.start()
print("consumer %d on worker %d started" % (i, uwsgi.worker_id()))
-
+
uwsgi.post_fork_hook = spawn_consumers
@@ -33,4 +35,3 @@ def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
yield "Task enqueued"
-
diff --git a/examples/uwsgirouter.py b/examples/uwsgirouter.py
index a6225373..6da6cc38 100644
--- a/examples/uwsgirouter.py
+++ b/examples/uwsgirouter.py
@@ -1,78 +1,76 @@
import uwsgi
+
def application(env, start_response):
- # open the socket
- fd = uwsgi.async_connect("192.168.173.100:3032")
+ # open the socket
+ fd = uwsgi.async_connect("192.168.173.100:3032")
- # wait for connection ready (3s timeout)
- yield uwsgi.wait_fd_write(fd, 3)
+ # wait for connection ready (3s timeout)
+ yield uwsgi.wait_fd_write(fd, 3)
- # has timed out ?
- if env['x-wsgiorg.fdevent.timeout']:
- print "connection timed out !!!"
- uwsgi.close(fd)
- raise StopIteration
+ # has timed out ?
+ if env['x-wsgiorg.fdevent.timeout']:
+ print "connection timed out !!!"
+ uwsgi.close(fd)
+ raise StopIteration
- # connection refused ?
- if not uwsgi.is_connected(fd):
- print "unable to connect"
- uwsgi.close(fd)
- raise StopIteration
-
+ # connection refused ?
+ if not uwsgi.is_connected(fd):
+ print "unable to connect"
+ uwsgi.close(fd)
+ raise StopIteration
- # send request
- # env can contains python objects, but send_message will discard them.
- # In this way we will automagically have a congruent and valid uwsgi packet
- uwsgi.async_send_message(fd, 0, 0, env)
+ # send request
+ # env can contains python objects, but send_message will discard them.
+ # In this way we will automagically have a congruent and valid uwsgi packet
+ uwsgi.async_send_message(fd, 0, 0, env)
- # send the http body
- # ready body in async mode and resend to fd
- # uwsgi.recv will use always an internal buffer of 4096, but can be limited in the number of bytes to read
+ # send the http body
+ # ready body in async mode and resend to fd
+ # uwsgi.recv will use always an internal buffer of 4096, but can be limited in the number of bytes to read
- # does this request has a body ?
- cl = uwsgi.cl()
+ # does this request has a body ?
+ cl = uwsgi.cl()
- if cl > 0:
- # get the input fd
- input = env['wsgi.input'].fileno()
+ if cl > 0:
+ # get the input fd
+ input = env['wsgi.input'].fileno()
- # read (in async mode) upto 'cl' data and send to uwsgi peer
- while cl > 0:
- bufsize = min(cl, 4096)
- yield uwsgi.wait_fd_read(input, 30)
- if env['x-wsgiorg.fdevent.timeout']:
- print "connection timed out !!!"
- uwsgi.close(fd)
- raise StopIteration
- body = uwsgi.recv(input, bufsize)
- if body:
- uwsgi.send(fd, body)
- cl = cl - len(body)
- else:
- break
-
+ # read (in async mode) upto 'cl' data and send to uwsgi peer
+ while cl > 0:
+ bufsize = min(cl, 4096)
+ yield uwsgi.wait_fd_read(input, 30)
+ if env['x-wsgiorg.fdevent.timeout']:
+ print "connection timed out !!!"
+ uwsgi.close(fd)
+ raise StopIteration
+ body = uwsgi.recv(input, bufsize)
+ if body:
+ uwsgi.send(fd, body)
+ cl = cl - len(body)
+ else:
+ break
- # wait for response (30s timeout)
- yield uwsgi.wait_fd_read(fd, 30)
+ # wait for response (30s timeout)
+ yield uwsgi.wait_fd_read(fd, 30)
- # has timed out ?
- if env['x-wsgiorg.fdevent.timeout']:
- print "connection timed out !!!"
- uwsgi.close(fd)
- raise StopIteration
+ # has timed out ?
+ if env['x-wsgiorg.fdevent.timeout']:
+ print "connection timed out !!!"
+ uwsgi.close(fd)
+ raise StopIteration
- data = uwsgi.recv(fd)
- # recv the data, if it returns None the callable will end
- while data:
- yield data
- # wait for response
- yield uwsgi.wait_fd_read(fd, 30)
- if env['x-wsgiorg.fdevent.timeout']:
- print "connection timed out !!!"
- uwsgi.close(fd)
- raise StopIteration
- data = uwsgi.recv(fd)
+ data = uwsgi.recv(fd)
+ # recv the data, if it returns None the callable will end
+ while data:
+ yield data
+ # wait for response
+ yield uwsgi.wait_fd_read(fd, 30)
+ if env['x-wsgiorg.fdevent.timeout']:
+ print "connection timed out !!!"
+ uwsgi.close(fd)
+ raise StopIteration
+ data = uwsgi.recv(fd)
- uwsgi.close(fd)
-
+ uwsgi.close(fd)
diff --git a/examples/uwsgirouter2.py b/examples/uwsgirouter2.py
index 430336b7..37ff9bcb 100644
--- a/examples/uwsgirouter2.py
+++ b/examples/uwsgirouter2.py
@@ -2,7 +2,7 @@
import uwsgi
-def application(e,s):
+def application(e, s):
- for part in uwsgi.send_message("192.168.173.100:3032", 0, 0, e, 0, e['wsgi.input'].fileno(), uwsgi.cl()):
- yield part
+ for part in uwsgi.send_message("192.168.173.100:3032", 0, 0, e, 0, e['wsgi.input'].fileno(), uwsgi.cl()):
+ yield part
diff --git a/examples/uwsgirouter3.py b/examples/uwsgirouter3.py
index a1941c1c..37b4b894 100644
--- a/examples/uwsgirouter3.py
+++ b/examples/uwsgirouter3.py
@@ -3,23 +3,24 @@ import uwsgi
current_node = 0
-def application(e,s):
- global current_node
+def application(e, s):
- nodes = uwsgi.cluster_nodes()
- print nodes
+ global current_node
- if len(nodes) == 0:
- print "no cluster node available"
- raise StopIteration
+ nodes = uwsgi.cluster_nodes()
+ print nodes
- if current_node >= len(nodes):
- current_node = 0
+ if len(nodes) == 0:
+ print "no cluster node available"
+ raise StopIteration
- node = nodes[current_node]
+ if current_node >= len(nodes):
+ current_node = 0
- for part in uwsgi.send_message(node, 0, 0, e, 0, e['wsgi.input'].fileno(), uwsgi.cl()):
- yield part
+ node = nodes[current_node]
- current_node+=1
+ for part in uwsgi.send_message(node, 0, 0, e, 0, e['wsgi.input'].fileno(), uwsgi.cl()):
+ yield part
+
+ current_node += 1
diff --git a/examples/uwsgirouter4.py b/examples/uwsgirouter4.py
index ce8e6fc4..7786ff6e 100644
--- a/examples/uwsgirouter4.py
+++ b/examples/uwsgirouter4.py
@@ -1,14 +1,15 @@
import uwsgi
-def application(e,s):
- node = uwsgi.cluster_best_node()
- print node
+def application(e, s):
- if not node:
- print "sorry node unavailable"
- raise StopIteration
+ node = uwsgi.cluster_best_node()
+ print node
- for part in uwsgi.send_message(node, 0, 0, e, 0, e['wsgi.input'].fileno(), uwsgi.cl()):
- yield part
+ if not node:
+ print "sorry node unavailable"
+ raise StopIteration
+
+ for part in uwsgi.send_message(node, 0, 0, e, 0, e['wsgi.input'].fileno(), uwsgi.cl()):
+ yield part
diff --git a/examples/uwsgirouter5.py b/examples/uwsgirouter5.py
index af27b6ba..85f4ed7f 100644
--- a/examples/uwsgirouter5.py
+++ b/examples/uwsgirouter5.py
@@ -3,7 +3,8 @@ import uwsgi
fd = uwsgi.connect("127.0.0.1:3033")
-def application(e,s):
- for part in uwsgi.send_message(fd, 0, 4, e, 30, e['wsgi.input'].fileno(), uwsgi.cl()):
- yield part
+def application(e, s):
+
+ for part in uwsgi.send_message(fd, 0, 4, e, 30, e['wsgi.input'].fileno(), uwsgi.cl()):
+ yield part
diff --git a/examples/uwsgistatus.py b/examples/uwsgistatus.py
index 3b3b0851..312ecc16 100644
--- a/examples/uwsgistatus.py
+++ b/examples/uwsgistatus.py
@@ -36,7 +36,7 @@ def application(env, start_response):
yield "<h2>Hooks</h2>"
- for h in range(0,255):
+ for h in range(0, 255):
if uwsgi.has_hook(h):
yield "%d<br/>" % h
@@ -45,15 +45,15 @@ def application(env, start_response):
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();
+ workers = uwsgi.workers()
yield '<h2>workers</h2>'
for w in workers:
- #print w
- #print w['running_time']
+ # 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>'
+ 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>'
diff --git a/examples/welcome.py b/examples/welcome.py
index 8db7af64..221d483a 100644
--- a/examples/welcome.py
+++ b/examples/welcome.py
@@ -2,7 +2,8 @@ import uwsgi
import os
import gc
import sys
-from uwsgidecorators import *
+from uwsgidecorators import rpc, signal, postfork
+
print(sys.version)
print(sys.version_info)
if 'set_debug' in gc.__dict__:
@@ -26,6 +27,7 @@ def after_request_hook():
uwsgi.after_req_hook = after_request_hook
+
@rpc('hello')
def hello_rpc(one, two, three):
arg0 = one[::-1]
@@ -33,20 +35,24 @@ def hello_rpc(one, two, three):
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_config(e, sr):
sr('200 OK', [('Content-Type', 'text/html')])
for opt in uwsgi.opt.keys():
@@ -57,11 +63,13 @@ 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())
+
def application(env, start_response):
try:
uwsgi.mule_msg(env['REQUEST_URI'], 1)
@@ -82,7 +90,6 @@ def application(env, start_response):
if env['PATH_INFO'] in routes:
return routes[env['PATH_INFO']](env, start_response)
-
if DEBUG:
print(env['wsgi.input'].fileno())
@@ -98,7 +105,7 @@ def application(env, start_response):
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 += '<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>
@@ -122,14 +129,9 @@ 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 bytes(output.encode('latin1'))
return output
-
-
-
-
-
diff --git a/examples/welcome3.py b/examples/welcome3.py
index 6f5a9d13..4d308d17 100644
--- a/examples/welcome3.py
+++ b/examples/welcome3.py
@@ -1,14 +1,17 @@
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_config(e, sr):
sr('200 OK', [('Content-Type', 'text/html')])
for opt in uwsgi.opt.keys():
@@ -20,6 +23,7 @@ routes['/xsendfile'] = xsendfile
routes['/logo'] = serve_logo
routes['/config'] = serve_config
+
def application(env, start_response):
if env['PATH_INFO'] in routes:
@@ -39,9 +43,3 @@ Configuration<br/>
""".format(version=uwsgi.version.decode('ascii'))
return bytes(body.encode('ascii'))
-
-
-
-
-
-