Simple UDP server

Discussion in 'Python' started by Tzury Bar Yochay, Sep 10, 2008.

  1. I am looking for the right way to write a small and simple UDP server.

    I am wondering between Forking, Threading (found at SocketServer.py)
    and the one describes at the snippet below.

    Can you tell me the advantages and disadvantages of each
    Would the one below will be capable of holding 30 concurrent
    connections?

    I have no intention of using Twisted or alike since I am looking for
    making it as lightweight as possible

    Thanks in advance,
    Tzury Bar Yochay

    # begin of snippet

    from socket import *
    # Create socket and bind to address
    UDPSock = socket(AF_INET,SOCK_DGRAM)
    UDPSock.bind(('',50008))

    while 1:
    data,addr = UDPSock.recvfrom(4*1024)

    if not data:
    print "No data."
    break
    else:
    print 'from:', addr, ' data:', data
    UDPSock.close()
     
    Tzury Bar Yochay, Sep 10, 2008
    #1
    1. Advertising

  2. Tzury Bar Yochay wrote:

    > Would the one below will be capable of holding 30 concurrent
    > connections?


    UDP is a connectionless datagram protocol, so that question doesn't
    really make much sense.

    </F>
     
    Fredrik Lundh, Sep 10, 2008
    #2
    1. Advertising

  3. On Sep 10, 9:55 pm, Fredrik Lundh <> wrote:
    > Tzury Bar Yochay wrote:
    > > Would the one below will be capable of holding 30 concurrent
    > > connections?

    >
    > UDP is a connectionless datagram protocol, so that question doesn't
    > really make much sense.
    >


    So what if it is connectionless.
    It would make sense if you get a load of users who sends large sets of
    binary data to each other.
     
    Tzury Bar Yochay, Sep 10, 2008
    #3
  4. > 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.
     
    Tzury Bar Yochay, Sep 10, 2008
    #4
  5. Tzury Bar Yochay

    James Mills Guest

    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
    <> 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"
     
    James Mills, Sep 10, 2008
    #5
  6. Tzury Bar Yochay

    James Mills Guest

    On Thu, Sep 11, 2008 at 10:36 AM, Nick Craig-Wood <> wrote:
    > For UDP I wouldn't thread or, fork, I'd use select and run
    > asynchronously.
    >
    > http://docs.python.org/lib/module-select.html
    >
    > Actually if I really had to do this I'd use twisted. Right tool for
    > the job!


    For anyone interested, pymills is an
    event-driven, asynchronous library
    geared towards Component architectures.

    It currently uses select for it's socket
    components, TCPServer, TCPClient,
    and it's UDP counter-parts.

    cheers
    James

    --
    --
    -- "Problems are solved by method"
     
    James Mills, Sep 11, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sarath
    Replies:
    0
    Views:
    397
    sarath
    Mar 11, 2006
  2. robert
    Replies:
    2
    Views:
    1,028
  3. Jack Klein
    Replies:
    0
    Views:
    459
    Jack Klein
    Aug 4, 2003
  4. Adam M.
    Replies:
    1
    Views:
    363
    Monisha Barooah
    Aug 4, 2003
  5. Vitali Gontsharuk

    Help needed in a simple UDP socket program

    Vitali Gontsharuk, May 15, 2006, in forum: C Programming
    Replies:
    2
    Views:
    673
    Vitali Gontsharuk
    May 15, 2006
Loading...

Share This Page