为我的网络设备创建一个python系统日志服务器我正在使用下面的代码https://gist.githubusercontent.com/marcelom/4218010/raw/53b643bd056d03ffc21abcfe2e1b9f6a7de005f0/pysyslog.py
这将满足我的需求,但我似乎无法运行任何python版本的sysloghandler。我看到这是旧代码大约5年左右。 我正在运行ubuntu 16.04系统。一切似乎都挂在了尝试上:用于启动服务器。
#!/usr/bin/env python
## Tiny Syslog Server in Python.
##
## This is a tiny syslog server that is able to receive UDP based syslog
## entries on a specified port and save them to a file.
## That's it... it does nothing else...
## There are a few configuration parameters.
LOG_FILE = 'youlogfile.log'
HOST, PORT = "0.0.0.0", 514
#
# NO USER SERVICEABLE PARTS BELOW HERE...
#
import logging
import SocketServer
logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=LOG_FILE, filemode='a')
class SyslogUDPHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = bytes.decode(self.request[0].strip())
socket = self.request[1]
print( "%s : " % self.client_address[0], str(data))
logging.info(str(data))
if __name__ == "__main__":
try:
server = SocketServer.UDPServer((HOST,PORT), SyslogUDPHandler)
server.serve_forever(poll_interval=0.5)
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
print ("Crtl+C Pressed. Shutting down.")
答案 0 :(得分:2)
您的代码适合我。如果我像这样启动服务器:
('127.0.0.1 : ', 'this is a test')
然后发送如下消息:
youlogfile.log
我在stdout上看到输出:
this is a test
文件^[0-9a-z]+([+/*-][0-9a-z]+)+=[0-9a-z]+$
包含:
dataSource: {
data: data2,
group: [{
field: "state",
dir: "desc"
}],
sort: {
field: "name",
dir: "asc"
}
}
我怀疑您的问题源于尝试使用TCP工具连接到UDP服务器。