国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Tornado Web Server框架編寫簡易Python服務(wù)器

2020-02-15 22:35:32
字體:
供稿:網(wǎng)友

我們都知道在Web開發(fā)中,都需要服務(wù)器,比如Java Web開發(fā)的Tomcat,WebLogic,WebSphere,現(xiàn)在來看利用Tornado Web Server框架如何寫一個簡易的Python服務(wù)器。

一般來說只需要實(shí)現(xiàn)get和post方法就可以了。以上次使用redis數(shù)據(jù)庫的例子說明,數(shù)據(jù)庫插入代碼如下:

import redisimport datetime class Database:  def __init__(self):    self.host = 'localhost'    self.port = 6379    self.write_pool = {}   def add_write(self,website,city,year,month,day,deal_number):    key = '_'.join([website,city,str(year),str(month),str(day)])    val = deal_number    self.write_pool[key] = val   def batch_write(self):    try:      r = redis.StrictRedis(host=self.host,port=self.port)      r.mset(self.write_pool)    except Exception, exception:      print exception       def add_data():  beg = datetime.datetime.now()  db = Database()  for i in range(1,10000):    db.add_write('meituan','beijing',2013,i,1,i)  db.batch_write()  end = datetime.datetime.now()  print end-beg      if __name__ == '__main__':  add_data()

以上代碼插入了數(shù)據(jù),那么現(xiàn)在用我們的服務(wù)器來訪問一些數(shù)據(jù)。

import jsonimport redisimport tornado.webimport tornado.httpserverfrom tornado.options import define, options define("port", default=8888, type=int) class DealHandler(tornado.web.RequestHandler):  def initialize(self):    self.port = 6379    self.host = "localhost"   def get(self):    website = self.get_argument("website",None)    city  = self.get_argument("city",None)    year  = self.get_argument("year",None)    month  = self.get_argument("month",None)     keyset = []    for i in range(1,31):      key = '_'.join([website,city,year,month,str(i)])      keyset.append(key)     r = redis.StrictRedis(host=self.host,port=self.port)    self.write( json.dumps(r.mget(keyset)) ) class ExampleHandler(tornado.web.RequestHandler):  def get(self):    who = self.get_argument("who", None)    if who:      self.write("Hello, " + who)    else:      self.write("Hello World!")      def post(self):    who = self.get_argument("who", None)    if who:      self.write("Hello, " + who)    else:      self.write("Hello World!") class Application(tornado.web.Application):  def __init__(self):    handlers = [      (r"/", ExampleHandler),      (r"/deal", DealHandler),    ]    settings = dict()    tornado.web.Application.__init__(self, handlers, settings) def create_server():  tornado.options.parse_command_line()  http_server = tornado.httpserver.HTTPServer(Application())  http_server.listen(options.port)  tornado.ioloop.IOLoop.instance().start() if __name__ == "__main__":  create_server()

以上代碼實(shí)現(xiàn)了一個簡單的服務(wù)器,用于處理http請求。

在瀏覽器中輸入:

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

即可得到需要的數(shù)據(jù)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昆明市| 措美县| 临安市| 广水市| 原阳县| 龙州县| 永吉县| 泽库县| 晋城| 津南区| 汪清县| 荔浦县| 偃师市| 崇信县| 常州市| 灵武市| 容城县| 景泰县| 当雄县| 凯里市| 蛟河市| 府谷县| 奇台县| 宜都市| 肇州县| 呈贡县| 高安市| 平遥县| 资源县| 碌曲县| 德庆县| 晴隆县| 桂阳县| 于田县| 寻乌县| 察隅县| 苍山县| 浦东新区| 瓦房店市| 怀安县| 海伦市|