diff options
author | Roberto De Ioris <roberto@unbit.it> | 2012-12-28 14:31:29 +0100 |
---|---|---|
committer | Roberto De Ioris <roberto@unbit.it> | 2012-12-28 14:31:29 +0100 |
commit | cf5794e22c21b45b764359762752141ef038b90d (patch) | |
tree | 034d29f31cb3cb8cc5f3ca4c3f5c5fbae6e4227a /examples | |
parent | 1f1008c415e4b5f7e14426bc3b59207b65276c03 (diff) | |
download | uwsgi-cf5794e22c21b45b764359762752141ef038b90d.tar.gz |
moved uwsgirouter*.py to examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/uwsgirouter.py | 78 | ||||
-rw-r--r-- | examples/uwsgirouter2.py | 8 | ||||
-rw-r--r-- | examples/uwsgirouter3.py | 25 | ||||
-rw-r--r-- | examples/uwsgirouter4.py | 14 | ||||
-rw-r--r-- | examples/uwsgirouter5.py | 9 |
5 files changed, 134 insertions, 0 deletions
diff --git a/examples/uwsgirouter.py b/examples/uwsgirouter.py new file mode 100644 index 00000000..a6225373 --- /dev/null +++ b/examples/uwsgirouter.py @@ -0,0 +1,78 @@ +import uwsgi + +def application(env, start_response): + + # 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) + + # 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 + + + # 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 + + # does this request has a body ? + cl = uwsgi.cl() + + 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 + + + # 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 + + 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) + diff --git a/examples/uwsgirouter2.py b/examples/uwsgirouter2.py new file mode 100644 index 00000000..430336b7 --- /dev/null +++ b/examples/uwsgirouter2.py @@ -0,0 +1,8 @@ + +import uwsgi + + +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 diff --git a/examples/uwsgirouter3.py b/examples/uwsgirouter3.py new file mode 100644 index 00000000..a1941c1c --- /dev/null +++ b/examples/uwsgirouter3.py @@ -0,0 +1,25 @@ + +import uwsgi + +current_node = 0 + +def application(e,s): + + global current_node + + nodes = uwsgi.cluster_nodes() + print nodes + + if len(nodes) == 0: + print "no cluster node available" + raise StopIteration + + if current_node >= len(nodes): + current_node = 0 + + node = nodes[current_node] + + 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 new file mode 100644 index 00000000..ce8e6fc4 --- /dev/null +++ b/examples/uwsgirouter4.py @@ -0,0 +1,14 @@ + +import uwsgi + +def application(e,s): + + node = uwsgi.cluster_best_node() + print node + + 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 new file mode 100644 index 00000000..af27b6ba --- /dev/null +++ b/examples/uwsgirouter5.py @@ -0,0 +1,9 @@ + +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 |