在Heroku python应用程序上使用哪个端口 [英] What port to use on heroku python app

查看:171
本文介绍了在Heroku python应用程序上使用哪个端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了2个iOS应用程序(一个发送坐标,一个接收坐标)和一个python服务器.其中一个应用程序将GPS坐标发送到托管在heroku上的python服务器.然后,服务器会将接收到的GPS坐标发射到OTHER iOS客户端应用程序,该应用程序将在接收到的坐标上放置Apple Maps引脚.

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.

在具有任何指定端口的本地主机上进行测试时,该项目可以完美运行.但是,当我将服务器迁移到Heroku时,我收到 os.environ ["PORT"] 等,但是由于我的Python和Twisted新手技能,我未能成功使iOS应用正确地与正确端口上的Heroku服务器通信.我的服务器代码如下:(注意:我正在使用Twisted)

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

推荐答案

Heroku在您的设置中有一个部分可以定义环境变量.

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

在本地运行Django时,我遇到类似的情况,但是类似的修复程序可能会对您有所帮助.

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

在heroku仪表板中,选择您的应用程序,然后单击设置"选项卡.

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

然后,如果单击揭示配置变量",然后将键名ON_HEROKU(或类似的名称,如果需要)添加为值True.

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

然后在您的python中:

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

我不确定100%get('PORT')是否正确,这是我的首要任务.

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

这篇关于在Heroku python应用程序上使用哪个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆