Tornado Web Server框架编写简易Python服务器

作者:acdreamers 时间:2021-10-18 09:23:52 

我们都知道在Web开发中,都需要服务器,比如Java Web开发的Tomcat,WebLogic,WebSphere,现在来看利用Tornado Web Server框架如何写一个简易的Python服务器。

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


import redis
import 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()

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


import json
import redis
import tornado.web
import tornado.httpserver
from 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()

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

在浏览器中输入:

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

即可得到需要的数据。

来源:https://blog.csdn.net/ACdreamers/article/details/24668551

标签:Tornado,Web,Server,python,服务器
0
投稿

猜你喜欢

  • python读写文件with open的介绍

    2022-04-03 22:19:34
  • 详解python基础之while循环及if判断

    2023-01-14 20:54:39
  • 多个函数验证同一表单方法

    2007-10-06 22:55:00
  • asp如何让计数器只对新进用户计数?

    2010-05-13 16:36:00
  • BeautifulSoup中find和find_all的使用详解

    2023-11-08 21:00:22
  • PyQt5+Pycharm安装和配置图文教程详解

    2022-12-20 08:50:26
  • python中get和post有什么区别

    2022-04-17 16:45:15
  • ASP实现防止网站被采集代码

    2011-03-25 10:40:00
  • python使用opencv驱动摄像头的方法

    2023-08-26 17:00:49
  • python cookie反爬处理的实现

    2021-10-16 23:33:17
  • asp让网站自动识别手机访问跳转至手机网站

    2014-12-06 09:36:02
  • python判断数字是否是超级素数幂

    2023-12-24 06:16:31
  • python钉钉机器人运维脚本监控实例

    2022-08-23 22:19:48
  • 基于PHP读取csv文件内容的详解

    2023-11-16 04:17:48
  • Python 正则 re.compile 真的必需吗

    2021-12-27 17:12:24
  • Python pip 安装与使用(安装、更新、删除)

    2022-07-30 01:58:19
  • AJAX的jQuery实现入门(一)

    2008-05-01 12:55:00
  • Python利用watchdog模块监控文件变化

    2023-10-23 22:27:22
  • 详解Python解决抓取内容乱码问题(decode和encode解码)

    2021-12-18 09:26:29
  • Python深度学习pytorch神经网络多层感知机简洁实现

    2021-08-20 17:37:43
  • asp之家 网络编程 m.aspxhome.com