我是Twisted web服務器和Heroku的新手。想要在Heroku上使用Twisted web服務器
我想在Heroku
使用雙絞線Web服務器我使用這樣
from twisted.internet import protocol, reactor
import os
class Knock(protocol.Protocol):
def dataReceived(self, data):
print 'Client:', data
if data.startswith("Hey, Heroku!"):
response = "Hi, please wait..."
self.transport.write(response)
else:
response = "I don't know who you are!"
self.transport.write(response)
class KnockFactory(protocol.Factory):
def buildProtocol(self, addr):
return Knock()
reactor.listenTCP(5000, KnockFactory())
reactor.run()
我該怎麼做才能連接在Heroku服務器的客戶端代碼?
更新2017年3月19日
我用例子來修改從Python and Django on Heroku
我設定的端口:在我的代碼5000,但端口返回從開擴應用程序隨機的。
我仍然無法連接Heroku上的服務器。 :(
app.py
import os
from flask import Flask
from twisted.internet import protocol, reactor
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello from Python Twisted ! Heroku server started on port: %s" % port
class Knock(protocol.Protocol):
def dataReceived(self, data):
return "Client:", data
if data.startswith("Hey, Heroku!"):
response = "Hi, please wait..."
self.transport.write(response)
else:
response = "I don't know who you are!"
self.transport.write(response)
class KnockFactory(protocol.Factory):
def buildProtocol(self, addr):
return Knock()
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
reactor.listenTCP(port, KnockFactory())
reactor.run()
@ JohnZwinck你能幫助我嗎? – soysoy