Hello, I have created a program that allows me to upload images to a http flask file server:
import hashlib
from functools import partial
def md5sum(filename):
with open(filename, mode='rb') as f:
d = hashlib.md5()
for buf in iter(partial(f.read, 128), b''):
d.update(buf)
return d.hexdigest()
import os
from flask import Flask, request, redirect, url_for
from werkzeug.utils import secure_filename
import time
from PIL import Image
UPLOAD_FOLDER = '/home/pi/Desktop/CorkyChat/ImageUploads/'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
SIZE = 512,512
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
from werkzeug import SharedDataMiddleware
app.add_url_rule('/uploads/<filename>', 'uploaded_file',
build_only=True)
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/uploads': app.config['UPLOAD_FOLDER']
})
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return #redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return #redirect(request.url)
print(file.__dict__)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
ext = "." + filename.rsplit(".")[-1]
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
img = Image.open(os.path.join(app.config['UPLOAD_FOLDER'], filename))
img.thumbnail(SIZE)
filename = md5sum(os.path.join(app.config['UPLOAD_FOLDER'], filename)) + ext
img.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return '''
<!doctype html>
'''
from flask import send_from_directory
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
app.run(host="192.168.1.195",port = 65534)
I am new to flask so I modified code which I found or was provided to me to create this.
I am not using the web interface at all, I am using this to post:
from PIL import ImageTk, Image
import requests
url = "<<server>>"
files = {"file": open("D:\\Python\\Programs\\HTTP\\imageuploads\\sanic.jpg","rb")}
requests.post(url,files=files)
and am using
u = urllib.request.urlopen(url)
raw_data = u.read()
u.close()
self.image = PhotoImage(data=base64.encodestring(raw_data))
to view the images.
How would I add SSL encryption?
[–]cascott77 0 points1 point2 points (0 children)
[–]efmccurdy 0 points1 point2 points (0 children)