create account

每天进步一点点:Python中使用urllib3访问STEEM RPC by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh · (edited)
$114.07
每天进步一点点:Python中使用urllib3访问STEEM RPC
在之前的文章中,我们学习在[Python中使用Requests访问STEEM RPC](https://steemit.com/python/@oflyhigh/python-requests-steem-rpc)、[Python中使用urllib访问STEEM RPC](https://steemit.com/python/@oflyhigh/python-urllib-steem-rpc)、[Python中使用PycURL访问STEEM RPC](https://steemit.com/pycurl/@oflyhigh/python-pycurl-steem-rpc),原本这三把板斧足够我用了,但是阅读好些代码都是用的urllib3,所以拿来试试啦。

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

# 介绍

urllib3是一个强大的、健全友好的Python HTTP客户端,包括requests、pip在内的很多Python生态系统都使用了urllib3。

urllib3具有如下特性:
* 线程安全
* 连接池
* 客户端SSL/TLS校验
* 多部分编码文件上传
* 请求重试以及HTTP重定向
* gzip以及deflate编码
* HTTP以及SOCKS代理
* 100%测试覆盖


# 安装

urllib3是第三方的库,所以使用之前需要先安装。

pip安装的指令为:
`pip install urllib3`

因为我安装过requests,所以会提示我已经安装啦。

# 代码

继续拿我们之前的命令为例来学习:

>`curl --data '{"jsonrpc": "2.0", "method": "call", "params": ["account_by_key_api", "get_key_references", [["STM6MGdForcZ8HskcguP84QSCb8udgz7W9yUPU5jtsAKQAxth3U16"]]], "id": 1}' https://api.steemit.com`

使用urllib3改写后的简单代码为:
```
import urllib3
import json

payload = {"jsonrpc": "2.0", "method": "call", "params": ["account_by_key_api", "get_key_references", [["STM6MGdForcZ8HskcguP84QSCb8udgz7W9yUPU5jtsAKQAxth3U16"]]], "id": 1}
rpc = "https://api.steemit.com"

http = urllib3.PoolManager()
r = http.request('POST', rpc, body=json.dumps(payload).encode('utf-8'))
print(r.data.decode('utf-8'))
```

# 结果

执行结果为:
>`InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
{"id":1,"result":[["oflyhigh"]]}`

加上这样一句就好啦
`urllib3.disable_warnings()`
![](https://steemitimages.com/DQmWxUdaVa77QNb3WRXooSitp7dyTipXUhwFZos1sfgUm4v/image.png)

但是实际使用中,不校验证书是不安全、不被提倡的做法。如何校验证书,将在其它文章中另行阐述。


# 高级功能

类似keep-alive等高级功能可以通过在构建urllib3.PoolManager类实例时通过参数指定。

\********************************************************
比如Keep-Alive功能,需要在上述代码中加入如下内容:
```
from urllib3.connection import HTTPConnection
socket_options = HTTPConnection.default_socket_options + \
[(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1), ]
http = urllib3.poolmanager.PoolManager(socket_options=socket_options)
```
\*********************************************************
***注:这段代码我理解有误,并非用于实现Keep-Alive功能***


更多功能和详情,参考用户手册吧。

# 参考链接
* https://urllib3.readthedocs.io/en/latest/index.html
* [每天进步一点点:Python中使用PycURL访问STEEM RPC](https://steemit.com/pycurl/@oflyhigh/python-pycurl-steem-rpc)
* [每天进步一点点:Python中使用urllib访问STEEM RPC](https://steemit.com/python/@oflyhigh/python-urllib-steem-rpc)
* [每天进步一点点:Python中使用Requests访问STEEM RPC](https://steemit.com/python/@oflyhigh/python-requests-steem-rpc)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 90 others
properties (23)
authoroflyhigh
permlinkpython-urllib3-steem-rpc
categorypython
json_metadata{"tags":["python","urllib3","steem","cn-programming","cn"],"image":["https://steemitimages.com/DQmXQzDMbiUMFVUWX8A9qodarzSPfjvgJH9NufV46enHncb/image.png","https://steemitimages.com/DQmWxUdaVa77QNb3WRXooSitp7dyTipXUhwFZos1sfgUm4v/image.png"],"links":["https://steemit.com/python/@oflyhigh/python-requests-steem-rpc","https://steemit.com/python/@oflyhigh/python-urllib-steem-rpc","https://steemit.com/pycurl/@oflyhigh/python-pycurl-steem-rpc","https://bing.com","https://urllib3.readthedocs.io/en/latest/index.html"],"app":"steemit/0.1","format":"markdown"}
created2018-02-25 13:33:09
last_update2018-03-01 10:58:36
depth0
children7
last_payout2018-03-04 13:33:09
cashout_time1969-12-31 23:59:59
total_payout_value96.658 HBD
curator_payout_value17.413 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,694
author_reputation6,384,147,372,083,281
root_title"每天进步一点点:Python中使用urllib3访问STEEM RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id40,336,974
net_rshares20,588,441,893,685
author_curate_reward""
vote details (154)
@carlosgerdet ·
Muy bueno, amigo, te seguiré aunque estés en otro idioma
properties (22)
authorcarlosgerdet
permlinkre-oflyhigh-python-urllib3-steem-rpc-20180225t193255627z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-25 19:06:33
last_update2018-02-25 19:06:33
depth1
children0
last_payout2018-03-04 19:06:33
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_length56
author_reputation273,164,074,193
root_title"每天进步一点点:Python中使用urllib3访问STEEM RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id40,403,037
net_rshares0
@farah-kamran ·
all commands are useful and easy
properties (22)
authorfarah-kamran
permlinkre-oflyhigh-python-urllib3-steem-rpc-20180225t133513478z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-25 13:35:21
last_update2018-02-25 13:35:21
depth1
children0
last_payout2018-03-04 13:35:21
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_length32
author_reputation175,703,680,965
root_title"每天进步一点点:Python中使用urllib3访问STEEM RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id40,337,425
net_rshares0
@hamidi103 ·
Good luck @oflyhigh
properties (22)
authorhamidi103
permlinkre-oflyhigh-2018225t21301723z
categorypython
json_metadata{"tags":["python","urllib3","steem","cn-programming","cn"],"app":"esteem/1.5.1","format":"markdown+html","community":"esteem"}
created2018-02-25 14:30:09
last_update2018-02-25 14:30:09
depth1
children0
last_payout2018-03-04 14:30:09
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_length19
author_reputation3,169,070,286,797
root_title"每天进步一点点:Python中使用urllib3访问STEEM RPC"
beneficiaries
0.
accountesteemapp
weight1,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id40,348,588
net_rshares0
@hotpacks ·
Congratulations
Upvoted ☝ Have a great day!
properties (22)
authorhotpacks
permlinkre-oflyhigh-python-urllib3-steem-rpc-20180301t205100272z
categorypython
json_metadata""
created2018-03-01 11:51:21
last_update2018-03-01 11:51:21
depth1
children0
last_payout2018-03-08 11:51:21
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_reputation-14,134,173,840
root_title"每天进步一点点:Python中使用urllib3访问STEEM RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id41,363,063
net_rshares0
@pengenkaya ·
Hy friend very very good post thanks for you
properties (22)
authorpengenkaya
permlinkre-oflyhigh-python-urllib3-steem-rpc-20180225t134533038z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-25 13:45:39
last_update2018-02-25 13:45:39
depth1
children0
last_payout2018-03-04 13:45:39
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_length44
author_reputation1,399,679,939,009
root_title"每天进步一点点:Python中使用urllib3访问STEEM RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id40,339,416
net_rshares0
@playnet ·
Buen post! Gracias por compartir.
properties (22)
authorplaynet
permlinkre-oflyhigh-python-urllib3-steem-rpc-20180225t184049589z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-25 18:41:15
last_update2018-02-25 18:41:15
depth1
children0
last_payout2018-03-04 18:41:15
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_length33
author_reputation5,139,860,917
root_title"每天进步一点点:Python中使用urllib3访问STEEM RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id40,398,450
net_rshares0
@zulfikrizal ·
Very good picture and your story
properties (22)
authorzulfikrizal
permlinkre-oflyhigh-python-urllib3-steem-rpc-20180225t140135429z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2018-02-25 14:01:45
last_update2018-02-25 14:01:45
depth1
children0
last_payout2018-03-04 14:01: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_length32
author_reputation12,742,438,086
root_title"每天进步一点点:Python中使用urllib3访问STEEM RPC"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id40,342,696
net_rshares0