最新消息: 新版网站上线了!!!

python-tornado的接口用swagger进行包装的实例

дÕâ¸öÎÄÕµÄÖ÷ÒªÔ­Òò£¬¾ÍÊÇÒòΪûÓÐÏà¹ØµÄ¶«Î÷£¬µ¼ÖÂÎÒÍêÈ«²»ÖªµÀÓ¦¸ÃÔõô×ö£¬¾­¹ýÁËÁ½¸öÍíÉϵÄÃþË÷£¬ÖÕÓÚ¸ãÇå³þÁË£¬Èç¹ûÓÐË­ÐèÒªtornado+swaggerµÄÊä³öģʽ£¬¿ÉÒÔÕÕÕâ¸öÌ×£»

Ö÷ÒªÊÇstaticÎļþµÄÉú³É

ÎÒÃÇÓÃswagger¾ÍÊÇΪÁË×öÒ»¸ö¾²Ì¬Ò³Ã棬Ҳ¾ÍÊÇÉú³ÉÒ»¸östaticÎļþ£º

¼¸¸ö±Ø±¸µÄ¹¤¾ß£ºswagger-py-codegenÎÒÃDz»ÓÃËûÃǵÄÎļþ¿ò¼ÜÖ»ÐèÒªÆäÉú³ÉµÄstaticÎļþ£¬¿½±´µ½×Ô¼ºÎļþ¼Ðϼ´¿É£»

1£©ÐèҪעÒâµÄÊÇÉú³ÉÎļþÐèÒªÓÃyamlÎļþ£¬µ±±à¼­ºÃÎļþºó£¬×îÖ÷ÒªµÄÊÇҪעÒâbase_pathÎļþµÄ¸Ä¶¯£¬ÄãÉú³ÉµÄÎļþµÄÇëÇó»á¼ÓÉÏÄãbasepathµÄÄÚÈÝ£¬»áÔì³ÉÇëÇó³ö´í£¬ ±ÈÈç ÄãµÄurl:port/GET?testparas=test,ÄÇôÈç¹û˵ÄãµÄstaticÎļþÉú³ÉµÄ¹ý³ÌÖÐ,ÈçÏÂͼËùʾ

2£©Éú³ÉµÄÎļþ£¬swagger-uiÖеÄswagger.jsonÎļþÊÇ°´ÕÕÄãµÄyamlÎļþÉú³ÉµÄ£¬index.htmlÎļþÖÐÊÇÓеط½ÉèÖÃswagger.jsonµÄµØÖ·µÄ£¬ÈçÏÂͼËùʾ

3£©Õâ¸öʱºòÎÒÃÇ¿ÉÒÔÆô¶¯ÎÒÃǵÄswagger³ÌÐò£¬

½ÓÏÂÀ´½ÓÊÜÒ»ÏÂtornadoÖеÄһЩÉèÖã¬Ö÷ҪΪapplicationµÄÉèÖãº

ÔÚÕâÀïÆäʵûÓõ½tornadoµÄ¸ß¼¶·½·¨£¬Ö»ÊÇËæÒâÉèÖÃÁËÒ»¸ö²ÎÊý½øÐвâÊÔ£º

`import tornado.web
import tornado.ioloop
import os


abs_path = os.path.dirname(os.path.abspath("__file__"))
print(abs_path)

class IndexHandler(tornado.web.RequestHandler):
 def get(self, *args, **kwargs):
  para = self.get_argument('test', None)
  self.write("test tornado {}".format(para))


if __name__ == '__main__':
 app = tornado.web.Application([
  (r'/GET', IndexHandler)
 ],
  static_path=os.path.join(abs_path, 'static')
 )
 # ¼àÌý¶Ë¿Ú
 app.listen(8000)
 tornado.ioloop.IOLoop.current().start()`

Ŀ¼½á¹¹ÈçÏÂ

Éú³Éswagger-uiµÄ´úÂëÈçÏ£º

swagger_py_codegen -s api.yaml project-one -p demo -tlp=tornado --ui --spec

ÆäÖÐ ¨Cui ¨CspecÊÇÉú³Éswagger-uiµÄ¹Ø¼ü´úÂë

ÒÔÉÏÕâƪpython-tornadoµÄ½Ó¿ÚÓÃswagger½øÐаü×°µÄʵÀý¾ÍÊÇС±à·ÖÏí¸ø´ó¼ÒµÄÈ«²¿ÄÚÈÝÁË£¬Ï£ÍûÄܸø´ó¼ÒÒ»¸ö²Î¿¼£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö½Å±¾Ö®¼Ò¡£

转载请注明:谷谷点程序 » python-tornado的接口用swagger进行包装的实例