tornado 入门之(异步、同步实现-ab测压)酱酱仙的博客【6】
根据tornado官方示例代码,做对百度网站异步访问的ab测试
这种异步方式适用于爬虫 http://www.tornadoweb.org/en/stable/networking.html
同步测试代码:
import tornado.web
import tornado.ioloop
import tornado.httpclient
class IndexHandler(tornado.web.RequestHandler):
def get(self):
wd = self.get_argument('wd')
client = tornado.httpclient.HTTPClient()
# https://www.baidu.com/s?wd=python
response = client.fetch('https://www.baidu.com/s?wd=%s' %wd)
print(response)
self.write('同步测试')
def make_app():
return tornado.web.Application(handlers =[
(r'/',IndexHandler),
])
if __name__ == '__main__':
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
异步测试代码:
import tornado.web
import tornado.ioloop
import tornado.httpclient
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.gen.coroutine
def get(self):
wd = self.get_argument('wd')
client = tornado.httpclient.AsyncHTTPClient()
# https://www.baidu.com/s?wd=python
response = yield client.fetch('https://www.baidu.com/s?wd=%s' %wd)
print(response)
self.write('异步测试')
def make_app():
return tornado.web.Application(handlers =[
(r'/',IndexHandler),
])
if __name__ == '__main__':
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()