PSE Entertainment Corp
May 21, 2012, 01:45:59 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Enjoy PSE Tunes!  They're great!
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Python UDP Packet Splitter  (Read 2283 times)
webmast
Guest
« on: April 09, 2008, 06:23:53 PM »

If you've ever needed a method to accept a udp packet of ascii data and split it to a number of other locations, here's a quick and easy Python method:

>>>>>>>>>>>>>>>>>>>>>>>>

import socket

##Create a socket for the inbound UDP packets that we'd like to split.
  
s_in = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s_in.bind(('127.0.0.1',6000))
print "Listening on: %s" % repr(s_in.getsockname())

##Create a socket for the outbound destinations we'd like
##to send the split UDP packets to.

s_out = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

##Here's a list of destination IP's and Ports - this list can go on and on...

destination_list = [('127.0.0.1',10000),('127.0.0.1',10001)]

##Infinite loop to listen and split the data

while 1:
    data,addr = s_in.recvfrom(1024)
    if not data:break
    else:
        for destination in destination_list:
            s_out.connect(destination)
            s_out.send(data)
            print "%s sent to : %s" % (data,destination)
« Last Edit: May 27, 2010, 09:02:24 AM by webmaster » Report to moderator   Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.14 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!