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 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发送的数据----------------")
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()