想請問有接觸過 Heroku twisted 的人
我在自己的電腦已經測試過 twisted的 code
當client去呼叫server時
server會回傳訊息給client
把server的code搬到 heroku後
1.要怎麼啟動他?
2.要怎麼設定ip?
我的目的是想把server放到heroku上能讓client呼叫他並回傳訊息,不一定要用網頁
我使用 git clone
heroku 官方的程式碼
Getting Started on Heroku with Python
下載後做的步驟
1.在requirements.txt裡增加 twisted==1.6.0
2.在Procfile 裡增加 worker: python server.py runserver 0.0.0.0:5000
(server.py 是 twisted的code)
3.git push heroku master
4.到網頁 Resources去啟動 worker: python manage.py runserver 0.0.0.0:5000
有試過在client.py 的 connectTCP設定("0.0.0.0",5000,f),但是沒反應
按了 open app 時會出現錯誤訊息(不過我本來沒有要用網頁....)
log ↓↓↓
at=error code=H14 desc="No web processes running" method=GET path="/" host=bitserver.herokuapp.com request_id=778e3bd2-310d-4d8d-aa75-737a1d89efdc fwd="220.130.40.2" dyno= connect= service= status=503 bytes=
server.py ↓↓↓
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()
client.py ↓↓↓
from twisted.internet import protocol, reactor
class KnockClient(protocol.Protocol):
def connectionMade(self):
self.transport.write("Hey, Heroku!")
def dataReceived(self, data):
if data.startswith("Hi, please wait..."):
print ("Heroku:",data)
self.transport.loseConnection()
reactor.stop()
else:
print ("Heroku:",data)
self.transport.loseConnection()
reactor.stop()
class KnockFactory(protocol.ClientFactory):
protocol = KnockClient
def main():
f = KnockFactory()
reactor.connectTCP("0.0.0.0", 5000, f)
reactor.run()
if __name__ == '__main__':
main()