-
-
Save infinitylx/8f373d4295c3f2afee80d151be09a44e to your computer and use it in GitHub Desktop.
Tiny Python3 Syslog Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
## Tiny Syslog Server in Python3. | |
## | |
## 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.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment