前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python中syslog功能代码

Python中syslog功能代码

作者头像
py3study
发布2020-01-10 16:47:23
8980
发布2020-01-10 16:47:23
举报
文章被收录于专栏:python3python3

参考网址:http://csl.name/py-syslog-win32/

在我们日常工作中,我们需要从Windows系统上发送syslog消息到syslog服务器。下面的代码能够工作的很好。

你能下载syslog.py代码:http://csl.name/py-syslog-win32/syslog.py

代码语言:javascript
复制
#!/usr/bin/python
# -*- encoding: iso-8859-1 -*-
"""
Python syslog client.
This code is placed in the public domain bythe author.
Written by Christian Stigen Larsen.
This is especially neat for Windows users,who (I think) don't
get any syslog module in the default pythoninstallation.
See RFC3164 for more info --http://tools.ietf.org/html/rfc3164
Note that if you intend to send messages toremote servers, their
syslogd must be started with -r to allow toreceive UDP from
the network.
"""
import socket
# I'm a python novice, so I don't know ofbetter ways to define enums
FACILITY = {
       'kern':0, 'user': 1, 'mail': 2, 'daemon': 3,
       'auth':4, 'syslog': 5, 'lpr': 6, 'news': 7,
       'uucp':8, 'cron': 9, 'authpriv': 10, 'ftp': 11,
       'local0':16, 'local1': 17, 'local2': 18, 'local3': 19,
       'local4':20, 'local5': 21, 'local6': 22, 'local7': 23,
}
LEVEL = {
       'emerg':0, 'alert':1, 'crit': 2, 'err': 3,
       'warning':4, 'notice': 5, 'info': 6, 'debug': 7
}
def syslog(message, level=LEVEL['notice'],facility=FACILITY['daemon'],
       host='localhost',port=514):
       """
       Sendsyslog UDP packet to given host and port.
       """
       sock= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
       data= '<%d>%s' % (level + facility*8, message)
       sock.sendto(data,(host, port))
       sock.close()

示例用法:

from syslog import syslog

syslog('There were zwei peanuts walking downder strasse...', host='brit.ain')

为了让它工作,你必须确保syslog服务接收远程消息。你能通过使用-r选项启动syslogd。

我也喜欢Pyton docstring有多么的好,例如:

$ python

Python 2.5.1 (r251:54863, Apr 18 2007,08:51:08) [MSC v.1310 32 bit (Intel)] on win32

Type "help", "copyright","credits" or "license" for more information.

>>> import syslog

>>> help(syslog)

Help on module syslog:

NAME

   syslog - Python syslog client.

FILE

   c:\documents and settings\csl\jall\syslog.py

DESCRIPTION

   This code is placed in the public domain by the author.

   Written by Christian Stigen Larsen.

   This is especially neat for Windows users, who (I think) don't

    getany syslog module in the default python installation.

    SeeRFC3164 for more info -- http://tools.ietf.org/html/rfc3164

   Note that if you intend to send messages to remote servers, their

   syslogd must be started with -r to allow to receive UDP from

    thenetwork.

FUNCTIONS

   syslog(message, level=5, facility=3, host='localhost', port=514)

       Send syslog UDP packet to given host and port.

DATA

   FACILITY = {'auth': 4, 'authpriv': 10, 'cron': 9, 'daemon': 3, 'ftp':...

   LEVEL = {'alert': 1, 'crit': 2, 'debug': 7, 'emerg': 0, 'err': 3,'inf...

>>> 

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档