What port to use in heroku python app

So I created 2 iOS apps (one sends coordinates, one receives them) and a python server. One of the app is sending GPS coordinates to my python server which is hosted on heroika. The server will then send the received GPS coordinate to the OTHER iOS client application, which will output the Apple Maps PIN to the received coordinate.

The project works fine when testing on localhost with any port specified. However, when I migrated the server to Heroku, I was getting this error The error occurs because Heroku is setting its own port to use where when my code was telling which port to use. I have scoured SO many times over the course of many hours trying to implement other people's solutions where they use os.environ ["PORT"] etc, However, due to my Python newbie and Twisted skills, I was unable to get iOS apps correctly 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()

      

+3


source to share


1 answer


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

I have a similar situation when running Django locally, but a fix like this might help you.

In the heroku dashboard, select your app and go to the settings tab.

Then if you click show config vars and add the key name ON_HEROKU

(or something similar if you like) with a 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') will be correct, I am doing it from my head.

Implementing it in your own code would include 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()

      

+3


source







All Articles