部分ssrpc.py代码分析 -- 服务端:

 1 #!/usr/bin/python3
2
3 from xmlrpc.client import Fault, dumps, loads
4 import sys
5 from socketserver import ForkingMixIn
6 from xmlrpc.server import SimpleXMLRPCServer
7
8 class VerboseFaultXMLRPCServer(SimpleXMLRPCServer):
9 def _marshaled_dispatch(self, data, dispatch_method = None, path = None):
10 """Dispatches an XML-RPC method from marshalled (XML) data.
11
12 XML-RPC methods are dispatched from the marshalled (XML) data
13 using the _dispatch method and the result is returned as
14 marshalled data. For backwards compatibility, a dispatch
15 function can be provided as an argument (see comment in
16 SimpleXMLRPCRequestHandler.do_POST) but overriding the
17 existing method through subclassing is the preferred means
18 of changing method dispatch behavior.
19 """
20
21 try:
22 params, method = loads(data)
23
24 # generate response
25 if dispatch_method is not None:
26 response = dispatch_method(method, params)
27 else:
28 response = self._dispatch(method, params)
29 # wrap response in a singleton tuple
30 response = (response,)
31 response = dumps(response, methodresponse=1,
32 allow_none=self.allow_none, encoding=self.encoding)
33 except Fault as fault:
34 response = dumps(fault, allow_none=self.allow_none,
35 encoding=self.encoding)
36 except:
37 # report exception back to server
38 exc_type, exc_value, exc_tb = sys.exc_info()
39 while exc_tb.tb_next is not None:
40 exc_tb = exc_tb.tb_next # find last frame of the traceback
41 lineno = exc_tb.tb_lineno
42 code = exc_tb.tb_frame.f_code
43 filename = code.co_filename
44 name = code.co_name
45 response = dumps(
46 Fault(1, "%s:%s FILENAME: %s LINE: %s NAME: %s" % (
47 exc_type, exc_value, filename, lineno, name)),
48 encoding=self.encoding, allow_none=self.allow_none,
49 )
50
51 return response.encode(self.encoding)
52
53 # One process per request
54 class ForkingXMLRPCServer(ForkingMixIn, VerboseFaultXMLRPCServer):
55 max_children = 500 # default is 40
56
57 server = ForkingXMLRPCServer(("", 8889), allow_none=True)
58
59 # Register functions here
60
61 from ssapi.disk.sas import sasdiskinfo
62 from ssapi.disk.ledctl import ledctl_set
63 from ssapi.zfs.zpoollist import zpoollist
64 from ssapi.zfs.zpoolstatus import zpoolstatus
65 from ssapi.zfs.zpoolcreate import zpoolcreate
66
67 funcs = [
68 sasdiskinfo,
69 ledctl_set,
70 zpoollist,
71 zpoolstatus,
72 zpoolcreate,
73 ]
74
75 for i in funcs:
76 server.register_function(i)
77
78 # Start service
79 server.serve_forever()

正如上篇文章所述,SimpleXMLRPCServer是一个单线程的服务器,所以这里支持多进程的方式如下:

1.定义VerboseFaultXMLRPCServer类,继承于SimpleXMLRPCServer

2.定义一个新类:ForkingXMLRPCServer(ForkingMixIn, VerboseFaultXMLRPCServer),其中ForkingMixIn是从socketserver中导入

3.调用新类创建server实体,server = ForkingXMLRPCServer(("", 8889), allow_none=True),则支持多进程

/usr/lib/python3.2/xmlrpc/server.py 中SimpleXMLRPCServer源代码:

 1 class SimpleXMLRPCServer(socketserver.TCPServer,
2 SimpleXMLRPCDispatcher):
3 """Simple XML-RPC server.
4
5 Simple XML-RPC server that allows functions and a single instance
6 to be installed to handle requests. The default implementation
7 attempts to dispatch XML-RPC calls to the functions or instance
8 installed in the server. Override the _dispatch method inherited
9 from SimpleXMLRPCDispatcher to change this behavior.
10 """
11
12 allow_reuse_address = True
13
14 # Warning: this is for debugging purposes only! Never set this to True in
15 # production code, as will be sending out sensitive information (exception
16 # and stack trace details) when exceptions are raised inside
17 # SimpleXMLRPCRequestHandler.do_POST
18 _send_traceback_header = False
19
20 def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler,
21 logRequests=True, allow_none=False, encoding=None, bind_and_activate=True):
22 self.logRequests = logRequests
23
24 SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
25 socketserver.TCPServer.__init__(self, addr, requestHandler, bind_and_activate)
26
27 # [Bug #1222790] If possible, set close-on-exec flag; if a
28 # method spawns a subprocess, the subprocess shouldn't have
29 # the listening socket open.
30 if fcntl is not None and hasattr(fcntl, 'FD_CLOEXEC'):
31 flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD)
32 flags |= fcntl.FD_CLOEXEC
33 fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags)

Tornado/Python 学习笔记(二)的更多相关文章

  1. python学习笔记(二)、字符串操作

    该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.字符串基本操作 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于 ...

  2. Python 学习笔记二

    笔记二 :print 以及基本文件操作 笔记一已取消置顶链接地址 http://www.cnblogs.com/dzzy/p/5140899.html 暑假只是快速过了一遍python ,现在起开始仔 ...

  3. (10.1)Python学习笔记二

    1.在项目工程中要模块化测试一个开发的功能,在测试通过后交付给项目组其他人员继续开发.要保证代码开发的性能和效率以及可扩展性. 2.项目工程中的文件夹分类要功能模块明确清晰,在python中引入某一个 ...

  4. Tornado/Python 学习笔记(一)

    1.源代码下载及安装:http://www.tornadoweb.org/en/stable/ 2.python中xmlrpc库官方文档:https://docs.python.org/3/libra ...

  5. Python学习笔记二

    ---恢复内容开始--- 一. python几种数据类型的比较. 从以下几个方面比较: 1. 是否可变. 不可变类型:变量的值可以发生变化,id也变了,相当于创建了一个新的对象,所以一修改值,id就变 ...

  6. Python学习笔记(二)

    标识符和关键字 1,邮箱的Python标识符是任意长度的非空字符序列(引导字符+后续字符.) python标识符必须符合两条规则--标识符区分大小写 (1)只要是unicode编码字母都可以充当引导字 ...

  7. python学习笔记(二):python数据类型

    上一篇博客写了python的入门和简单流程控制,这次写python的数据类型和各种数据类型的内置方法.一.数据类型是什么鬼?计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各 ...

  8. python学习笔记二:流程控制

    一.if else: #!/usr/bin/python x = int(raw_input('please input:')) if x >= 90: if x >= 95: print ...

  9. python 学习笔记二 (列表推导式)

    2018年年初写了第一篇博客,说要做一个认真的技术人 https://www.cnblogs.com/yingchen/p/8455507.html 今天已经是11月19日了,这是第二篇博客,看来坚持 ...

随机推荐

  1. PHP中与类有关的运算符

    与类有关的运算符: new, instanceof:判断一个“变量”(对象,数据),是否是某个类的“实例”: 示意如下: class  A {} class  B {} class  C extend ...

  2. Collections带有的排序方法 传入的元素类型 需是子类或者这个类的实例

  3. BZOJ 4034 树上操作(树的欧拉序列+线段树)

    刷个清新的数据结构题爽一爽? 题意: 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x ...

  4. 【明哥报错簿】之【HTTP Status 500 - Servlet.init() for servlet mvc-dispatcher threw exception】

    报错:java.lang.NoClassDefFoundError: /factory/config/EmbeddedValueResolver spring或者jdk的问题,解决办法:spring3 ...

  5. P1039 侦探推理

    题目描述 明明同学最近迷上了侦探漫画<柯南>并沉醉于推理游戏之中,于是他召集了一群同学玩推理游戏.游戏的内容是这样的,明明的同学们先商量好由其中的一个人充当罪犯(在明明不知情的情况下),明 ...

  6. Guardian of Decency UVALive - 3415(最大独立集板题)

    老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学生数量 ...

  7. Linux学习笔记二:tar命令使用

    tar命令详解 tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的 ...

  8. 【Socket】从零打造基于Socket在线升级模块

    一.前言       前段时间一直在折腾基于Socket的产品在线升级模块.之前我曾写过基于.Net Remoting的.基于WCF的在线升级功能,由于并发量较小及当时代码经验的不足一直没有实际应用. ...

  9. 洛谷 U14472 数据结构【比赛】 【差分数组 + 前缀和】

    题目描述 蒟蒻Edt把这个问题交给了你 ---- 一个精通数据结构的大犇,由于是第一题,这个题没那么难.. edt 现在对于题目进行了如下的简化: 最开始的数组每个元素都是0 给出nnn,optopt ...

  10. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...