ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Web] Python Flask Simple 업로드
    Study/Web 2023. 1. 11. 14:09

    Flask 란?

    파이썬 웹 프레임워크로 Django에 비해 간단하며 가벼운게 특징


    Flask 파일업로드 샘플

    가끔 간단한 파일 업로드 서버가 필요할 때가 있어 작성해둠

    python3 기준으로 작성함

     

    Step 1. 

    flask 패키지 설치

    pip install flask

     

    Step 2.

    Flask 소스를 둘 폴더 생성 - ex) "flask_upload"

    Flask 폴더 안에 html 소스를 둘 폴더 "templates" 생성

    upload.py 파일 작성

    upload.py

    from distutils.log import debug
    from fileinput import filename
    from flask import *  
    app = Flask(__name__)  
      
    @app.route('/')  
    def main():  
        return render_template("index.html")  
      
    @app.route('/success', methods = ['POST'])  
    def success():  
        if request.method == 'POST':  
            f = request.files['file']
            f.save(f.filename)  
            return render_template("success.html", name = f.filename)  
      
    if __name__ == '__main__':  
        app.run('0.0.0.0',port=9999,debug = True)

     

    Step 3.

    templates 하위 폴더에 index.html, success.html 작성

    index.html

    <html>  
    <head>  
        <title>upload the file : GFG</title>  
    </head>  
    <body>  
        <form action = "/success" method = "post" enctype="multipart/form-data">  
            <input type="file" name="file" />  
            <input type = "submit" value="Upload">  
        </form>  
    </body>  
    </html>

    success.html

    <html>
       <head>
          <title>success</title>
       </head>
       <body>
          <p>File uploaded successfully</p>
          <p>File Name: {{name}}</p>
       </body>
    </html>

     

    Step 4.

    업로드 테스트

    python upload.py

     

    참고 - https://www.geeksforgeeks.org/how-to-upload-file-in-python-flask/

     

     

    'Study > Web' 카테고리의 다른 글

    [Web] 보안헤더  (0) 2023.01.16
Designed by Tistory.