Getting a UDP socket program in Python to accept messages from a Syslog client?

Posted by Elvar on Stack Overflow See other posts from Stack Overflow or by Elvar
Published on 2010-04-18T17:49:53Z Indexed on 2010/04/18 17:53 UTC
Read the original article Hit count: 282

Filed under:
|
|
|

I'm trying to write a Syslog listener and so far so good on getting it to accept incoming messages through TCP but I also want UDP to function.

This is the UDP server code I'm using, which works using a python client app. I also have another app which also works just using the python client app.

# Server program
# UDP VERSION


from socket import *

# Set the socket parameters
host = "localhost"
port = 514
buf = 1024
addr = (host,port)

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

# Receive messages
while 1:
    data,addr = UDPSock.recvfrom(buf)
    if not data:
        print "Client has exited!"
        break
    else:
        print "\nReceived message '", data,"'"

# Close socket
UDPSock.close()

Using this code I can send to the server and have it display the code.

# Client program

from socket import *

# Set the socket parameters
host = "localhost"
port = 514
buf = 1024
addr = (host,port)

# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

def_msg = "===Enter message to send to server===";
print "\n",def_msg

# Send messages
while (1):
    data = raw_input('>> ')
    if not data:
        break
    else:
        if(UDPSock.sendto(data,addr)):
            print "Sending message '",data,"'....."

# Close socket
UDPSock.close()

I have tried the Kiwi Syslog Message Generator and Snare to send syslog messages to the UDP server and nothing comes up. Could someone help me understand?

© Stack Overflow or respective owner

Related posts about python

Related posts about syslog