Tornado Web Server框架編寫簡(jiǎn)易Python服務(wù)器
我們都知道在Web開發(fā)中,都需要服務(wù)器,比如Java Web開發(fā)的Tomcat,WebLogic,WebSphere,現(xiàn)在來看利用Tornado Web Server框架如何寫一個(gè)簡(jiǎn)易的Python服務(wù)器。
一般來說只需要實(shí)現(xiàn)get和post方法就可以了。以上次使用redis數(shù)據(jù)庫(kù)的例子說明,數(shù)據(jù)庫(kù)插入代碼如下:
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()
以上代碼插入了數(shù)據(jù),那么現(xiàn)在用我們的服務(wù)器來訪問一些數(shù)據(jù)。
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()
以上代碼實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的服務(wù)器,用于處理http請(qǐng)求。
在瀏覽器中輸入:
http://localhost:8888/deal?website=meituan&city=beijing&year=2013&month=9
即可得到需要的數(shù)據(jù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中CURL 和python requests的相互轉(zhuǎn)換實(shí)現(xiàn)
本文主要介紹了python中CURL 和python requests的相互轉(zhuǎn)換實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
基于Keras中Conv1D和Conv2D的區(qū)別說明
這篇文章主要介紹了基于Keras中Conv1D和Conv2D的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python解決“ImportError:?Couldn‘t?import?Django”問題全攻略
本文主要介紹了Python解決“ImportError:?Couldn‘t?import?Django”問題全攻略,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
基于OpenCV4.2實(shí)現(xiàn)單目標(biāo)跟蹤
這篇文章主要介紹了如何和何時(shí)使用OpenCV 4.2中可用的8種不同的跟蹤器- BOOSTING, MIL, KCF, TLD, MEDIANFLOW, GOTURN, MOSSE和CSRT,并用他們實(shí)現(xiàn)單目標(biāo)跟蹤,需要的可以參考一下2022-03-03
在python3中pyqt5和mayavi不兼容問題的解決方法
今天小編就為大家分享一篇在python3中pyqt5和mayavi不兼容問題的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
深入解析python項(xiàng)目引用運(yùn)行路徑
這篇文章主要介紹了python項(xiàng)目引用運(yùn)行路徑的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05

