使用步驟如下:
一、如果想發送json格式的數據,需要使用request模塊中的Request類來創建對象,作為urlopen函數的參數
二、header中添加content-type為application/json
三、使用json中dumps方法將請求體內容解析為字符串類型
from urllib import request import json # 請求體數據 request_data ={ "account": "xxxxxx", "sign": "xxxx" } headers ={ "content-type":"application/json" } req = request.Request(url = "http://host:port/mm.nn.zz", headers = headers, data = json.dumps(request_data).encode("utf-8")) reps = request.urlopen(req).read().decode("utf-8")