What port to use on heroku python app

Heroku have a section in your settings where you can define environment variables.

I have a similar situation when running Django locally, but a similar fix may help you.

In heroku dashboard, select your app and then click the settings tab.

Then if you click reveal config vars and add the key name ON_HEROKU (or something similar if you prefer) with the value True.

Then in your python:

import os
ON_HEROKU = os.environ.get('ON_HEROKU')

if ON_HEROKU:
    # get the heroku port
    port = int(os.environ.get('PORT', 17995))  # as per OP comments default is 17995
else:
    port = 3000

I'm not 100% sure if get('PORT') would be correct, I'm doing this off the top of my head.

Implementing it into your own code would involve something like:

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []

import os
ON_HEROKU = os.environ.get('ON_HEROKU')
if ON_HEROKU:
    # get the heroku port 
    port = int(os.environ.get("PORT", 17995))  # as per OP comments default is 17995
else:
    port = 3000

reactor.listenTCP(port, factory)
print "Iphone Chat server started on port %s" % port
reactor.run()