Deploying Flask in Openshift

假如想象 提交于 2019-12-23 02:55:16

问题


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)


来源:https://stackoverflow.com/questions/39051871/deploying-flask-in-openshift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!