Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.6 / 3....
  • Linux Kernel 3.0.82 LTS...
  • KDE Software Compilatio...
  • PulseAudio 4.0
  • Wireshark 1.10.0
  • NetworkManager 0.9.8.2
  • LibreOffice 3.6.6 / 4.0...
  • SystemRescueCd 3.7.0
  • Linux Kernel 3.10 RC6
  • Ubuntu Tweak 0.8.5
  • Home > Linux > Programming > Libraries

    ltprotocol 0.2.1

    Download button

    No screenshots available
    Downloads: 464  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    David Underhill | More programs
    GPL / FREE
    September 16th, 2009, 11:17 GMT
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    ltprotocol description

    Twisted-based client and server for protocols which begin with a length and type field

    ltprotocol is a Python module that provides a Twisted-based client and server implementation for protocols which begin with a legnth and type field. Create your protocol by constructing an LTProtocol with a list of LTMessage objects which specify your protocol. Use LTTwistedServer and LTTwistedClient to create a server or client.

    Example: A complete example can be downloaded here. To try it out, run:

     python test_lpprotocol.py server
     python test_lpprotocol.py client

    To use it, you first define the messages in your protocol:

    class NumMsg(LTMessage):
     @staticmethod
     def get_type():
     return 1
     
     def __init__(self, n):
     LTMessage.__init__(self)
     self.num = n
     
     def pack(self):
     return struct.pack("> I", self.num)
     
     @staticmethod
     def unpack(body):
     return NumMsg(struct.unpack("> I", body)[0])
     
     def __str__(self):
     return str(self.num)
     
    class StrMsg(LTMessage):
     @staticmethod
     def get_type():
     return 2
     
     def __init__(self, s):
     LTMessage.__init__(self)
     self.str = s
     
     def pack(self):
     return struct.pack("> %us" % len(self.str), self.str)
     
     @staticmethod
     def unpack(body):
     return StrMsg(struct.unpack("> %us" % len(body), body)[0])
     
     def __str__(self):
     return self.str


    When your client or server receives data, it posts a callback to a method you specify. For this example, I am going to use this helper function to simply print out the messages we receive:

    def print_ltm(prefix, ltm):
     print '%s got: %s' % (prefix, str(ltm))


    To create a client, I would do something like this (the second argument is the function to call when a complete message has arrived):

    p = LTProtocol([NumMsg, StrMsg])
    client = LTTwistedClient(p, lambda m : print_ltm('client', m))
    client.connect('127.0.0.1', 9999)


    Likewise, to create a server I would do something like this:

    p = LTProtocol([NumMsg, StrMsg])
    server = LTTwistedServer(p, lambda m : print_ltm('server', m))
    server.listen(9999)


    You may want to override the buildProtocol() method of the LTTwistedClient class to have the client send some message to the server when it first connects rather than waiting for the server to send it something. Alternatively, you might subclass the LTTwistedProtocol class (setting LTTwistedServer.protocol to your new subclass) in order to have the server send some message to the client when it connects, or to take some special action when the client disconnects.


    Product's homepage

    Requirements:

    · Python

      


    TAGS:

    Twisted client | Twisted server | Python library | Python | Twisted | server

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM