# -*- coding:utf-8 -*-
from flask import request, Flask
import time
import os
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'hello world'
# 接受传过来的数据
@app.route("/upload", methods=['POST'])
def get_frame():
print(request.headers)
upload_file = request.files['bfile']
if upload_file:
upload_file.save('C:\\Users\\empty\\Pictures\\t1.jpg')
print("sucess")
return 'success'
else:
return 'failed'
if __name__ == "__main__":
app.run("127.0.0.1", port=8081,debug=True)
上面为服务器端,下面为模拟浏览器端的脚本;
注意post提交的文件以二进制打开。
# -*- coding:utf-8 -*-
"""
@author xxx
@description 模拟客户端请求,注意文件格式
"""
import requests
files = {'bfile':open("C:\\Users\\empty\\Pictures\\cuckoo.jpg",'rb')}
headers = {'content-type': 'multipart/form-data'}
response = requests.post("http://127.0.0.1:8081/upload", files=files,)
print(response.headers)
print(response.text)