Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Simple UDP server

Reply
Thread Tools

Simple UDP server

 
 
Tzury Bar Yochay
Guest
Posts: n/a
 
      09-10-2008
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(('',5000)

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

if not data:
print "No data."
break
else:
print 'from:', addr, ' data:', data
UDPSock.close()
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      09-10-2008
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>

 
Reply With Quote
 
 
 
 
Tzury Bar Yochay
Guest
Posts: n/a
 
      09-10-2008
On Sep 10, 9:55*pm, Fredrik Lundh <fred...@pythonware.com> 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.
 
Reply With Quote
 
Tzury Bar Yochay
Guest
Posts: n/a
 
      09-10-2008
> 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.
 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      09-10-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
<> 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"
 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      09-11-2008
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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a Simple Java udp portscanner robert Java 2 05-23-2006 12:13 AM
maintain state in web server while calling another server (UDP) sarath Java 0 03-11-2006 02:12 PM
udp (0) -> udp (0) traffic ? Tom Cisco 2 03-04-2004 06:06 PM
Re: this very simple UDP client/server.. doesn't work? Adam M. C Programming 1 08-04-2003 11:09 AM
Re: this very simple UDP client/server.. doesn't work? Jack Klein C Programming 0 08-04-2003 05:32 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57