본문 바로가기

리눅스

tiangolo/uwsgi-nginx 사용해서 웹과 flask 사용하기(pyinstaller)

반응형

이전 글에서 (https://bagng.tistory.com/246) flask를 사용하는 docker를 만들어 보았습니다.

이번에는 여기에 pyinstaller로 실행화일을 만드는 방법을 보여드리겠습니다.

 

화일 구조는 다음과 같습니다.

~+ docker
 ├── app
 │   ├── dist
 │   │   └── test
 │   ├── static
 │   │   └── index.html 
 │   ├── main.py
 │   ├── test.py
 │   ├── test.spec
 │   ├── uwsgi.ini
 │   └── index.js
 ├── docker-compose.yml
 ├── Dockerfile
 ├── endtrypoint.sh
 ├── requirements.txt
 └── uwsgi.ini 

 

test.py를 사용해서 pyinstaller로 실행화일을 만들어 봅니다.

import sys
print (sys.path)

import traceback

from flask import Flask
from flask import request
from flask import Response

app = Flask(__name__)

DEBUG_ENABLE = True

@app.route('/test', methods=['GET', 'POST', 'OPTIONS'])
def handler_test():
    try:
        res = ''
        res += 'request.path = {0}<br>\n'.format(request.path)
        res += 'request.args = {0}<br>\n'.format(request.args) # GET DATA
        res += 'request.data = {0}<br>\n'.format(request.data)
        res += 'request.form = {0}<br>\n'.format(request.form) # POST DATA
        res += 'request.method = {0}<br>\n'.format(request.method)
        res += 'request.headers = {0}<br>\n'.format(str(request.headers))

        res += "<br><br>Hello World from Flask using Python & Docker - test.py by pyinstaller."
        return res, 200
    except Exception as e:
        #print(traceback.format_exc())
        #traceback.print_exc()
        #result['data'] = traceback.format_exc()
        #print(result['data'])
        return traceback.format_exc()

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8000, debug=DEBUG_ENABLE)
    #app.run(host='0.0.0.0', port=8080, ssl_context='adhoc')

main.py와 다른 점은 사용하는 port가 8080에서 8000으로 변경되고 출력하는 내용을 약간 수정하였습니다.

 

화일 구조에 보시면 dist 폴더가 있고 여기에 test라는 실행화일이 있습니다.

 

pyinstaller로 test 화일을 만들기 위해서는 docker에서 exec로 실행한 상태에서 pyinstaller를 설치합니다.

docker exec -it sample-server /bin/bash

pip install pyinstaller

 

pyinstaller를 설치했으면 컴파일을 합니다.

cd /app

pyinstaller --onefile test.py

 

dist 폴더에 보면 test라는 화일이 만들어 집니다.

이를 실행하려면 

nohup ./test -sS &

를 입력하면 됩니다.

 

 test는 8000 port를 사용하는 것으로 되어 있습니다.

따라서 nginx.conf는 다음과 같이 변경합니다.

server {
    location / {
        alias /app/static/;
    }

    location ~ ^/(hello) {
        try_files $uri @app;
    }

    location @app {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }

    location ~ /index.html {
        root /app/static/;
        add_header 'Cache-Control' 'no-store';
    }

    location /test/ {
        proxy_pass http://127.0.0.1:8000/test;
    }
}

 

github에 업데이트해서 올려 놓았습니다.

https://github.com/bagng/tiangolo-uwsgi-nginx

 

GitHub - bagng/tiangolo-uwsgi-nginx

Contribute to bagng/tiangolo-uwsgi-nginx development by creating an account on GitHub.

github.com

 

반응형