• 使用图灵机器人api开发个人智能机器人


    图灵智能机器人地址 http://www.tuling123.com/openapi/cloud/proexp.jsp

    一次偶然在某论坛发现这个玩意儿,挺好玩的,还居然有开放API,就跟微信公共账号似的,因此自己就想着捣鼓一个自己的智能及其人来玩玩。。

    开始构想。想用python来开发,因为调用外部API比较耗时。为了缩短请求处理的响应时间,想把API调用的请求做成服务器的异步请求处理模式,

    刚好python有Tornado这个非常强大的web框架,并且支持服务器端的异步请求处理,就决定使用它了。。。。

    代码很简单,除了开发一个tornado项目必备固定的那几个部分外,请求处理类只需要一个。其余的则是js部分。。。

    python请求处理类代码:

    handlers.py

    import tornado.web
    import os
    import tornado.httpclient
    import json
    config = {}
    execfile(os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.py"), config)

    class AnwserHandler(tornado.web.RequestHandler):
        @tornado.web.asynchronous
        def get(self):
            self.info = self.get_argument("info","")
            url = config['URL']+"?key="+config['KEY']+"&info="+self.info
            asyncclient = tornado.httpclient.AsyncHTTPClient()
            asyncclient.fetch(url,callback=self.on_response)
        def on_response(self,response):
            reback = json.loads(response.body)
            if not self.info:
                self.render("index.html",section = reback['text'])
            else:
                print response.body
                self.write(response.body)
                self.finish()

    程序主模块:

    main.py

    import tornado.httpserver
    import tornado.ioloop
    import tornado.web
    import os
    import urls
    from tornado.options import define,options

    define("port",default=8000,help="run on the given port",type=int)
    class Application(tornado.web.Application):
        def __init__(self):
            handlers = urls.handler
            settings = dict(template_path = os.path.join(os.path.dirname(__file__),"templates"),
                       static_path = os.path.join(os.path.dirname(__file__),"static"))
            tornado.web.Application.__init__(self, handlers, **settings)
            
    if __name__ == "__main__":
        tornado.options.parse_command_line()
        application  = Application()
        http_server = tornado.httpserver.HTTPServer(application)
        http_server.listen(options.port)
        tornado.ioloop.IOLoop.instance().start()

    项目效果图:

    不是专门写前端的,界面凑合着看吧

  • 相关阅读:
    JS——switch case
    JS——“==”与“===”
    C#——枚举格式转换与比较
    XML——读与写
    SQl基本操作——try catch
    JS——indexOf replace search
    C#——数据库的访问
    SQL基本操作——存储过程
    C#——设置开机启动
    C#——计时器的操作
  • 原文地址:https://www.cnblogs.com/wuxinqiu/p/3959524.html
Copyright © 2020-2023  润新知