使用 Python3 Bottle 访问上传的文件
Access an uploaded file with Python3 Bottle
我在为我的大学做一个项目。我们已经学习了 python 的基础知识。任务是创建一个读取 fastq 文件并对其进行分析的小程序。我认为用 bottle 创建一个基于 html 的用户界面会很好。但是没有真正的服务器。
我的问题是:我不知道如何访问上传的文件。该代码不会创建新目录。我也不知道在哪里可以找到 "uploaded" 文件或服务器站点上的新功能如何获取该文件并使用它。
我已阅读以下网站:
http://bottlepy.org/docs/dev/tutorial.html#file-uploads
http://bottlepy.org/docs/dev/api.html#bottle.FileUpload.file
How do I access an uploaded file with Bottle?
Bottle file upload and process
和其他人。
此外,当我尝试上传文件时出现的错误让我很烦,错误是文件已经存在。有class UploadFile
函数save
,它有一个覆盖选项,但我不知道如何实现它。
bottle_test.py:
from bottle import route, run, template, static_file, request, response, url, default_app, get, post, FileUpload
import bottle
import os
# Aufrufen der Hauptseite
@route('/')
def index():
return template('main_template')
# Einbinden unterschiedlicher Dateien z.B. Bilder oder CSS-Files
@route('/static/style/<filepath:re:.*\.css>')
def server_static(filepath):
return static_file(filepath, root='static/style')
@route('/static/images/<filepath:re:.*\.(jpg|png|gif|ico|svg)>')
def img(filepath):
return static_file(filepath, root="static/images")
@route('/static/sonstige-bilder/<filepath:re:.*\.(jpg|png|gif|ico|svg)>')
def img(filepath):
return static_file(filepath, root='static/sonstige-bilder')
# Formularabfrage
@route('/repeat', method='POST')
def do_login():
username = request.forms.get('username')
password = request.forms.get('password')
if username == 'arsenij' and password == '1234':
return "<p>Your login information was correct.</p>"
else:
return "<p>Login failed.</p>"
@route('/upload', method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if ext not in ('.fastq'):
return 'File extension not allowed.'
save_path = '/tmp/(category)'
if not os.path.exists(save_path):
os.makedirs(save_path)
file_path = "{path}/{file}".format(path=save_path, file=upload.filename)
upload.save(file_path)
print(request.files.get('upload'))
return 'File uploaded'
if __name__ == '__main__':
bottle.debug(True)
bottle.run(host='0.0.0.0', port=8080, reloader=True)
main_template.tpl
<form action="/upload" method="post" enctype="multipart/form-data">
Category: <input type="text" name="category" />
Select a file: <input type="file" name="upload" />
<input type="submit" value="Start upload" />
</form>
documentation for FileUpload.save 展示了如何做到这一点:
upload.save(file_path, overwrite=True)
我在为我的大学做一个项目。我们已经学习了 python 的基础知识。任务是创建一个读取 fastq 文件并对其进行分析的小程序。我认为用 bottle 创建一个基于 html 的用户界面会很好。但是没有真正的服务器。
我的问题是:我不知道如何访问上传的文件。该代码不会创建新目录。我也不知道在哪里可以找到 "uploaded" 文件或服务器站点上的新功能如何获取该文件并使用它。
我已阅读以下网站:
http://bottlepy.org/docs/dev/tutorial.html#file-uploads
http://bottlepy.org/docs/dev/api.html#bottle.FileUpload.file
How do I access an uploaded file with Bottle?
Bottle file upload and process
和其他人。
此外,当我尝试上传文件时出现的错误让我很烦,错误是文件已经存在。有class UploadFile
函数save
,它有一个覆盖选项,但我不知道如何实现它。
bottle_test.py:
from bottle import route, run, template, static_file, request, response, url, default_app, get, post, FileUpload
import bottle
import os
# Aufrufen der Hauptseite
@route('/')
def index():
return template('main_template')
# Einbinden unterschiedlicher Dateien z.B. Bilder oder CSS-Files
@route('/static/style/<filepath:re:.*\.css>')
def server_static(filepath):
return static_file(filepath, root='static/style')
@route('/static/images/<filepath:re:.*\.(jpg|png|gif|ico|svg)>')
def img(filepath):
return static_file(filepath, root="static/images")
@route('/static/sonstige-bilder/<filepath:re:.*\.(jpg|png|gif|ico|svg)>')
def img(filepath):
return static_file(filepath, root='static/sonstige-bilder')
# Formularabfrage
@route('/repeat', method='POST')
def do_login():
username = request.forms.get('username')
password = request.forms.get('password')
if username == 'arsenij' and password == '1234':
return "<p>Your login information was correct.</p>"
else:
return "<p>Login failed.</p>"
@route('/upload', method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if ext not in ('.fastq'):
return 'File extension not allowed.'
save_path = '/tmp/(category)'
if not os.path.exists(save_path):
os.makedirs(save_path)
file_path = "{path}/{file}".format(path=save_path, file=upload.filename)
upload.save(file_path)
print(request.files.get('upload'))
return 'File uploaded'
if __name__ == '__main__':
bottle.debug(True)
bottle.run(host='0.0.0.0', port=8080, reloader=True)
main_template.tpl
<form action="/upload" method="post" enctype="multipart/form-data">
Category: <input type="text" name="category" />
Select a file: <input type="file" name="upload" />
<input type="submit" value="Start upload" />
</form>
documentation for FileUpload.save 展示了如何做到这一点:
upload.save(file_path, overwrite=True)