[Twisted-Python] Multiple clients and ports

Carlos Eduardo Sotelo Pinto carlos.sotelo.pinto at gmail.com
Thu Apr 18 20:33:49 EDT 2013

I am doing a gps tracker wich use multiple protocols and multiple
clients, my problen is when I have more than one client on more than
one protocol at the same time, data and validation start to crashing
one to other

My Server is

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Heracles Tracker Listener Server."""

from twisted.internet import threads
from twisted.protocols.gps.nmea import NMEAReceiver
from twisted.internet.protocol import Factory, Protocol
import binascii

#class TrackerServer(NMEAReceiver):
#class TrackerServer(LineReceiver):
class TrackerServer(Protocol):
    """Tracker Twisted Protocol Class."""

    def __init__(self, decoder):
        """Class Constructor."""
        decoderModule = __import__('listener.protocols.%sDecoder' %
(decoder, ), fromlist=['%sDecoder' % (decoder, )])
        decoderClass = getattr(decoderModule, '%sDecoder' % (decoder, ))
        self.decoder = decoderClass()
        #self.delimiter = '0';

    def connectionMade(self):
        """ConnectionMade Twisted event."""
            print 'Connection made to', self.transport.getHost(),
'from', self.transport.getPeer()
        except ValueError:
            print "Oops!  Connection was not started"

    def connectionLost(self, reason):
        """ConnectionLost Twisted event."""
        print "Connection lost ", reason

    def dataReceived(self, data):
        Protocol.dataReceived(self, data)

        """DataReceived Twisted event."""
            #d = threads.deferToThread(self.decoder.processDatagram(data ))
        except ValueError:
            print "Oops!  That was no valid data.  Try again..."

    def sendResponse (self, response):

class TrackerFactory(Factory):
    """Tracker Factory Twisted Class."""

    def __init__(self, decoder):
        """Class Constructor. """
        self.decoder = decoder

    def buildProtocol(self, addr):
        """BuildProtocol Twisted."""
        return TrackerServer(self.decoder)

My tac file is

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys
import ConfigParser
from twisted.application import internet, service
from listener.TrackerServer import TrackerFactory

PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))

path = None
config = ConfigParser.ConfigParser()

application = service.Application("tracker")
for protocol in config.get('protocols', 'keys').split(','):
    port = int(config.get(protocol, 'port'))
    factory = TrackerFactory(config.get(protocol, 'name'))
    trackerService = internet.TCPServer(port, factory) # create the service

Carlos Eduardo Sotelo Pinto | http://carlossotelo.com | csotelo at twitter
    GNU Linux Admin | PHP Senior Web Developer
    Mobil: RPC (Claro)+51, 958194614 | Mov: +51, 959980794
    GTalk: carlos.sotelo.pinto at gmail.com | Skype: csotelop
    MSN: carlos.sotelo.pinto at gmail.com | Yahoo: csotelop
    GNULinux RU #379182 | GNULinux RM #277661
GPG FP:697E FAB8 8E83 1D60 BBFB 2264 9E3D 5761 F855 4F6B

More information about the Twisted-Python mailing list