0

I am new for Twisted web server and Heroku.

I want to use Twisted web server on Heroku

I use the client code like this

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

How can I do to connect server on Heroku?

update 2017/3/19

I use the example to modify from Python and Django on Heroku

I set port:5000 in my code but the port returned is random from openning the app.

I still can't connect the server on 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()
Community
  • 1
  • 1
soysoy
  • 289
  • 2
  • 10

1 Answers1

1

Heroku tells you what port to listen on using an environment variable.

You should listen on int(os.environ["PORT"]) instead of hard-coding port 8080.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
  • Sorry... I don't know what you mean. You mean I have to set the environment variable on heroku, isn't it? – soysoy Mar 06 '17 at 01:12
  • Do you have any idea why "App crashed" ? I went to look my error in [Heroku Error Codes](https://devcenter.heroku.com/articles/error-codes#r10-boot-timeout) ,but I don't know how to solution the error. – soysoy Mar 07 '17 at 01:52
  • Maybe there are some syntax errors? There are some syntax errors in the code in your question. If the code in your question isn't identical to what you're running on Heroku, I can't really tell. – Jean-Paul Calderone Mar 07 '17 at 14:31