http传输json文件_python
https://cloud.tencent.com/developer/article/1571365
http傳輸圖片
https://www.cnblogs.com/jruing/p/12215688.html
python自帶http服務
https://www.cnblogs.com/ngbjng/p/11994336.html
python中的HTTP傳輸
https://blog.csdn.net/testcs_dn/article/details/50449106
Python實現基于HTTP文件傳輸實例
任務:自己寫一個http.server/client傳輸json格式數據
從網上東拼西湊攢出來的,已經調通了。(PS:想感謝兩位貼源碼的大神,但是找不到原網頁在哪了,抱歉!)
上代碼:
http server端
from http.server import HTTPServer, BaseHTTPRequestHandler
import jsonclass Resquest(BaseHTTPRequestHandler):def do_POST(self):print(self.headers)print(self.command)req_datas = self.rfile.read(int(self.headers['content-length'])) print("--------------------接受client發送的數據----------------")res1 = req_datas.decode('utf-8')res = json.loads(res1)print(res)print("--------------------接受client發送的數據------------------")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’} #要發送的數據 ,因為要轉成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端發送數據
response = conn.getresponse()
stc1 = response.read().decode(‘utf-8’)#接受server端返回的數據
stc = json.loads(stc1)
print("-----------------接受server端返回的數據----------------")
print(stc)
print("-----------------接受server端返回的數據----------------")
conn.close()
總結
以上是生活随笔為你收集整理的http传输json文件_python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: modin pandas 加速
- 下一篇: tornado资源