0

我第一次尝试在 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

有什么建议吗?

4

1 回答 1

1

如果您查看此页面https://devcenter.heroku.com/articles/dynos(搜索“web dynos”),您会看到 Heroku 设置了环境变量$PORT 所以,即使您不能指定端口,Flask 也会继续运行,您至少可以从$PORT变量中知道它。

于 2013-08-21T08:22:54.890 回答