1. 程式人生 > >python Tornado簡單伺服器搭建

python Tornado簡單伺服器搭建

官方網站http://old.sebug.net/paper/books/tornado/

FriendFeed使用了一款使用 Python 編寫的,相對簡單的 非阻塞式 Web 伺服器。其應用程式使用的 Web 框架看起來有些像 web.py 或者 Google 的 webapp, 不過為了能有效利用非阻塞式伺服器環境,這個 Web 框架還包含了一些相關的有用工具 和優化。

Tornado 就是我們在 FriendFeed 的 Web 伺服器及其常用工具的開源版本。Tornado 和現在的主流 Web 伺服器框架(包括大多數 Python 的框架)有著明顯的區別:它是非阻塞式伺服器,而且速度相當快。得利於其 非阻塞的方式和對 

epoll 的運用,Tornado 每秒可以處理數以千計的連線,因此 Tornado 是實時 Web 服務的一個 理想框架。我們開發這個 Web 伺服器的主要目的就是為了處理 FriendFeed 的實時功能 ——在 FriendFeed 的應用裡每一個活動使用者都會保持著一個伺服器連線。(關於如何擴容 伺服器,以處理數以千計的客戶端的連線的問題)

以下是經典的 “Hello, world” 示例:

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

寫過的一個例子:

IN:http://10.10.177.179:10081/slquery?dept=PEK&dest=CDG&type=flight&pay_method=mioji

OUT:源列表

#!/usr/bin/python
#! -*- coding:utf-8 -*-

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import json
import sys
sys.path.append('/home/workspace/ProxyServer/bin')
sys.path.append('/home/fangwang/statistic_scripts/')
import os
from DBHandle import DBHandle
import re

monitor_source = DBHandle("10.10.87.87","root","miaoji@2014!","devdb")


source_mioji_dict=dict()
source_self_mioji_dict=dict()
source_raw=[]
flight_validation_dict=dict()

def init_source_tab():
    #初始化源列表
    res=monitor_source.QueryBySQL("SELECT * FROM source WHERE pay_method != 'NULL'")    
    for line in res:
        if  line['pay_method'].find('mioji') != -1:
            tmp_type=line['type']
            source_mioji_dict.setdefault(tmp_type,[])
            source_mioji_dict[tmp_type].append(line['name'])
        elif  line['pay_method'] == 'self+mioji':
            tmp_type=line['type']
            source_self_mioji_dict.setdefault(tmp_type,[])
            source_self_mioji_dict[tmp_type].append(line['name'])


def init_flight_validation_tab():
    #初始化過濾列表
    res=monitor_source.QueryBySQL("SELECT * FROM flight_validation where status != 0")
    for line in res:
        if line['type']=='oneway':
            key=line['dept_id']+'|'+line['dest_id']
            flight_validation_dict.setdefault(key,[])
            flight_validation_dict[key].append(line['source'])

def add_source(_type,pay_method):
    #取出source所有源
    source_list=[]
    global source_raw
    if  _type == 'flight':
        if pay_method == 'mioji':
            source_raw=source_mioji_dict['flight_one_way']
        elif pay_method == 'self+mioji':
            source_raw=source_self_mioji_dict['flight_one_way']
        for source in source_raw:
            source_list.append(source)
    return source_list

def validation_source(dept,dest,_type,source_list):
    #過濾source表
    if _type == 'flight':
        source_validation=[]
        key=dept+'|'+dest
        if key not in flight_validation_dict.keys():
            source_validation.append('ctripFlight')
            source_validation.append('expediaFligh')
            return source_validation
        tmp_source = flight_validation_dict[key]
        for source in source_raw:
            if source in tmp_source:
                source_validation.append(source)
        if len(source_validation)<=1:
            source_validation.append('ctripFlight')
            source_validation.append('expediaFlight')
        return source_validation
class hello(tornado.web.RequestHandler):
    def get(self):
        print self.request
        try:
            dept = self.get_argument('dept')
        except:
            print 'put in dept error'
        try:
            dest = self.get_argument('dest')
        except:
            print 'put in dest error'
        try:
            pay_method = self.get_argument('pay_method')
        except:
            print 'put in pay_method error'
        try:
            trans_type = self.get_argument('type')
        except:
            print 'put in trans_type error'
        print("dept: %s, dest: %s, trans_type: %s, pay_method: %s" % (dept, dest, trans_type,pay_method))
        source_list = []
        #根據型別計算source_list
        #...
                
        source_list = add_source(trans_type,pay_method)
        source_list = validation_source(dept,dest,trans_type,source_list)
        
        source_list = list(set(source_list))
        if 'mioji' in source_list:
            source_list.remove('mioji')

        self.write(json.dumps(source_list))

if __name__ == '__main__':
    init_source_tab()
    init_flight_validation_tab()
    print 'inited over'
    application = tornado.web.Application([
        (r"/slquery", hello)
            ])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(10081)
    http_server.start()
    tornado.ioloop.IOLoop.instance().start()