Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 79 lines (61 sloc) 1.882 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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)

Something went wrong with that request. Please try again.