python tornado 框架使用 (5)

(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

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值