私の大学のプロジェクトで働いています。私たちはPythonの基礎を学びました。タスクは、fastqファイルを読み込んで解析する小さなプログラムを作成することです。私はボトルを使ってhtmlベースのユーザーインターフェイスを作成するのがいいと思った。しかし、実際のサーバーはありません。
私の問題は次のとおりです。アップロードされたファイルにどのようにアクセスできるか分かりません。コードは新しいディレクトリを作成しません。私は "アップロードされた"ファイルがどこにあるのか、またはサーバーの新しい機能がこのファイルをどのようにつかんで作業できるかもわかりません。
私は、以下のサイトを読んだ:
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 などを。Python3でアップロードしたファイルにアクセスするBottle
また、ファイルをアップロードしようとするとエラーが表示され、そのファイルは既に存在しています。関数save
のクラスUploadFile
があります。これには上書きオプションがありますが、実装方法はわかりません。
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>
ちなみに、私はあなたのコードを実行することをお勧め'pylint'を通して。 –