supervisord usage in Centos7.x (via: systemd)

  1. yum install python-setuptools python-pip
  2. pip install supervisor
  3. echo_supervisord_conf > /etc/supervisord.conf
  4. create init script in : /usr/lib/systemd/system/supervisord.service
    “`
    [Unit]
    Description=supervisord – Supervisor process control system for UNIX
    Documentation=http://supervisord.org
    After=network.target
    [Service]
    Type=forking
    ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
    ExecReload=/usr/bin/supervisorctl reload
    ExecStop=/usr/bin/supervisorctl shutdown
    User=root</li>
    </ol>

    [Install]
    WantedBy=multi-user.target

    <pre><code>“`

    Share

在shell 脚本里嵌入python

#!/bin/bash
function pytest() {
python - $@ <<EOT
import sys,datetime
print 'Current Time is %s' % datetime.datetime.now()
print sys.argv
EOT
}


RT=$(pytest $@)
echo my python script says: $RT

Share

snmp via python [IF-MIB]

依赖 pysnmp pysnmp-mibs

# -*- coding: utf-8 -*-
'''
Created on 14/11/20.
@author: seven
'''

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
    cmdgen.CommunityData('community_str'),
    cmdgen.UdpTransportTarget(('211.xx.169.xx', 161)),
    cmdgen.MibVariable('IF-MIB', 'ifNumber'),
    cmdgen.MibVariable('IF-MIB', 'ifDescr'),
    cmdgen.MibVariable('IF-MIB', 'ifType'),
    cmdgen.MibVariable('IF-MIB', 'ifMtu'),
    cmdgen.MibVariable('IF-MIB', 'ifSpeed'),
    cmdgen.MibVariable('IF-MIB', 'ifPhysAddress'),

    lookupValues=True
)

if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBindTable[-1][int(errorIndex) - 1] or '?'
        )
        )
    else:
        for varBindTableRow in varBindTable:
            for name, val in varBindTableRow:
                print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
Share

Python timelib

可以通过pip安装这个库

 

我是为了要使用和php中的 strtotime函数才用到的,很实用

获取上周的时间戳


from timelib import strtotime
from datetime import date
if __name__=='__main__':
 print date.fromtimestamp(strtotime('-2 week Monday'))

Share

用Make 编译Python项目

python编译后性能并不会提升,但是1来可以减少文件量,2来可以对明文的代码封装,还是挺好的。

makefile

#编译后文件的去处

BINDIR := ../bin

#源代码所在目录
BASEDIR := ../src

#编译参数 可选
DBGFLAGS ?= -O

ifeq ($(findstring -O,$(DBGFLAGS)),-O)
COM := .pyo
else
COM := .pyc
endif

define dirflow
FILES := $(wildcard $(1)*)
DIRS :=
$$(foreach e, $$(FILES), $$(if $$(wildcard $$(e)/*), $$(eval DIRS := $$(DIRS) $$(e))))
FILES := $$(filter-out $$(DIRS),$$(FILES))
ALLFILES := $$(ALLFILES) $$(FILES)
$$(foreach e,$$(DIRS),$$(eval $$(call dirflow,$$(e)/)))
endef

$(eval $(call dirflow ,$(BASEDIR)))
sources := $(filter %.py,$(ALLFILES))
clean_com := $(patsubst %.py, _clean_%.py, $(sources))

MKDIR := mkdir -p
TEST  := test -d
RM    := rm -rf

.PHONY : all

all:$(sources)

%.py:bin_dir
@echo Go into $(dir $@)
$(shell $(TEST) $(subst $(BASEDIR),$(BINDIR),$(dir $@)) || $(MKDIR) $(subst $(BASEDIR),$(BINDIR),$(dir $@)))
python $(DBGFLAGS) -m py_compile $@
mv $(patsubst %.py, %$(COM), $@) $(subst $(BASEDIR),$(BINDIR),$(dir $@))
@echo Compile $@ done.

bin_dir:
$(shell $(TEST) $(BINDIR) || $(MKDIR) $(BINDIR))

.PHONY: clean
clean: $(clean_com)
$(RM) $(BINDIR)

$(clean_com):bin_dir
$(RM)$(patsubst _clean_%.py, %$(COM), $@)

Share