python完全接收图像
您好,我在从python客户端接收flask服务器上的文件时遇到问题。我正在将屏幕截图保存在内存中,但无法在服务器端接收它。我很感激我能得到的任何帮助,我对我的要求感到迷茫 服务器代码:python完全接收图像,python,flask,stringio,Python,Flask,Stringio,您好,我在从python客户端接收flask服务器上的文件时遇到问题。我正在将屏幕截图保存在内存中,但无法在服务器端接收它。我很感激我能得到的任何帮助,我对我的要求感到迷茫 服务器代码: @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] filename = secure_filename(file.filename) file.save(os.path.j
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['upload_folder'], filename))
客户端代码:
content_type = 'image/jpeg'
headers = {'content-type': content_type}
from PIL import ImageGrab
from StringIO import StringIO
screen = ImageGrab.grab()
img = stringIO()
screen.save(img, 'JPEG')
img.seek(0)
fd = open('screen.jpg', 'wb')
fd.write(img.getvalue())
fd.close()
fin = open('screen.jpg', 'wb')
files = {'file': fin}
requests.post('http://localhost/upload', files=files)
要上载的文件的格式应为
('filename',fileobj',content\u type')
,因此请更改
files={'file': ('test.jpg', content_type)}
到
文件编号:
文件--(可选)“名称”字典:类似文件的对象(或
用于多部分编码上载的{'name':文件元组})。文件元组可以是
2元组('filename',fileobj),3元组('filename',fileobj,
'内容类型')或4元组('filename',fileobj',内容类型',
自定义标题),其中“内容类型”是定义内容的字符串
给定文件的类型和自定义\u头是类似dict的对象
包含要为文件添加的其他标头
files={'file':('test.jpg',content_type)}
您的图像数据在哪里?感谢您的回复,我已经更新了代码,可以将图像保存到磁盘并发布,没有任何问题,但我想了解如何在不将图像保存到磁盘的情况下仍然发布图像。您好,非常感谢您的回答和文档,我确实错过了这一部分,这是我的错。
files={'file': ('test.jpg', img, content_type)}