views:

484

answers:

2

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?

+1  A: 

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

Alex Martelli
+1 nice, I don't see anything wrong with that so I'll probably follow the example. I asked because I was reading twisted docs and was googling around to see example of push producer with tcp server ... but you're right for my purpose the example you provided is good enough (yes I'll add reading from file that's not a problem ...)
stefanB
@stefanB, OK, if you come up with problems (e.g. that sleep call blocking your server in undesirable ways) let us know (ideally by editing your question AND adding a comment here so Stack Overflow will let me know automatically;-) and we'll see what can be done!-)
Alex Martelli
I was thinking that if for say next 5 minutes I sit in connectionMade() and keep sending data to client I will probably not get any other events ... but I'll see what happens (depends how much time I'll have to play with it)
stefanB
@stefanB, right -- time.sleep is a blocking call for a single-threaded server. Consider `twisted.internet.service.TimerService`, `twisted.internet.task.LoopingCall`, `reactor.callLater`, etc, if that's a problem for your application!!!
Alex Martelli
I think using a blocking call and/or using loops in an event driven framework like Twisted tends to make the framework useless.
Manuel Faux
A particular problem this approach has is that none of the data will be written until `connectionMade` returns. This is equivalent to: time.sleep(len(thedata)); self.transport.write(''.join(thedata)); self.transport.loseConnection(). If the goal is to send one line per second, using a LoopingCall (or TimerService, etc) is a good idea. This is different from the idea of a "producer" in Twisted, which is something that writes whenever there is room in the buffer to do so.
Jean-Paul Calderone
+3  A: 

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

Benjamin Rutt