我正在尝试编写一个 Syslog 侦听器,到目前为止,它在通过 TCP 接受传入消息方面做得很好,但我也希望 UDP 能够正常工作。
这是我正在使用的 UDP 服务器代码,它使用 python 客户端应用程序工作。我还有另一个应用程序,它也可以使用 python 客户端应用程序工作。
# 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()
使用此代码,我可以发送到服务器并让它显示代码。
# 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()
我已尝试使用 Kiwi Syslog 消息生成器和 Snare 将 syslog 消息发送到 UDP 服务器,但没有任何反应。有人可以帮我理解吗?