xmlrpc通信机制

pyspider通过xmlrpc来实现webui状态和服务端的通信

# run.py
def scheduler():
    if xmlrpc:
    utils.run_in_thread(scheduler.xmlrpc_run, port=xmlrpc_port, bind=xmlrpc_host)

# scheduler.py
class Scheduler(object):
    def xmlrpc_run(self, port=23333, bind='127.0.0.1', logRequests=False):
        from pyspider.libs.wsgi_xmlrpc import WSGIXMLRPCApplication
        application = WSGIXMLRPCApplication()
        # 服务端注册函数
        application.register_function(self.quit, '_quit')
        application.register_function(self.__len__, 'size')
        application.register_function(dump_counter, 'counter')
        application.register_function(new_task, 'newtask')
        application.register_function(send_task, 'send_task')
        application.register_function(update_project, 'update_project')
        application.register_function(get_active_tasks, 'get_active_tasks')
        application.register_function(get_projects_pause_status, 'get_projects_pause_status')
        application.register_function(webui_update, 'webui_update')
        ……

#wsgi_xmlrpc
from six.moves.xmlrpc_server import SimpleXMLRPCDispatcher

class WSGIXMLRPCApplication(object):
    def __init__(self, instance=None, methods=None):
        """Create windmill xmlrpc dispatcher"""
       self.dispatcher = SimpleXMLRPCDispatcher(allow_none=True, encoding=None)
       self.dispatcher.register_instance(instance)
       self.dispatcher.register_function(method)
       self.dispatcher.register_introspection_functions()

    def register_instance(self, instance):
        return self.dispatcher.register_instance(instance)

    def register_function(self, function, name=None):
        return self.dispatcher.register_function(function, name)

#webui/index.py
@app.route('/counter')
def counter():
    rpc = app.config['scheduler_rpc']
    if rpc is None:
        return json.dumps({})

    result = {}
    try:
        data = rpc.webui_update()  # 客户端调用函数
        ……

#run.py
def webui():
    app.config['scheduler_rpc'] = connect_rpc(ctx, None, 'http://127.0.0.1:23333/')
    or
    app.config['scheduler_rpc'] = scheduler_rpc

def connect_rpc(ctx, param, value):
    from six.moves import xmlrpc_client
    return xmlrpc_client.ServerProxy(value, allow_none=True)
xmlrpc通信
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 系统概述 渠道监测旨在通过爬取各种渠道、网盘、论坛、贴吧等抓取和App相关的信息,通过对获取信息的分析识别正盗版,...
    大道至简_Andy阅读 2,237评论 0 10
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,242评论 6 13
  • 我读了:《稻草人》里面的《皇帝的新衣》。 讲的是:安徒生写过皇帝的新衣,那个皇帝穿上了两个骗子织的...
    Jolin08阅读 162评论 0 0
  • 在学校的时光是人生最美好的回忆。 中专没考上中意学校,我们最后都选择读了高中。 初中时,我和你原本只是校友,想不到...
    尘埃KM阅读 390评论 0 14
  • 清单对于我1年前的我根本没有任何概念,我的生活,工作都没有调理。每天就好像消防队员那样,哪里焦急往哪里扑! 201...
    炫米阅读 184评论 0 0