python Syslog C10k服务器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Syslog C10k服务器相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env bash
# run in terminal (in diff sessions)
# python ./server.py 514 &
# python ./server.py 515 &
# python ./server.py 516 &
python ./client.py 514 &
python ./client.py 514 &
python ./client.py 515 &
python ./client.py 515 &
python ./client.py 516 &
python ./client.py 516 &
# python ./client.py 515
# python ./client.py 515
# python ./client.py 515
# python ./client.py 515
# python ./client.py 515
import socket
import time
class Facility:
"Syslog facilities"
KERN, USER, MAIL, DAEMON, AUTH, SYSLOG, \
LPR, NEWS, UUCP, CRON, AUTHPRIV, FTP = range(12)
LOCAL0, LOCAL1, LOCAL2, LOCAL3, \
LOCAL4, LOCAL5, LOCAL6, LOCAL7 = range(16, 24)
class Level:
"Syslog levels"
EMERG, ALERT, CRIT, ERR, \
WARNING, NOTICE, INFO, DEBUG = range(8)
class Syslog:
"""A syslog client that logs to a remote server.
Example:
>>> log = Syslog(host="foobar.example")
>>> log.send("hello", Level.WARNING)
"""
def __init__(self,
host="localhost",
port=514,
facility=Facility.DAEMON):
self.host = host
self.port = port
self.facility = facility
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send(self, message, level):
"Send a syslog message to remote host using UDP."
data = "<%d>%s" % (level + self.facility*8, message)
self.socket.sendto(bytes(data, "ascii"), (self.host, self.port))
def warn(self, message):
"Send a syslog warning message."
self.send(message, Level.WARNING)
def notice(self, message):
"Send a syslog notice message."
self.send(message, Level.NOTICE)
def error(self, message):
"Send a syslog error message."
self.send(message, Level.ERR)
# ... add your own stuff here
# If you put it in a file syslog_client.py you can use it as a module.# If you put it in a file syslog_client.py you can use it as a module.
# >>> import syslog_client
# >>> log = syslog_client.Syslog("remote-host-name")
# >>> log.send("howdy", syslog_client.WARNING)
import sys
port = int(sys.argv[1])
log = Syslog(port=port)
msg = "If you put it in a file syslog_client.py you can use it as a module.# If you put it in a file"# syslog_client.py you can use it as a module."
# msg = "x"
# for id in range(1, 2):
from time import sleep
for id in range(1, 15001):
if id % 100 == 0:
sleep(0.05)
log.send(msg, Level.WARNING)
LOG_FILE = ' e.log'
HOST, PORT = "0.0.0.0", 514
#
# NO USER SERVICEABLE PARTS BELOW HERE...
#
import logging, logging.handlers
import queue
import socketserver
import time
log_que = queue.Queue(-1)
queue_handler = logging.handlers.QueueHandler(log_que)
log_handler = logging.StreamHandler()
queue_listener = logging.handlers.QueueListener(log_que, log_handler)
queue_listener.start()
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(message)s", datefmt='', handlers=[queue_handler])
print(time.time())
print(time.clock())
print(time.perf_counter())
print(time.process_time())
# time.sleep(10)
print(time.time())
print(time.clock())
print(time.perf_counter())
print(time.process_time())
g_msg_per_sec = 10
g_t = 10
g_msg_total = 0
class SyslogUDPHandler(socketserver.BaseRequestHandler):
def __init__(self, p1, p2, p3):
# print('ok', 'self = ', self, 'p1 = ', p1, 'p2 = ', p2, 'p3 = ', p3)
super().__init__(p1, p2, p3)
def handle(self):
global g_t
global g_msg_per_sec
global g_msg_total
# print(g_t, g_msg_per_sec)
data = bytes.decode(self.request[0].strip())
socket = self.request[1]
# print( "%s : " % self.client_address[0], str(data))
# print(f"time = {time.time()}, clock = {time.clock()}, counter = {time.perf_counter()}, process_time = {time.process_time()}")
g_msg_total += 1
tclock = time.clock()
if tclock > g_t:
g_t = tclock + 1
print("messages per sec = ", g_msg_per_sec)
g_msg_per_sec = 0
else:
g_msg_per_sec += 1
# logging.info(str(data))
if __name__ == "__main__":
try:
import threading
def input_handler():
global g_msg_total
while(True):
cmd = input("> ")
if cmd == 'exit':
break
elif cmd == 'c':
g_msg_total = 0
else:
print('total = ', g_msg_total)
t = threading.Thread(target=input_handler)
t.start()
import sys
PORT = int(sys.argv[1])
print(PORT, type(PORT))
server = socketserver.UDPServer((HOST,PORT), SyslogUDPHandler)
g_t = time.clock() + 1
g_msg_per_sec = 0
server.serve_forever(poll_interval=0.5)
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
print ("Crtl+C Pressed. Shutting down.")
#!/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 = 'yourlogfile.log'
HOST = '0.0.0.0'
TCP_PORT = 514
UDP_PORT = 1514
#
# NO USER SERVICEABLE PARTS BELOW HERE...
#
import logging
import time
import threading
import socketserver
listening = False
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())
data = self.request[0].strip()
# socket = self.request[1]
print( "%s : " % self.client_address[0], str(data))
logging.info(str(data))
class SyslogTCPHandler(socketserver.BaseRequestHandler):
End = '\n'
def join_data(self, total_data):
final_data = ''.join(total_data)
for data in final_data.split(self.End):
print( "%s : " % self.client_address[0], str(data))
logging.info(str(data))
def handle(self):
total_data = []
while listening:
data = bytes.decode(self.request.recv(8192).strip())
if not data: break
if self.End in data:
split_index = data.rfind(self.End)
total_data.append(data[:split_index])
self.join_data(total_data)
del total_data[:]
total_data.append(data[split_index + 1:])
else:
total_data.append(data)
if len(total_data) > 0:
self.join_data(total_data)
# logging.info(str(data))
if __name__ == "__main__":
listening = True
try:
# UDP server
udpServer = socketserver.UDPServer((HOST, UDP_PORT), SyslogUDPHandler)
udpThread = threading.Thread(target=udpServer.serve_forever)
udpThread.daemon = True
udpThread.start()
# udpServer.serve_forever(poll_interval=0.5)
# TCP server
tcpServer = socketserver.TCPServer((HOST, TCP_PORT), SyslogTCPHandler)
tcpThread = threading.Thread(target=tcpServer.serve_forever)
tcpThread.daemon = True
tcpThread.start()
while True:
time.sleep(1)
# tcpServer.serve_forever(poll_interval=0.5)
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
listening = False
udpServer.shutdown()
udpServer.server_close()
tcpServer.shutdown()
tcpServer.server_close()
print ("Crtl+C Pressed. Shutting down.")
以上是关于python Syslog C10k服务器的主要内容,如果未能解决你的问题,请参考以下文章