Deploying Flask in Openshift

2019-06-05 19:13发布

问题:

The following codes are working without any problem in my system's localhost... But ain't doing the job on OpenShift.. There is something wrong with my wsgi.py.. Do I have to pass my username and password using environment variables OR I've need to change the localhost ?

The following is the tree of the directory/repository...

myflaskaws
├── requirements.txt
├── setup.py
├── static
│   ├── assets
│   │   ├── style.css
│   └── images
│       ├── no.png
│       └── yes.png
├── templates
│   ├── index.html
│   ├── login.html
│   ├── searchlist.html
│   ├── update.html
├── test.py
├── test.pyc
└── wsgi.py`

wsgi.py

#!/usr/bin/python
import os
virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
    pass
from test import app as application
if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    httpd = make_server('localhost', 8051, application)
    print("Serving at http://localhost:8051/ \n PRESS CTRL+C to Terminate. \n")
    httpd.serve_forever()
    print("Terminated!!")

test.py

from flask import Flask
app = Flask(__name__)

PS : I'm not using "if name == 'main':" in test.py

回答1:

Yes, you do need to use Openshift's environment variables to set up the IP and port.

Try adding in the below code to setup the proper IP and port depending if you are on OS or localhost.

Import os

if 'OPENSHIFT_APP_NAME' in os.environ:              #are we on OPENSHIFT?
    ip = os.environ['OPENSHIFT_PYTHON_IP']
    port = int(os.environ['OPENSHIFT_PYTHON_PORT'])
else:
    ip = '0.0.0.0'                            #localhost
    port = 8051

httpd = make_server(ip, port, application)