What port to use on heroku python app

别来无恙 提交于 2020-04-12 21:21:08

问题


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

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/27899666/what-port-to-use-on-heroku-python-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!