Simple UDP server

James Mills prologic at shortcircuit.net.au
Thu Sep 11 00:00:41 CEST 2008


Tzury,

You may consider using pymills
to simplify writing your UDP server
and just concentrating on the
behavior of the system.

You can get a copy of the
latest development branch
by cloning it with Mercurial:

hg clone http://hg.shortcircuit.net.au/pymills/

There is an example UDP Server
in examples/net/ but I'll paste
it here for your reference.

Note, as stated before, UDP is a connectionless
protocol (Datagram), concurrency doesn't apply.
Also note, technically UDP doesn't guarantee
reliability or the order of packets, however the
reality is, it doesn't give you any feedback. You
have to handle this yourself.

Have fun,

cheers
James

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set sw=3 sts=3 ts=3

from pymills import event
from pymills.event import *
from pymills.net.sockets import UDPServer

class EchoServer(UDPServer):

   @listener("connect")
   def onCONNECT(self, sock, host, port):
      print "New connection: %s:%d" % (host, port)

   @listener("disconnect")
   def onDISCONNECT(self, sock):
      print "Disconnection: %s" % sock

   @listener("read")
   def onREAD(self, sock, line):
      line = line.strip()
      print "%s: %s" % (sock, line)

   @listener("error")
   def onERROR(self, sock, msg):
      print "ERROR (%s): %s" % (sock, msg)

def main():
   server = EchoServer(1234)
   event.manager += server

   while True:
      try:
         manager.flush()
         server.poll()
      except KeyboardInterrupt:
         break

if __name__ == "__main__":
   main()

On Thu, Sep 11, 2008 at 5:33 AM, Tzury Bar Yochay
<Afro.Systems at gmail.com> wrote:
>> Transmitting large binary data over UDP? That makes only sense for few
>> applications like video and audio streaming. UDP does neither guarantee
>> that your data is received nor it's received in order. For example the
>> packages A, B, C, D might be received as A, D, B (no C).
>>
>> Can your protocol handle missing packages and out of order packages?
>
> I intend of using it for audio transmission and don't care about lose
> or out of order.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
--
-- "Problems are solved by method"



More information about the Python-list mailing list