python3 http.client/server post傳輸json問(wèn)題
任務(wù):
自己寫(xiě)一個(gè)http.server/client傳輸json格式數(shù)據(jù)
從網(wǎng)上東拼西湊攢出來(lái)的,已經(jīng)調(diào)通了。(PS:想感謝兩位貼源碼的大神,但是找不到原網(wǎng)頁(yè)在哪了,抱歉!)
上代碼:
http server端
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
class Resquest(BaseHTTPRequestHandler):
def do_POST(self):
print(self.headers)
print(self.command)
req_datas = self.rfile.read(int(self.headers['content-length']))
print("--------------------接受client發(fā)送的數(shù)據(jù)----------------")
res1 = req_datas.decode('utf-8')
res = json.loads(res1)
print(res)
print("--------------------接受client發(fā)送的數(shù)據(jù)------------------")
data1 = {'bbb':'222'}
data = json.dumps(data1)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(data.encode('utf-8'))
if __name__ == '__main__':
host = ('localhost', 8888)
server = HTTPServer(host, Resquest)
print("Starting server, listen at: %s:%s" % host)
server.serve_forever()http client 端:
import http.client, urllib.parse
import json
diag1 = {'aaa':'111'} #要發(fā)送的數(shù)據(jù) ,因?yàn)橐D(zhuǎn)成json格式,所以是字典類型
data = json.dumps(diag1)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = http.client.HTTPConnection('localhost', 8888)
conn.request('POST', '/ippinte/api/scene/getall', data.encode('utf-8'), headers)#往server端發(fā)送數(shù)據(jù)
response = conn.getresponse()
stc1 = response.read().decode('utf-8')#接受server端返回的數(shù)據(jù)
stc = json.loads(stc1)
print("-----------------接受server端返回的數(shù)據(jù)----------------")
print(stc)
print("-----------------接受server端返回的數(shù)據(jù)----------------")
conn.close()運(yùn)行結(jié)果:
server端(client to server)

client端(server back client)

對(duì)于json應(yīng)用的例子
首先要知道的是type(json) = str,傳輸?shù)臅r(shí)候也是以字符串格式傳輸,但其形式是字典:{‘aaa’:‘bbb’}。
在我的項(xiàng)目中,先利用json.dump()將字典轉(zhuǎn)成json格式
dict1 = {'aaa':'111'}
jstr = json.dumps(dict1)此時(shí),我們看到的type(jstr) = str

在client給server傳輸?shù)臅r(shí)候,要將json轉(zhuǎn)成字節(jié)流
jstr1 = jstr.encode('utf-8')在server端接受到的client端消息的時(shí)候就要解碼
jstr2 = jstr1.decode('utf-8')然后再將json轉(zhuǎn)為字典類型
jstr3 = json.loads(jstr2)
總結(jié)
打完收工~
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
OpenCV使用cv2進(jìn)行實(shí)時(shí)獲取攝像頭數(shù)據(jù)過(guò)程
文章介紹了如何使用OpenCV庫(kù)中的cv2模塊實(shí)時(shí)獲取攝像頭數(shù)據(jù),并提供了相關(guān)的安裝步驟和代碼總結(jié)2026-01-01
關(guān)于Python八大排序?qū)崿F(xiàn)方法(冒泡排序、快速排序等)
這篇文章主要介紹了關(guān)于Python八大排序?qū)崿F(xiàn)方法,主要有基數(shù)排序、歸并排序、堆排序、簡(jiǎn)單選擇排序、直接插入排序、希爾排序、快速排序、冒泡排序等,需要的朋友可以參考下2023-03-03
python如何實(shí)現(xiàn)單鏈表的反轉(zhuǎn)
這篇文章主要介紹了python如何實(shí)現(xiàn)單鏈表的反轉(zhuǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
python 限制函數(shù)調(diào)用次數(shù)的實(shí)例講解
下面小編就為大家分享一篇python 限制函數(shù)調(diào)用次數(shù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
python中閉包Closure函數(shù)作為返回值的方法示例
閉包(closure)是函數(shù)式編程的重要的語(yǔ)法結(jié)構(gòu),Python也支持這一特性,下面這篇文章主要給大家介紹了關(guān)于python中閉包Closure函數(shù)作為返回值的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12
使用Python編寫(xiě)簡(jiǎn)單的端口掃描器的實(shí)例分享
這篇文章主要介紹了使用Python編寫(xiě)簡(jiǎn)單的端口掃描器的實(shí)例分享,文中分別介紹了單線程和多線程的實(shí)現(xiàn)方式,需要的朋友可以參考下2015-12-12

