【发布时间】:2015-01-12 10:17:50
【问题描述】:
所以我创建了 2 个 iOS 应用程序(一个发送坐标,一个接收坐标)和一个 python 服务器。其中一个应用程序将 GPS 坐标发送到我托管在 heroku 上的 python 服务器。然后,服务器会将接收到的 GPS 坐标发送到 OTHER iOS 客户端应用程序,该应用程序将在接收到的坐标上放置一个 Apple 地图图钉。
在使用任何指定端口的本地主机上进行测试时,该项目可以完美运行。但是,当我将服务器迁移到 Heroku 时,我收到了this error 发生错误是因为 Heroku 设置了它自己的端口供您使用,而我的代码指定了要使用的端口。我已经浏览了好几个小时,试图实现其他人的解决方案,他们使用os.environ["PORT"] 等等,但是由于我的新手 Python 和 Twisted 技能,我没有成功让 iOS 应用程序与 Heroku 服务器正确通信在正确的端口上。我的服务器代码如下:(注意:我使用的是 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()
【问题讨论】: