Question

I want to put together simple TCP server using Python and Twisted.

The server starts up and waits for connection - I already have client - non-python application. Once connection is made server starts sending data at some interval (e.g. 1 sec).

The server reads data from a static file (a record at a time), I should be able to figure out this part.

I assume that I would use push producer to start pushing data once client is connected.

I have simple tcp server with factory in twisted and I can react to connectionMade/dataReceived and so on but I can't figure out how to plug in the push producer.

Anyone knows any examples showing push producer with tcp server in twisted?

Was it helpful?

Solution

What about something simplistic like:

thedata = '''
Questa mattina
mi son svegliato
o bella ciao, bella ciao,
bella ciao, ciao, ciao
questa mattina
mi son svegliato
ho trovato l'invasor!
'''.splitlines(True)

class Push(protocol.Protocol):
    """This is just about the simplest possible protocol"""
    def connectionMade(self):
        for line in thedata:
          if not line or line.isspace():
            continue
          self.transport.write(line)
          time.sleep(1.0)
        self.transport.loseConnection()

This has hard-coded data, but you say that reading it from a file instead is not your problem. If you can tell us what's wrong with this overly simplistic "push server", maybe we can offer better help!-)

OTHER TIPS

Here is a complete example of a push producer. It's been added to the twisted svn as an example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top