Tornado写简易服务器


文章来源:http://blog.csdn.net/ACdreamers/article/details/24668551


我们都知道在Web开发中,都需要服务器,比如Java Web开发的Tomcat,WebLogic,WebSphere,现在来看利

Tornado Web Server框架如何写一个简易的Python服务器。


一般来说只需要实现get和post方法就可以了。以上次使用Redis数据库的例子说明,数据库插入代码如下:

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import redis  
  2. import datetime  
  3.   
  4. class Database:  
  5.     def __init__(self):  
  6.         self.host = 'localhost'  
  7.         self.port = 6379  
  8.         self.write_pool = {}  
  9.   
  10.     def add_write(self,website,city,year,month,day,deal_number):  
  11.         key = '_'.join([website,city,str(year),str(month),str(day)])  
  12.         val = deal_number  
  13.         self.write_pool[key] = val  
  14.   
  15.     def batch_write(self):  
  16.         try:  
  17.             r = redis.StrictRedis(host=self.host,port=self.port)  
  18.             r.mset(self.write_pool)  
  19.         except Exception, exception:  
  20.             print exception  
  21.               
  22.   
  23. def add_data():  
  24.     beg = datetime.datetime.now()  
  25.     db = Database()  
  26.     for i in range(1,10000):  
  27.         db.add_write('meituan','beijing',2013,i,1,i)  
  28.     db.batch_write()  
  29.     end = datetime.datetime.now()  
  30.     print end-beg  
  31.               
  32. if __name__ == '__main__':  
  33.     add_data()  


以上代码插入了数据,那么现在用我们的服务器来访问一些数据。


[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import json  
  2. import redis  
  3. import tornado.web  
  4. import tornado.httpserver  
  5. from tornado.options import define, options  
  6.   
  7. define("port", default=8888, type=int)  
  8.   
  9. class DealHandler(tornado.web.RequestHandler):  
  10.     def initialize(self):  
  11.         self.port = 6379  
  12.         self.host = "localhost"  
  13.   
  14.     def get(self):  
  15.         website = self.get_argument("website",None)  
  16.         city    = self.get_argument("city",None)  
  17.         year    = self.get_argument("year",None)  
  18.         month   = self.get_argument("month",None)  
  19.   
  20.         keyset = []  
  21.         for i in range(1,31):  
  22.             key = '_'.join([website,city,year,month,str(i)])  
  23.             keyset.append(key)  
  24.   
  25.         r = redis.StrictRedis(host=self.host,port=self.port)  
  26.         self.write( json.dumps(r.mget(keyset)) )  
  27.   
  28. class ExampleHandler(tornado.web.RequestHandler):  
  29.     def get(self):  
  30.         who = self.get_argument("who"None)  
  31.         if who:  
  32.             self.write("Hello, " + who)  
  33.         else:  
  34.             self.write("Hello World!")  
  35.           
  36.     def post(self):  
  37.         who = self.get_argument("who"None)  
  38.         if who:  
  39.             self.write("Hello, " + who)  
  40.         else:  
  41.             self.write("Hello World!")  
  42.   
  43. class Application(tornado.web.Application):  
  44.     def __init__(self):  
  45.         handlers = [  
  46.             (r"/", ExampleHandler),  
  47.             (r"/deal", DealHandler),  
  48.         ]  
  49.         settings = dict()  
  50.         tornado.web.Application.__init__(self, handlers, settings)  
  51.   
  52. def create_server():  
  53.     tornado.options.parse_command_line()  
  54.     http_server = tornado.httpserver.HTTPServer(Application())  
  55.     http_server.listen(options.port)  
  56.     tornado.ioloop.IOLoop.instance().start()  
  57.   
  58. if __name__ == "__main__":  
  59.     create_server()  

以上代码实现了一个简单的服务器,用于处理http请求。


在浏览器中输入:


http://localhost:8888/deal?website=meituan&city=beijing&year=2013&month=9


即可得到需要的数据。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值