首页 > 把通过表单上传的图片 使用StringIO 接收,然后再提交到其它服务器?

把通过表单上传的图片 使用StringIO 接收,然后再提交到其它服务器?

Python: 2.7.9
Flask : 0.10.1
python Flask框架。
通过表单提交的方式,把本地图片上传到A服务器,但是A服务器不要保存此图片,而是要通过A服务器把图片提交到B服务器、C服务器、D服务器上去。如何实现?
了解到python的cStingIO、StringIO 模块 可以实现此功能,搜索了一天了,还是不懂如何应用到此处。请各位不吝赐教!

A服务器上的代码:

import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
import requests
import cStringIO
from cStringIO import StringIO
import Image
UPLOAD_FOLDER = 'c:\uploads_1'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)
else:
    pass
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        upload_files = request.files.getlist('file[]')
        filenames = []
        for file in upload_files:
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #使用StringIO时,请注释掉此行,因为我不需要保存图片在此服务器上。
                filenames.append(filename)

                data = {'file[]': open(os.path.join(app.config['UPLOAD_FOLDER'],filename), 'rb')}

                #buf = cStringIO.StringIO(file)
                #buf.seek(0)
                #buf_img = buf.read()    
                #data = {'file[]': files=buf_img}
                r = requests.post('http://127.0.0.1:5000/',files=data)
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file multiple="" name="file[]">
         <input type=submit value=Upload>
    </form>
    '''
if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=8000)

B服务器上的代码:

import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
import requests

UPLOAD_FOLDER = 'c:\uploads_2'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)
else:
    pass
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        upload_files = request.files.getlist('file[]')
        filenames = []
        for file in upload_files:
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                filenames.append(filename)


    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file multiple="" name="file[]">
         <input type=submit value=Upload>
    </form>
    '''
if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0')


建议你可以查下 FileStorage 的文档.

在你的循环中, file 就是一个标准的 FileStorage 对象, 在你没有把上传的文件保存之前, 它是存在于内存中的, 另外, 它也有一个 read 方法, 读取到的就是文件的二进制流, 然后要怎么操作, 你就怎么操作了.

单个文件上传示例:

pythonfileobj = request.files['filedata']
data = fileobj.read()
【热门文章】
【热门文章】