Aimee
Aimee

Reputation: 143

What port to use on heroku python app

So I have created 2 iOS apps (One sends coordinates, one receives them) and a python server. One of the apps sends GPS coordinates to my python server that is hosted on heroku. The server will then emit the received GPS coordinate to the OTHER iOS client app that will drop an Apple Maps pin on the received coordinate.

The project works perfectly while testing on local host with any specified port. However when I have migrated the server to Heroku I was receiving this error The error occurs because Heroku sets it's own port for you to use, where as my code was specifying which port to use. I have been browsing SO for numerous hours trying to implement other peoples solutions where they use os.environ["PORT"] and so on, however due to my novice Python and Twisted skills I haven't succeeded in getting the iOS apps to properly communicate with the Heroku server on the right port. My code for my server is below: (note: I am using Twisted)

import os
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor

class IphoneChat(Protocol):
def connectionMade(self):
    #self.transport.write("""connected""")
    self.factory.clients.append(self)
    print "clients are ", self.factory.clients

def connectionLost(self, reason):
    self.factory.clients.remove(self)

def dataReceived(self, data):
    #print "data is ", data
    a = data.split(':')
    if len(a) > 1:
        command = a[0]
        content = a[1]

        msg = ""
        if command == "new":
            self.name = content
            msg = content

        elif command == "msg":
            msg = self.name + ": " + content

        print msg

        for c in self.factory.clients:
            c.message(msg)

def message(self, message):
    self.transport.write(message + '\n')


factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
port = 3000
reactor.listenTCP(port, factory)
print "Iphone Chat server started on port ", port
reactor.run()

Upvotes: 8

Views: 7620

Answers (2)

vintagexav
vintagexav

Reputation: 2057

The answer is the following. The port is set by Heroku in the environment variables, and in this example 17995 is used only locally when the PORT environment variable is absent (on local).

port = int(os.environ.get("PORT", 17995))
app.run(host='0.0.0.0', port=port)

Source: https://blog.heroku.com/python_and_django

Upvotes: -1

Llanilek
Llanilek

Reputation: 3466

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

Upvotes: 13

Related Questions