#!/usr/bin/python
# -*- coding: utf-8 -*-

from socket import socket, AF_INET, SOCK_DGRAM

def create_udp_server_socket(endpoint):
    skt = socket(AF_INET, SOCK_DGRAM)
    skt.bind(endpoint)
    return skt

if __name__ == '__main__':
    ENDPOINT = ("", 3000)       # empty string == INADDR_ANY
    skt = create_udp_server_socket(ENDPOINT)
    while True:
        data, (host, port) = skt.recvfrom(1500)
        print "(%s:%i) %s" % (host, port, data)


#!/usr/bin/python
# -*- coding: utf-8 -*-

from socket import socket, AF_INET, SOCK_DGRAM
from optparse import OptionParser

def create_udp_server_socket(endpoint):
    skt = socket(AF_INET, SOCK_DGRAM)
    skt.bind(endpoint)
    return skt

def MakeOptionParser():
    parser = OptionParser()
    parser.add_option('-a', '--append', dest='append', metavar="FILE",
            help="Append log data to this file")
    return parser

if __name__ == '__main__':
    parser = MakeOptionParser()
    options, args = parser.parse_args()
    append_file = None
    if options.append:
        append_file = open(options.append, 'a', 0)

    ENDPOINT = ("", 3000)       # empty string == INADDR_ANY
    skt = create_udp_server_socket(ENDPOINT)

    while True:
        data, (host, port) = skt.recvfrom(1500)
        log_line = "(%s:%i) %s\n" % (host, port, data)
        print log_line,
        if append_file:
            append_file.write(log_line)


Support for appending to a file
Support for appending to a file
