場景一
http://127.0.0.1:8088/api/getblogbyany/?category=Django&authorname=ArithmeticJia類似這樣的請求格式
註釋掉的方法也可以
# url:http://127.0.0.1:8088/api/getblogbyany/?category=Django&authorname=ArithmeticJia
class GetBlogByAny(RequestHandler):
def get(self):
# category = self.get_query_argument('category')
# authorname = self.get_query_argument('authorname')
category = self.get_argument('category')
authorname = self.get_argument('authorname')
print(category, authorname)
# 適合API請求
(r'/api/getblogbyany/', GetBlogByAny),
場景二
http://127.0.0.1:8088/api/getblogbycategory/Django類似這樣的請求
@basic_auth(basic_auth_valid)
class GetBlogByCategory(tornado.web.RequestHandler):
def get(self, category):
print(category)
(r'/api/getblogbycategory/(?P<category>.+)', GetBlogByCategory),
這裏用到了正則表達式
多個參數可以這樣
class GetPython(RequestHandler):
def get(self, name, age):
self.write('name:%s age:%s' % (name, age))
(r'/api/python/(?P<name>.+)/(?P<age>[0-9]+)', GetPython),
這裏url裏的參數名一定要和get函數裏面對應,順序無所謂,但是名字一定要一樣 。