Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion how to fetch my own url in tornado?

Received: by 10.42.33.9 with SMTP id g9mr12831714icd.25.1349844571128;
        Tue, 09 Oct 2012 21:49:31 -0700 (PDT)
X-BeenThere: python-tornado@googlegroups.com
Received: by 10.50.0.208 with SMTP id 16ls9251208igg.1.canary; Tue, 09 Oct
 2012 21:49:28 -0700 (PDT)
Received: by 10.43.53.144 with SMTP id vq16mr12668668icb.26.1349844568421;
        Tue, 09 Oct 2012 21:49:28 -0700 (PDT)
Received: by 10.50.0.146 with SMTP id 18msige;
        Tue, 9 Oct 2012 19:09:40 -0700 (PDT)
Received: by 10.68.204.134 with SMTP id ky6mr5665551pbc.1.1349834980463;
        Tue, 09 Oct 2012 19:09:40 -0700 (PDT)
Date: Tue, 9 Oct 2012 19:09:40 -0700 (PDT)
From: yition cheung <zyx3998...@gmail.com>
To: python-tornado@googlegroups.com
Message-Id: <e2d231c8-a35f-4aa8-8bd3-0e0393d8c77c@googlegroups.com>
Subject: how to fetch my own url in tornado?
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1494_29662282.1349834980079"

------=_Part_1494_29662282.1349834980079
Content-Type: multipart/alternative; 
	boundary="----=_Part_1495_15790975.1349834980079"

------=_Part_1495_15790975.1349834980079
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Some of my handlers are used to return json, I write a new handler to get 
the jsons and comput them to generate new json, but i can't do this, it 
seems to no effect. But when i fetch them in a alone python script, it runs 
well. Cos i use gevent and urlopen? but I don't know how to do this use 
tornado's method. Please help.

some code:
def fetch_info(url, n):
    fetch_url = str(url) + str(n)
    raw_info = urlopen(fetch_url).read()
    info = tornado.escape.json_decode(raw_info)
    return info

def getinfo(n):
    url = 'http://localhost:8888/serverdemic/'
    info_dict = fetch_info(url, n)
    down_speed = info_dict['net_info']['load']
    up_speed = info_dict['net_info']['up']
    return (down_speed, up_speed)

def getstatus():
    servers = [i['machineNo'] for i in db.nebula_machine.find()]
    jobs = [gevent.spawn(fetch_info, 'http://127.0.0.1:8888/serverstatus/', 
i) for i in servers]
    gevent.joinall(jobs)
    ids = [job.value['id'] for job in jobs if job.value['status'] != 0]
    return ids

class TotalNetHandler(BaseHandler):
    def get(self):
        jobs = [gevent.spawn(getinfo, i) for i in getstatus()]
        gevent.joinall(jobs)
        raw_info = [job.value for job in jobs]
        down_up = zip(*raw_info)
        downup_sum = [sum(i) for i in down_up]
        info = {'down': downup_sum[0], 'up': downup_sum[1]}
        self.write(tornado.escape.json_encode(info))

------=_Part_1495_15790975.1349834980079
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Some of my handlers are used to return json, I write a new handler to get t=
he jsons and comput them to generate new json, but i can't do this, it seem=
s to no effect. But when i fetch them in a alone python script, it runs wel=
l. Cos i use gevent and urlopen? but I don't know how to do this use tornad=
o's method. Please help.<div><br></div><div>some code:</div><div><div>def f=
etch_info(url, n):</div><div>&nbsp; &nbsp; fetch_url =3D str(url) + str(n)<=
/div><div>&nbsp; &nbsp; raw_info =3D urlopen(fetch_url).read()</div><div>&n=
bsp; &nbsp; info =3D tornado.escape.json_decode(raw_info)</div><div>&nbsp; =
&nbsp; return info</div><div><br></div><div>def getinfo(n):</div><div>&nbsp=
; &nbsp; url =3D 'http://localhost:8888/serverdemic/'</div><div>&nbsp; &nbs=
p; info_dict =3D fetch_info(url, n)</div><div>&nbsp; &nbsp; down_speed =3D =
info_dict['net_info']['load']</div><div>&nbsp; &nbsp; up_speed =3D info_dic=
t['net_info']['up']</div><div>&nbsp; &nbsp; return (down_speed, up_speed)</=
div><div><br></div><div>def getstatus():</div><div>&nbsp; &nbsp; servers =
=3D [i['machineNo'] for i in db.nebula_machine.find()]</div><div>&nbsp; &nb=
sp; jobs =3D [gevent.spawn(fetch_info, 'http://127.0.0.1:8888/serverstatus/=
', i) for i in servers]</div><div>&nbsp; &nbsp; gevent.joinall(jobs)</div><=
div>&nbsp; &nbsp; ids =3D [job.value['id'] for job in jobs if job.value['st=
atus'] !=3D 0]</div><div>&nbsp; &nbsp; return ids</div><div><br></div><div>=
class TotalNetHandler(BaseHandler):</div><div>&nbsp; &nbsp; def get(self):<=
/div><div>&nbsp; &nbsp; &nbsp; &nbsp; jobs =3D [gevent.spawn(getinfo, i) fo=
r i in getstatus()]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; gevent.joinall(jo=
bs)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; raw_info =3D [job.value for job i=
n jobs]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; down_up =3D zip(*raw_info)</d=
iv><div>&nbsp; &nbsp; &nbsp; &nbsp; downup_sum =3D [sum(i) for i in down_up=
]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; info =3D {'down': downup_sum[0], 'u=
p': downup_sum[1]}</div><div>&nbsp; &nbsp; &nbsp; &nbsp; self.write(tornado=
.escape.json_encode(info))</div></div>
------=_Part_1495_15790975.1349834980079--

------=_Part_1494_29662282.1349834980079--