尝试将Python / flask应用程序部署到Heroku时无法绑定到$ PORT

时间:2013-08-21 07:34:39

标签: javascript python heroku flask procfile

我第一次尝试在Heroku上远程部署我的Python烧瓶网络应用程序,当然我遇到了一些问题。 我的python代码如下所示:

# server settings
HOST = '127.0.0.1'
PORT = int(os.environ.get('PORT', 5000))
DEBUG_MODE = True
...
#functions definition
...
app = Flask(__name__)

@app.route("/geodesicDistance")
def geodesicDistance():
    t_start = time.time()
    query = parse_query(request.args)
    if query:
        position = geocode_address(query)
         if (position) and (len(position[u'results'])>0):
            lat = position[u'results'][0][u'geometry'][u'location'][u'lat']
            lng = position[u'results'][0][u'geometry'][u'location'][u'lng']
            response = create_response(200,
                                       'Good query',
                                       lat,
                                       lng,
                                       position[u'results'][0][u'formatted_address'],
                                       get_distance(lat,lng)
                                       )
        else:
            response = create_response(400,
                                       'Bad formed query',
                                       '',
                                       '',
                                       '',
                                       '')
    else:
        response = create_response(204,
                                   'No query made',
                                   givenAddress['lat'],
                                    givenAddress['lng'],
                                   'White Bear Yard, 144a Clerkenwell Road, London,                              EC1R 5DF, UK',
                                   0.0
                                   )
    response['result']['elapsed'] = (time.time() - t_start)*1.0e3
    http_response = make_response(json.dumps(response))
    http_response.headers['Content-type'] = 'application/json'
    http_response.headers['Access-Control-Allow-Origin'] = '*'
    return http_response

if __name__=="__main__":
  app.run(debug=DEBUG_MODE, host=HOST, port=PORT)

我的JavaScript:

<script type="text/javascript">
    function checkGeolocation() {
        if (navigator.geolocation) {
            refresh();
        } else {
           $('#results')[0].innerHTML = "<p>Your browser doesn't support geolocation <br /></p>";
               }
        }

        function getDistance(position) {
            url = 'http://127.0.0.1:5000/geodesicDistance';
            query  =position.coords.latitude.toString()+','+position.coords.longitude.toString();
                     $.get(url, {'q': query},function(data) {
                         $('#results .distance')[0].innerHTML = Math.round(data['result']['distance']*1000)/1000;

                     })
                 }

      function handleError(error) {
         alert(error.message);
      }

      function refresh() {
          navigator.geolocation.getCurrentPosition(getDistance,handleError);
       }

         $(document).ready(function(){
             checkGeolocation();
          });
</script>

这是Heruku根据日志提供的错误

2013-08-21T00:12:55.985496+00:00 heroku[web.1]: Starting process with command `python         geodesicDistance.py`
2013-08-21T00:12:57.326395+00:00 app[web.1]:  * Running on http://127.0.0.1:12176/
2013-08-21T00:12:57.326395+00:00 app[web.1]:  * Restarting with reloader
2013-08-21T00:13:56.914333+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process     failed to bind to $PORT within 60 seconds of launch
2013-08-21T00:13:56.914595+00:00 heroku[web.1]: Stopping process with SIGKILL
2013-08-21T00:13:58.185305+00:00 heroku[web.1]: Process exited with status 137
2013-08-21T00:13:58.205874+00:00 heroku[web.1]: State changed from starting to crashed
2013-08-21T00:18:23.999482+00:00 heroku[router]: at=error code=H10 desc="App crashed"      method=GET path=/ host=stormy-sands-9363.herokuapp.com fwd="93.46.195.73" dyno= connect=  service= status=503 bytes=

我认为这是一个端口问题,原因可能是我的javascript代码中的这一行

url = 'http://127.0.0.1:5000/geodesicDistance';

无法使用Heroku设置静态端口

我的Procfile(它在本地工作)

Web: python geodesicDistance.py

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

如果您查看此页面https://devcenter.heroku.com/articles/dynos(搜索“网络dynos”),您会看到Heroku设置了环境变量 $ PORT 因此,即使您无法指定Flask将运行的端口,您至少可以从 $ PORT 变量中了解它。