0

I'm trying for the first time to remotely deploy my Python flask web app on Heroku and of course I'm encountering some problems. My python code looks like this:

# 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)

And my 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>

This is the error Heruku gives according to the logs

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=

I believe it's a port problem and the cause may be this line in my javascript code

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

not having the possibility to set a static port with Heroku

My Procfile (it's working in local)

Web: python geodesicDistance.py

Any advice?

TylerH
  • 20,799
  • 66
  • 75
  • 101
user2697881
  • 93
  • 2
  • 13

1 Answers1

1

if you look at this page https://devcenter.heroku.com/articles/dynos (search for "web dynos") you see that Heroku sets the environment variable $PORT So, even if you can't specify the port Flask will run on, you can at least know it from the $PORT variable.

Andrea Grandi
  • 925
  • 11
  • 18