(5)自己的handler,也就是对应url的处理函数
my_handler.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import json
import time
import logging
from unipath import Path
import traceback
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.gen
import urllib
import urllib2
import zlib
from tornado.options import define, options
from tornado.httpclient import AsyncHTTPClient
from tornado.web import HTTPError
from tornado.iostream import StreamClosedError
import common_logging
import common_urllib
from common_conf import g_conf
logger = logging.getLogger(__name__)
class MyHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self):
error_string = ""
res=""
try:
# 获取url中的参数
parm = self.get_argument("parm", "")
res = parm
except:
logger.error("%s" % traceback.format_exc());
error_string = "error"
finally:
logger.error("[url] request[%s] time[%s] error[%s] " % ( \
self.request.uri, \
self.request.request_time(), \
error_string \
))
#输出返回结果
self.write(res)
self.finish()
@tornado.web.asynchronous
@tornado.gen.coroutine
def post(self):
error_string = ""
res=''
try:
data = self.request.body
res = data
except:
logger.error("%s" % traceback.format_exc());
gaode_fetch_time = time.time() - gaode_start_time
error_string = "cycle curl fail"
finally:
logger.error("[url] request[%s] data:[%s] time[%s] error[%s]" % ( \
self.request.uri, \
data, \
self.request.request_time(), \
error_string
))
self.write(res)
self.finish()
如果请求中需要在线抓取其他服务,则可使用tornado框架的代理抓取。
75 client = AsyncHTTPClient()
76 my_response = ""
80 start_time = time.time()
81 try:
82 my_response = yield client.fetch(url, method="POST", body=data, \
83 request_timeout = float(200))
84 except:
86 gaode_response = ""
87 fetch_time = time.time() - start_time
88
89 if gaode_response and gaode_response.code == 200:
90 str_res = gaode_response.body