python文件转字节流上传? pythonweb前后端分离项目怎么实现单文件上传
Python\u7684\u6587\u4ef6\u4e0a\u4f20\u505a\u7684\u662fweb\u5f00\u53d1\u5417\uff1f\u53ef\u4ee5\u7528\u6846\u67b6\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u63a8\u8350\u4f7f\u7528Django\u6846\u67b6\uff0c\u7075\u6d3b\u5f3a\u5927\uff0c\u7279\u522b\u9002\u5408\u505a\u65b0\u95fb\u3001\u535a\u5ba2\u7c7b\u7f51\u7ad9\u3002\u7528Python\u4e0a\u4f20\u6587\u4ef6\uff0c\u8981\u7528\u5230\u4e2d\u95f4\u4ef6\uff0c\u627e\u4e00\u4e2a\u9002\u5408\u4f60\u7528\u7684\u4e2d\u95f4\u4ef6\uff0c\u901a\u8fc7\u8fd9\u4e2a\u4e2d\u95f4\u4ef6\u4e0a\u4f20\uff0c\u5982\u679c\u662f\u81ea\u5df1\u5199\u7684\u8bdd\u4f1a\u5f88\u9ebb\u70e6\u3002\u5b58\u5165\u6570\u636e\u5e93\u7684\u4e0d\u5e94\u8be5\u662f\u6587\u4ef6\u800c\u5e94\u8be5\u662f\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u5b58\u5165\u4e86\u591a\u5c11\u6761\u6570\u636e\u8fd9\u4e2a\u5c31\u6bd4\u8f83\u597d\u5b9e\u73b0\u4e86\uff0c\u7528\u4e00\u4e2a\u8ba1\u6570\u53d8\u91cf\uff0c\u4fdd\u5b58\u4e00\u6761\u5c31\u589e\u52a01\uff0c\u6700\u540e\u8fd4\u56de\u8fd9\u4e2a\u53d8\u91cf\u5c31\u53ef\u4ee5\u4e86\u3002
\u5728HTML\u4e0a\u4f20\u5230\u540e\u53f0\u63a5\u6536\u65f6\uff0c\u53c8\u8981\u628a\u63a5\u6536\u5230\u7684\u6587\u4ef6\u6216\u56fe\u7247\u590d\u5236\u5230\u9879\u76ee\u4e0b\u9762\uff0c\u8981\u4e0d\u7136\u5c31\u6ca1\u663e\u793a\u3002\u6240\u4ee5\u81ea\u5df1\u603b\u7ed3\u4e86\u4e0b\u600e\u4e48\u6837\u628a\u6587\u4ef6\u4e0a\u4f20\u5230\u6307\u5b9a\u6587\u4ef6\u76ee\u5f55\u4e0b
\u611f\u89c9\u8fd8\u633a\u5b9e\u7528\u7684\uff0c\u522b\u4eba\u901a\u8fc7\u5c40\u57df\u7f51\u8bbf\u95ee\u6211\u7684\u9879\u76ee\u6765\u4e0a\u4f20\u56fe\u7247\uff0c\u6211\u7684\u9879\u76ee\u4e5f\u80fd\u63a5\u6536\u590d\u5236\u5230\u6307\u5b9a\u76ee\u5f55\u4e0b\uff0c\u670d\u52a1\u5668\u6ca1\u8bd5\u8fc7\uff0c\u4f46\u8fd8\u662f\u6765\u5206\u4eab\u4e0b\u3002
图片,视频都是二进制的,你读取的时候写 rb ,然后你在请求的时候记得把视频转成.content
像这样load_src = requests.get("http://"+srcs[i], headers=headers).content
可以用requests库
requests.put(url, headers, data)
with open(path, 'rb') as f:
requests.post(url,
data=f.read(),
headers={'content-type': 'application/octet-stream'})
绛旓細鍥剧墖锛岃棰戦兘鏄簩杩涘埗鐨勶紝浣犺鍙栫殑鏃跺欏啓 rb 锛岀劧鍚庝綘鍦ㄨ姹傜殑鏃跺欒寰楁妸瑙嗛杞垚.content 鍍忚繖鏍穕oad_src = requests.get("http://"+srcs[i], headers=headers).content
绛旓細su = u'a姹夊瓧b'sl = su.encode('latin1', 'replace')su_g2l = su.encode('gbk').decode('latin1')su_glg = su.encode('gbk').decode('latin1').encode('latin1').decode('gbk')su_g2u = su.encode('gbk').decode('utf8', 'replace')su_gug = su.encode('gbk')...
绛旓細鍦Python涓锛宔ncode鍜宒ecode鏄袱涓牳蹇冩搷浣滐紝鍒嗗埆璐熻矗Unicode瀛楃涓蹭笌瀛楄妭娴涔嬮棿鐨勮浆鎹俥ncode鍑芥暟璐熻矗灏哢nicode瀛楃涓杞崲涓鐗瑰畾缂栫爜鏍煎紡鐨勫瓧鑺傛祦锛岃宒ecode鍒欏弽涔嬶紝灏嗗瓧鑺傛祦杩樺師涓篣nicode瀛楃涓层俇nicode鏄绠楁満绯荤粺澶勭悊瀛楃鐨勬爣鍑嗭紝浣嗚绠楁満浠ヤ簩杩涘埗鏁版嵁宸ヤ綔锛屾墍浠ラ渶瑕佽繖绉嶈浆鎹俥ncode鏂规硶鏄瓧绗︿覆瀵硅薄鐨勪竴涓柟...
绛旓細python3鐨瀛楄妭娴save鏂规硶鐢ㄤ簬鍦ㄥ唴瀛樹腑浠ュ瓧鑺傛祦褰㈠紡淇濆瓨鏂囦欢銆傚畠鎺ュ彈涓涓繀闇鐨勫弬鏁filename锛岃繖鏄淇濆瓨鐨勬枃浠跺悕銆傚彲閫夊弬鏁癮ppend鍜宔xclusive琛ㄧず闄も渨鈥濇ā寮忓杩樺簲璇ヤ娇鐢ㄤ粈涔堟牱鐨勬枃浠惰闂ā寮忋傛鏂规硶涓嶄細鍏抽棴鏂囦欢锛屽洜姝ゅ鏋滀笉寮哄埗鍏抽棴锛屽垯鏂囦欢灏嗕繚鐣欐墦寮鐘舵併
绛旓細a = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]fmt = str(len(a)) + 'H' # 榛樿灏忕妯″紡 struct.pack(fmt, *a) # 鍒楄〃a鍓嶉潰鍔*锛岃〃绀哄皢a瑙e帇鍚庡崟涓紶鍏ュ弬鏁 鎺ㄧ悊锛歛濡傛灉鏄姩鎬佸彉鍖栫殑list锛屾垨鑰呭姩鎬佹埅鍙朼鍏朵腑涓娈垫潵杞崲鎴愬瓧鑺傛祦閮芥槸鍙互鐨 ...
绛旓細绠鍗曟潵璇达紝搴忓垪鍖栫殑杩囩▼灏辨槸灏哖ython瀵硅薄杞崲涓鍙互瀛樺偍鎴栦紶杈撶殑鏍煎紡锛屽瀛楄妭娴鎴鏂囦欢绛夈傝繖绉嶆枃浠跺湪Python绋嬪簭涓粡甯歌鐢ㄤ綔淇濆瓨鍜屽姞杞芥暟鎹殑涓绉嶆墜娈点侾ickle妯″潡鑳藉澶勭悊Python涓鐨勫熀鏈暟鎹被鍨嬶紝濡傚垪琛ㄣ佸瓧鍏哥瓑锛屼互鍙婁竴浜涘鏉傜殑鑷畾涔夊璞°傞氳繃灏嗘暟鎹簭鍒楀寲涓簆ickle鏂囦欢锛岀▼搴忓彲浠ユ柟渚垮湴淇濆瓨宸ヤ綔鐘舵侊紝浠ヤ究...
绛旓細棣栧厛锛宲ickle妯″潡锛屼綔涓Python鍐呯疆鐨勪竴閮ㄥ垎锛屾彁渚涗簡搴忓垪鍖栧拰鍙嶅簭鍒楀寲Python瀵硅薄鐨勫己澶у伐鍏枫備娇鐢╬ickle.dump()淇濆瓨妯″瀷锛屽皢妯″瀷杞崲涓哄瓧鑺傛祦骞跺瓨鍌ㄥ湪鏂囦欢涓紝鑰宲ickle.load()鍒欑敤浜庝粠鏂囦欢涓仮澶嶆ā鍨嬨備緥濡傦細1. 淇濆瓨妯″瀷锛歱ickle.dump(model, 'model.pickle')2. 鍔犺浇妯″瀷锛歮odel = pickle.load(open('...
绛旓細1. 浜嗚В.pkl鏂囦欢 .pkl鏂囦欢鏄Python鐨pickle妯″潡鐢熸垚鐨勬枃浠舵牸寮忋侾ickle妯″潡鍙互灏哖ython瀵硅薄搴忓垪鍖栦负涓涓瀛楄妭娴锛岀劧鍚庡皢杩欎釜瀛楄妭娴佸啓鍏ュ埌涓涓枃浠朵腑锛屼互渚夸簬瀛樺偍鍜屼紶杈撱傝繖鏍凤紝浣犲氨鍙互鍦ㄥ叾浠栧湴鏂归噸鏂板姞杞借繖涓枃浠讹紝鎭㈠鍘熸潵鐨凱ython瀵硅薄銆2. 浣跨敤Python鐨刾ickle妯″潡鎵撳紑.pkl鏂囦欢 涓轰簡鎵撳紑鍜岃鍙.pkl鏂囦欢锛屼綘...
绛旓細Python鐨搴忓垪鍖栦笌鍙嶅簭鍒楀寲鍔熻兘涓昏鐢眕ickle妯″潡瀹炵幇锛屽叾鏍稿績鍦ㄤ簬灏哖ython瀵硅薄杞寲涓瀛楄妭娴锛坧ickling锛夊拰鎭㈠瀵硅薄缁撴瀯锛坲npickling锛夈俻ickle.dumps鐢ㄤ簬灏嗗璞″簭鍒楀寲锛宲ickle.loads鍒欑敤浜庡弽搴忓垪鍖栨暟鎹傚湪灏佸瓨杩囩▼涓紝瀵硅薄鐨勫睘鎬ц鎻愬彇骞朵互閿煎褰㈠紡瀛樺偍锛屽悓鏃跺寘鍚被鍚嶄俊鎭傚弽搴忓垪鍖栨椂锛岄氳繃璇诲彇pickle杈撳叆娴侊紝...
绛旓細搴忓垪鍖栫殑杩囩▼鏄皢瀵硅薄杞崲涓鍙互瀛樺偍鎴栦紶杈撶殑鏁版嵁鏍煎紡锛屽瀛楄妭娴銆傝繖鎰忓懗鐫鑳藉灏Python绋嬪簭涓殑浠讳綍瀵硅薄淇濆瓨涓鏂囦欢锛岃繖浜涘璞″彲浠ユ槸鏁版嵁缁撴瀯濡傚垪琛ㄣ佸瓧鍏革紝鎴栬呮槸澶嶆潅鐨凱ython瀵硅薄銆傝屽弽搴忓垪鍖栧垯鏄簭鍒楀寲鐨勯嗚繃绋嬶紝鍗冲皢瀛樺偍鐨勬暟鎹牸寮忛噸鏂拌浆鎹负鍘熷鐨勫璞°傚洜姝わ紝浣跨敤pickle妯″潡鐢熸垚鐨刾kl鏂囦欢鍙互鍦ㄤ箣鍚庣殑...