Skip to content

Instantly share code, notes, and snippets.

@pbertera
Created June 27, 2017 12:38
Show Gist options
  • Save pbertera/0eddab7f62ee5e69c68b633789115258 to your computer and use it in GitHub Desktop.
Save pbertera/0eddab7f62ee5e69c68b633789115258 to your computer and use it in GitHub Desktop.
Minimal syslog server in Python (working with 2.7 and 3.x)
#!/usr/bin/env python
try:
import socketserver
except ImportError: # Python 2:
import SocketServer as socketserver
import socket
import sys
class LoggedUDPServer(socketserver.ThreadingMixIn, socketserver.UDPServer):
def __init__(self, server_address, RequestHandlerClass):
socketserver.UDPServer.__init__(self, server_address, RequestHandlerClass)
class UDPHandler(socketserver.BaseRequestHandler):
def handle(self):
print(self.request[0])
if __name__ == "__main__":
if len(sys.argv) == 3:
port = int(sys.argv[2])
address = sys.argv[1]
server = LoggedUDPServer((address, port), UDPHandler)
print("Syslog server listening on UDP %s:%d\n\n" % (address, port))
server.serve_forever()
else:
print("ERROR: you must specify address and port:")
print("%s 192.168.10.33 514" % sys.argv[0])
sys.exit(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment