create account

每天进步一点点:在Python 中使用 XML RPC by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh ·
$158.79
每天进步一点点:在Python 中使用 XML RPC
Steem和Bitshares都支持RPC,也就是远程过程调用(Remote Procedure Call),这东西要我给出个正式的定义有点难,总之我理解就是通过RPC可以像调用本地函数一样使用远程服务的一些函数,来实现很多功能。


![](https://steemitimages.com/DQmXQzDMbiUMFVUWX8A9qodarzSPfjvgJH9NufV46enHncb/image.png)
(图源:[bing.com](bing.com))


steem和bitshares都支持JSON PRC,在之前的文章中我没少和大家一起探讨,感兴趣的可以去翻翻。今天我来介绍一个Python自带的XML RPC库,来了解一下这东西咋玩。

# Python 2.X

在Python 2.X里使用***SimpleXMLRPCServer***很简单,举一个简单的例子:

#### xmlrpc_server.py2
```
from SimpleXMLRPCServer import SimpleXMLRPCServer

def hello():
        return "Hello World!"

server = SimpleXMLRPCServer(("localhost", 8001))
server.register_function(hello)
server.serve_forever()
```
#### xmlrpc_client.py2

```
import xmlrpclib
client = xmlrpclib.ServerProxy("http://localhost:8001")
print client.hello()
```

#### 运行 & 结果

首先启动server
`python2 xmlrpc_server.py2`

然后我们在另外的终端中运行client
`python2 xmlrpc_client.py2`

在server所在终端会显示类似如下信息:
>`127.0.0.1 - - [08/Feb/2018 20:45:54] "POST /RPC2 HTTP/1.1" 200 -`

在client所在终端,我们会得出结果:
>`Hello World!`


# Python 3.X

在Python 3中,xmlrpclib被移到xmlrpc.client, SimpleXMLRPCServer被移到xmlrpc.server,所以上述代码要略作修改。

#### xmlrpc_server.py
```
from xmlrpc.server import SimpleXMLRPCServer

def hello():
        return "Hello World!"

server = SimpleXMLRPCServer(("localhost", 8001))
server.register_function(hello)
server.serve_forever()
```

#### xmlrpc_client.py
```
import xmlrpc.client
client = xmlrpc.client.ServerProxy("http://localhost:8001")
print(client.hello())
```

#### 运行 & 结果

首先启动server
`python3 xmlrpc_server.py`

然后我们在另外的终端中运行client
`python3 xmlrpc_client.py`

在server所在终端会显示类似如下信息:
>`127.0.0.1 - - [08/Feb/2018 21:06:14] "POST /RPC2 HTTP/1.1" 200 -`

在client所在终端,我们会得出结果:
>`Hello World!`

# 结论

在Python中使用XML RPC还是很简单的,当然了,还有一些较高深的使用方式以及出错处理等,可以去参考手册了解更多,本文就不再赘述了。

另外尽管XML RPC很方便,我还是倾向于使用JSON RPC,回头在和大家分享一下Python里JSON RPC的库吧。

# 参考链接

* https://docs.python.org/2/library/xmlrpclib.html
* https://docs.python.org/3/library/xmlrpc.html
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 102 others
properties (23)
authoroflyhigh
permlinkpython-xml-rpc
categorypython
json_metadata{"tags":["python","xml","rpc","cn-programming","cn"],"image":["https://steemitimages.com/DQmXQzDMbiUMFVUWX8A9qodarzSPfjvgJH9NufV46enHncb/image.png"],"links":["bing.com","https://docs.python.org/2/library/xmlrpclib.html","https://docs.python.org/3/library/xmlrpc.html"],"app":"steemit/0.1","format":"markdown"}
created2018-02-08 13:34:00
last_update2018-02-08 13:34:00
depth0
children10
last_payout2018-02-15 13:34:00
cashout_time1969-12-31 23:59:59
total_payout_value135.152 HBD
curator_payout_value23.636 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,922
author_reputation6,307,182,306,853,118
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,917,783
net_rshares23,019,013,563,965
author_curate_reward""
vote details (166)
@alanzheng ·
哈哈,我也在学习怎么用C/C++搭一个RPC server。Python方便,不过有点慢。
properties (22)
authoralanzheng
permlinkre-oflyhigh-python-xml-rpc-20180208t134835211z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-08 13:48:36
last_update2018-02-08 13:48:36
depth1
children2
last_payout2018-02-15 13:48:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length45
author_reputation2,727,533,211,976
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,920,508
net_rshares0
@huawuya ·
google的gRPC是现在比较流行的rpc框架,多语言支持,跨平台。N年前也用过C++的xml-rpc,比较慢,估计很久都不更新了。
properties (22)
authorhuawuya
permlinkre-alanzheng-201828t225840711z
categorypython
json_metadata{"tags":"python","app":"esteem/1.5.1","format":"markdown+html","community":"esteem"}
created2018-02-08 14:58:45
last_update2018-02-08 14:58:45
depth2
children0
last_payout2018-02-15 14:58:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length67
author_reputation16,414,652,638
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries
0.
accountesteemapp
weight1,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,934,371
net_rshares0
@oflyhigh ·
加油
properties (22)
authoroflyhigh
permlinkre-alanzheng-re-oflyhigh-python-xml-rpc-20180208t140505252z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-08 14:05:06
last_update2018-02-08 14:05:06
depth2
children0
last_payout2018-02-15 14:05:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2
author_reputation6,307,182,306,853,118
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,923,662
net_rshares0
@farazali1850 ·
Nice phythons
properties (22)
authorfarazali1850
permlinkre-oflyhigh-201828t203143166z
categorypython
json_metadata{"tags":["python","xml","rpc","cn-programming","cn"],"app":"esteem/1.5.0","format":"markdown+html","community":"esteem"}
created2018-02-08 15:32:57
last_update2018-02-08 15:32:57
depth1
children0
last_payout2018-02-15 15:32:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation27,599,706,273
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries
0.
accountesteemapp
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,941,083
net_rshares0
@hhjjkk ·
O哥,你说的每一个字每一个字母我都能看懂,为啥一放到一块我就看不懂了,我的脑袋有瑕疵啊!!
properties (22)
authorhhjjkk
permlinkre-oflyhigh-python-xml-rpc-20180208t133649094z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-08 13:36:51
last_update2018-02-08 13:36:51
depth1
children1
last_payout2018-02-15 13:36:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length45
author_reputation684,424,592,252
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,918,336
net_rshares0
@oflyhigh ·
其实呢,我也不太懂 😀
properties (22)
authoroflyhigh
permlinkre-hhjjkk-re-oflyhigh-python-xml-rpc-20180208t133935584z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-08 13:39:36
last_update2018-02-08 13:39:36
depth2
children0
last_payout2018-02-15 13:39:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length11
author_reputation6,307,182,306,853,118
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,918,819
net_rshares0
@kil ·
good and useful information
properties (22)
authorkil
permlinkre-oflyhigh-python-xml-rpc-20180208t142722559z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-08 14:27:42
last_update2018-02-08 14:27:42
depth1
children0
last_payout2018-02-15 14:27:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length27
author_reputation273,665,869,015
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,928,212
net_rshares0
@kstjiang ·
文章写得非常棒,,任何一个小白跟着你走都可以完成,给你10085个赞。

少给一个赞,怕你骄傲!

话说,我这样不脸真的好吗。。。
properties (22)
authorkstjiang
permlinkre-oflyhigh-python-xml-rpc-20180208t181330538z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-08 18:13:30
last_update2018-02-08 18:13:30
depth1
children0
last_payout2018-02-15 18:13:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation-8,240,821,202
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,972,246
net_rshares0
@sances ·
very good
properties (22)
authorsances
permlinkre-oflyhigh-python-xml-rpc-20180208t140448760z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-08 14:04:54
last_update2018-02-08 14:04:54
depth1
children0
last_payout2018-02-15 14:04:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9
author_reputation-634,309,385,460
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,923,607
net_rshares0
@sheriffakin ·
Nice...Python.
properties (22)
authorsheriffakin
permlinkre-oflyhigh-python-xml-rpc-20180208t133713342z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-08 13:37:18
last_update2018-02-08 13:37:18
depth1
children0
last_payout2018-02-15 13:37:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length14
author_reputation6,092,187,265,180
root_title"每天进步一点点:在Python 中使用 XML RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,918,410
net_rshares0