存档 Python

[Python]在局域网内获得接入internet的公网ip

是我回答chinaunix的网友提问用的; 

>>> import re,urllib2
>>> re.search(’\d+\.\d+\.\d+\.\d+’,urllib2.urlopen(”http://www.hereismyip.com”).read()).group()
‘58.49.245.149′

哈哈,python真是简洁啊!

 原理就是访问http://www.hereismyip.com or http://www.whereismyip.com 然后用正则表达式查询得到ip;

 

No comment »

试验Pyrex:增加IPC2扩展模块

平时工作相关开发,经常用c++写程序,进程见通讯主要用消息队列和共享内存;

python没有直接操作消息队列共享内存的模块,就想上网找一个;

google了几下,在韩国的python用户组网站找到一个ipc2.pyx,除了pyx代码外,

其他什么都看不懂(都是韩文);

http://www.python.or.kr/pykug/ipc2_2epyx

google一下pyx相关资料,需要Pyrex来处理为python模块;

先用easy_install Pyrex安装了Pyrex;

然后执行:Pyrexc ipc2.pyx 有两个错误警告:long * char * 类型转换的问题;

修改了一下消息队列的读写方法;处理通过;

下面就开始了我的编译之旅!

我的机器Windows 2003装了Python2.4的Windows版本,又装了cygwin,cygwin自带了python2.4;

先在Windows下编译,用

gcc -c -fPIC ipc2.c  -I/usr/include/python2.4 -L/usr/lib/python2.4

提示一堆错误;转念一想,windows又不支持ipc,编译了无用!!尴尬!

切换到cygwin下编译

gcc -c -fPIC ipc2.c  -I/usr/include/python2.4 -L/usr/lib/python2.4

转换为共享库:
 gcc –shared ipc2.o -o ipc2.so -L/lib/python2.4/config -lpython2.4
得到ipc2.so

执行python测试;import ipc2,提示没有这个模块;

执行看来一下site-packages目录下的其他的python模块,发现是dll扩展名;

mv ipc2.so ipc2.dll 再次测试 import ipc2 成功了;

在RedHat linux 9下编译:

python版本是2.2;刚刚达到了Pyrex的要求;又搜索了一下pyrex相关资料,学会了setup.py

写了setup.py如下:

from distutils.core import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext

setup(name=’ipc2′, ext_modules=[Extension(”ipc2″, [”ipc2.pyx”])],
 cmdclass = {’build_ext’: build_ext}
)

用easy_install 安装了pyrex后;

执行python setup.py install

提示编译错误,PyMODINIT_FUNC 没有定义;

在我windows机器上的python24的include目录中的pyport.h中定义;

加入到linux机器的pyport.h中。

再次执行python setup.py install,编译通过,成功安装;

from ipc2 import *

m = mq(0×1001,0666)

m.read()

OK!!!可以读写;

其他的暂时还没有测试;用的时候再测试吧;

 

 

 

Comments (1) »

python小脚本:循环执行命令的脚本

python小工具:循环执行命令的脚本 

主要用在测试时,循环调用测试工具;

如:需要执行 ls -l 100次,每秒一次;

执行命令:

docmd 1 100 ls -l

脚本源码如下:

#!/bin/env python
import time,os,sys
import glob,sys
import string

def main (interval,cnt,cmd):
    
print “begin do…”
    
for in range(cnt) :
        
print “——>%s”%cmd
        
print “——>ret:%d” % ( os.system(cmd) )
        
if interval > 0:
            time.sleep(interval)
    
return cnt

if __name__ == ‘__main__’:
    
print “docmd V1.0 ”
    
if len(sys.argv) 4:
        
print “\ndocmd interval count command ”
        
sys.exit(0)
    interval = int(sys.argv[
1])
    cnt = int(sys.argv[
2])
    cmd = string.join(sys.argv[
3:],“ ”)
    ret = main(interval,cnt,cmd)
    
print “done.”  

No comment »

links for 2006-08-10

没有留言

转化iRiver的歌词文件SNC到标准歌词文件lrc的python脚本

老婆学英语,下载了一些英语资料mp3,可以支持iRiver的mp3播放器显示歌词,

歌词是和mp3打包在一个mp3文件中.

到网上找了一个分离软件,分离出来的歌词格式是snc.我家的mp3不支持.

在网络找了半天也没有找到转化为lrc格式的工具;对比了一下两个格式,发现差别就是在时间码上;

就自己动手用python写了一个脚本负责转化!

import os,glob,sys
import os.path
from string import strip
def snc2lrc(fi):
    newname = os.path.join(os.path.dirname(fi),os.path.basename(fi).split(”.”)[0]+”.lrc”)
    print “—————————————————————————–”
    print “snc2lrc:”,fi
    fo = open(newname,”wt”)
    f = open(fi)
    lines = f.readlines()
    newline = “”
    for x in range(len(lines)):
        line = lines[x]
        print x,line
        if line.startswith(”⑩”):
            if x>0:
                print newline
                fo.write(newline+”\n”);
                newline = “”
            newline = “[”+line[4:6]+”:”+line[6:8]+”.”+str(int(line[8:9]))+”]”
            continue
        else:
            newline +=strip(line[:-1])
    fo.write(newline+”\n”);
    fo.close()
    f.close()
    return

edir = r”Z:\李阳疯狂英语突破精华版\??.snc”
files = glob.glob(edir)
for f in files:
    snc2lrc(f)

 

No comment »

备份cvs服务器的python脚本

公司的所有后台代码都保存在cvs服务器上,以前备份都是手工在做,不能做到及时备份.

最近天气冷了,空调等用电大户开始启动,办公室已经跳闸好几次了.一台VSS服务器的cpu就光荣牺牲了.

幸亏不是烧的硬盘,万幸之余又些后怕.要是cvs服务器坏了,那就不得了.赶快写个自动备份的脚步.

本来是准备用cvsup做的,安装配置还比较麻烦,干脆就用最简单的方式了.先压缩,后ftp进行备份.

 脚步如下:

[code]

#!/bin/env Python
import os,time,ftplib,string
import glob,sys
import os.path

home=’/root/backcvs’                    #本地保存文件目录
ftphost=’192.168.0.99′                  #ftp备份服务器
ftpuser=’cvsroot’   #ftp登陆用户名
ftppass=’xxxxxxx’   #ftp登陆密码
ftppassmode=1    #ftp模式 port 0 passive 1
ftpdir=’backcvs’   #ftp上存放备份的目录

cvsdir=’/home/cvsroot’   #cvs代码存放目录
data=time.strftime(’%y-%m-%d’)
cvstarfile=”cvsroot”+data+”.tgz”
global fd

if not os.path.isdir(home):
    os.mkdir(home)
os.chdir(home)
lastfilelen = os.path.getsize(glob.glob(”*.tgz”)[-1])
cmd = “tar zcvf “+cvstarfile+” “+cvsdir
if (os.system(cmd))!=0:
    print “tar file faild! cmd:[%s]”%(cmd)
    sys.exit(1)

newfilelen = os.path.getsize(cvstarfile)
print “new:%d,old:%d”%(newfilelen,lastfilelen)
if (newfilelen == lastfilelen):
    print “file size not change! exit!”
    sys.exit(0)

print “while upload file:%s,size:%d”%(cvstarfile,newfilelen)
if os.getcwd()==home:
    ftp=ftplib.FTP(ftphost,ftpuser,ftppass)
    ftp.set_pasv(ftppassmode)
    ftp.cwd(ftpdir)
    paths=ftp.nlst(’cvsroot*’)
    print paths
    if len(paths) > 5:
        ftp.delete(paths[0])
        os.remove(paths[0])
    fd = open(cvstarfile, ‘rb’)
    ftp.storbinary(’STOR %s’ % os.path.basename(cvstarfile), fd)
    fd.close()
    ftp.quit()

[/code]

然后在crontab里面加一行就每天定时启动的命令就可以了!

0 1 * * * Python /root/backcvs/backcvs.py

 

 

No comment »

用Python写个进程监控程序

有个应用程序不是很稳定,又暂时找到具体问题,只知道出现错误时日志文件会反映出来.
该应用定时更新日志目录,有两个日志文件:fromclient.log 记录接收请求,fromserver.log记录接收服务端返回.

出现问题时一般是fromclient.log日志在更新,但是fromserver.log就停止了.
灵机一动,何不用python写个监控日志的程序,发现程序异常就自动重启,这样不至于严重影响客户使用.
多出时间来彻底解决问题.

于是就有了下面的代码:
#!/bin/env Python
# -*- coding: cp936 -*-
import glob,os,time,stat,sys

deadflag = 60   #判断进程死掉的秒数

def GetFileTime(filename):
    return os.stat(filename)[stat.ST_CTIME]

def main():

    path = glob.glob(”../log/PROXY*”)[-1]

    clifile = path + “/” + “mt.log”
    svrfile = path + “/” + “fromserver.log”

    clitime = GetFileTime(clifile)
    svrtime = GetFileTime(svrfile)

    print clifile,time.localtime(clitime)
    print svrfile,time.localtime(svrtime)
    if abs(svrtime-clitime) > deadflag:
        print “time is over! will restart!”
        os.system(”sh /home/esm/bin/restart_tcpproxy.sh”)
   
    return

if __name__ == “__main__”:
    main()

   

No comment »

用swig给Python增加Linux下的IPC模块

我负责的一套系统,运行与linux中,模块之间使用linux消息队列通讯的。现在有个地方需要单独一个通讯模块的功能。要我写个通讯接口。本来想用c++来写,后来考虑一下觉得用python跟快捷,跟便于维护。
就上网找python的linux下消息队列相关模块。找到Walter de Jong写的模块:
http://www.xs4all.nl/~walterj/python-ipc/

实际测试一下,发现使用起来不是很方便。就觉得之间改改它。
通过这个模块我才知道swig是如何使用的,就到swig的官方网站去下载了一个最新版安装。

用swig给Python增加Linux下的IPC模块

1、下载安装最新版本的SWIG-1.3.22
    tar zxvf swig-1.3.22.tar.gz
    cd SWIG-1.3.22/
    ./configure
    make
    make install
    此时swig被安装到了/usr/local/bin目录下。需要把老版本的swig给改名了。
    mv /usr/bin/swig /usr/bin/swig1.1
   
2、下面是修改后的ipc包的文件,主要只有3个文件ipc.h,ipc.i,Makefile
   ipc.h:
/*
 ipc.h WJ103

 This file is a rip-off of the standard includes:
 /usr/include/sys/types.h
 /usr/include/sys/ipc.h
 /usr/include/sys/shm.h
 /usr/include/bits/shm.h
 etc.

 If things do not work, check for differences between those files
 and this one. I’m sorry, I could not get it done in any other way…
*/

#include

#define IPC_CREAT   01000   /* Create key if key does not exist. */
#define IPC_EXCL    02000   /* Fail if key exists.  */
#define IPC_NOWAIT  04000   /* Return error on wait.  */

/* Control commands for `msgctl’, `semctl’, and `shmctl’.  */
#define IPC_RMID    0    /* Remove identifier.  */
#define IPC_SET     1    /* Set `ipc_perm’ options.  */
#define IPC_STAT    2    /* Get `ipc_perm’ options.  */
#define IPC_INFO    3    /* See ipcs.  */

/* Special key values.  */
#define IPC_PRIVATE ((key_t)0)  /* Private key.  */

/* Permission flag for shmget.  */
#define SHM_R  0400  /* or S_IRUGO from
*/
#define SHM_W  0200  /* or S_IWUGO from
*/

/* Flags for `shmat’.  */
#define SHM_RDONLY 010000  /* attach read-only else read-write */
#define SHM_RND  020000  /* round attach address to SHMLBA */
#define SHM_REMAP 040000  /* take-over region on attach */

/* Commands for `shmctl’.  */
#define SHM_LOCK 11  /* lock segment (root only) */
#define SHM_UNLOCK 12  /* unlock segment (root only) */

/* ipcs ctl commands */
#define SHM_STAT  13
#define SHM_INFO  14

/* shm_mode upper byte flags */
#define SHM_DEST 01000 /* segment will be destroyed on last detach */
#define SHM_LOCKED 02000   /* segment will not be swapped */

/* Permission struct */
struct ipc_perm {
 key_t __key;     /* Key.  */
 unsigned short int uid;   /* Owner’s user ID.  */
 unsigned short int gid;   /* Owner’s group ID.  */
 unsigned short int cuid;  /* Creator’s user ID.  */
 unsigned short int cgid;  /* Creator’s group ID.  */
 unsigned short int mode;  /* Read/write permission.  */
 unsigned short int __seq;  /* Sequence number.  */
};

/* Data structure describing a set of semaphores.  */
struct shmid_ds
  {
    struct ipc_perm shm_perm;  /* operation permission struct */
    int shm_segsz;   /* size of segment in bytes */
 time_t shm_atime;   /* time of last shmat() */
    time_t shm_dtime;   /* time of last shmdt() */
    time_t shm_ctime;   /* time of last change by shmctl() */
    pid_t shm_cpid;    /* pid of creator */
    pid_t shm_lpid;    /* pid of last shmop */
    unsigned short int shm_nattch; /* number of current attaches */
    unsigned short int __shm_npages; /* size of segment (pages) */
    unsigned long int *__shm_pages; /* array of ptrs to frames -> SHMMAX */
    void *__attaches;    /* descriptors for attaches */
  };

struct shminfo {
    int shmmax;
    int shmmin;
    int shmmni;
    int shmseg;
    int shmall;
};

struct shm_info {
    int used_ids;
    unsigned long int shm_tot; /* total allocated shm */
    unsigned long int shm_rss; /* total resident shm */
    unsigned long int shm_swp; /* total swapped shm */
    unsigned long int swap_attempts;
    unsigned long int swap_successes;
};

/* Flags for `semop’.  */
#define SEM_UNDO 0×1000  /* undo the operation on exit */

/* Commands for `semctl’.  */
#define GETPID  11  /* get sempid */
#define GETVAL  12  /* get semval */
#define GETALL  13  /* get all semval’s */
#define GETNCNT  14  /* get semncnt */
#define GETZCNT  15  /* get semzcnt */
#define SETVAL  16  /* set semval */
#define SETALL  17  /* set all semval’s */

/* Data structure describing a set of semaphores.  */
struct semid_ds
{
  struct ipc_perm sem_perm;  /* operation permission struct */
  time_t sem_otime;   /* last semop() time */
  time_t sem_ctime;   /* last time changed by semctl() */
  struct sem *__sembase;  /* ptr to first semaphore in array */
  struct sem_queue *__sem_pending; /* pending operations */
  struct sem_queue *__sem_pending_last; /* last pending operation */
  struct sem_undo *__undo;  /* ondo requests on this array */
  unsigned short int sem_nsems;  /* number of semaphores in set */
};

/*
 According to X/Open you have to define the union semun yourself.
 What the crap is that all about anyway??? It’s probably just because
 of its name. Well, here it is. I define the semun myself. Ha!

  –Walter
*/
union semun {
     int val;    /* value for SETVAL */
     struct semid_ds *buf;  /* buffer for IPC_STAT & IPC_SET */
     unsigned short int *array;  /* array for GETALL & SETALL */
     struct seminfo *__buf;  /* buffer for IPC_INFO */
};

/* ipcs ctl cmds */
#define SEM_STAT 18
#define SEM_INFO 19

struct  seminfo
{
  int semmap;
  int semmni;
  int semmns;
  int semmnu;
  int semmsl;
  int semopm;
  int semume;
  int semusz;
  int semvmx;
  int semaem;
};

/*
 Note: use the helper function mkmsgbuf() to create a message buffer
 This structure is merely used for typecasting a larger object
*/
struct msgbuf
  {
    long int mtype;  /* type of received/sent message */
    char mtext[2048];  /* text of the message */
  };

/* Define options for message queue functions.  */
#define MSG_NOERROR 010000 /* no error if message is too big */
#define MSG_EXCEPT 020000 /* recv any msg except of specified type */

/* Structure of record for one message inside the kernel.
   The type `struct msg’ is opaque.  */
struct msqid_ds
{
  struct ipc_perm msg_perm; /* structure describing operation permission */
  struct msg *__msg_first; /* pointer to first message on queue */
  struct msg *__msg_last; /* pointer to last message on queue */
  time_t msg_stime;  /* time of last msgsnd command */
  time_t msg_rtime;  /* time of last msgrcv command */
  time_t msg_ctime;  /* time of last change */
  void *__wwait; /* ??? */
  void *__rwait; /* ??? */
  unsigned short int __msg_cbytes;/* current number of bytes on queue */
  unsigned short int msg_qnum; /* number of messages currently on queue */
  unsigned short int msg_qbytes;/* max number of bytes allowed on queue */
  pid_t msg_lspid; /* pid of last msgsnd() */
  pid_t msg_lrpid; /* pid of last msgrcv() */
};

/*
#define msg_cbytes __msg_cbytes
*/

/* ipcs ctl commands */
#define MSG_STAT 11
#define MSG_INFO 12

/* buffer for msgctl calls IPC_INFO, MSG_INFO */
struct msginfo {
    int msgpool;
    int msgmap;
    int msgmax;
    int msgmnb;
    int msgmni;
    int msgssz;
    int msgtql;
    unsigned short int msgseg;
};

/* EOB */

swig的接口定义文件:ipc.i
//
// ipc.i WJ103
//

%module ipc
%{
#include “ipc.h”
%}

// Produce constants and helper functions for structures and unions
%include “ipc.h”

int ftok(char *, char);

int shmget(int, int, int);
void *shmat(int, void *, int);
int shmdt(void *);
int shmctl(int, int, struct shmid_ds *);

int semget(int, int, int);
int semctl(int, int, int, union semun);
int semop(int, struct sembuf *, unsigned int);

int msgget(int, int);
int msgctl(int, int, struct msqid_ds *);
int msgsnd(int, struct msgbuf *, int, int);
int msgrcv(int, struct msgbuf *, int, long, int);

%inline
%{
%}

// EOB

编译定义文件Makefile
#
# Makefile
#
# python ipc module by Walter de Jong walter@heiho.net>
#
CC=gcc
COPT=-g #-O2 -Winline
CFLAGS=$(COPT) -Wall
CFLAGSWRAP = $(COPT) -fpic
CFLAGSSO = $(COPT) -shared

SWIG=swig
PYTHON_INCLUDE=/usr/include/python2.2

OBJS=ipc_wrap.o

all: _ipc.so

_ipc.so: $(OBJS)
 $(CC) $(CFLAGSSO) $(OBJS) -o _ipc.so

ipc_wrap.o: ipc_wrap.c
 $(CC) $(CFLAGSWRAP) -c ipc_wrap.c -Dbool=char -I. -I$(PYTHON_INCLUDE)

ipc_wrap.c: ipc.i
 $(SWIG) -python ipc.i

.c.o:
 $(CC) $(CFLAGS) -c $
clean:
 rm -f $(OBJS) _ipc.so ipc.py ipc.pyc ipc_wrap.c ipc_wrap.doc

# EOB
编译:

执行make
程序编译后生成了:
ipc.py,ipc.pyc模块文件,以及模块支持库_ipc.so

3、用法介绍:

由于我只使用消息队列相关函数,所以没有对其它函数进行测试!

>>> import ipc
>>> dir(ipc)
[’GETALL’, ‘GETNCNT’, ‘GETPID’, ‘GETVAL’, ‘GETZCNT’, ‘IPC_CREAT’, ‘IPC_EXCL’, ‘IPC_INFO’, ‘IPC_NOWAIT’, ‘IPC_RMID’, ‘IPC_SET’, ‘IPC_STAT’, ‘MSG_EXCEPT’, ‘MSG_INFO’, ‘MSG_NOERROR’, ‘MSG_STAT’, ‘SEM_INFO’, ‘SEM_STAT’, ‘SEM_UNDO’, ‘SETALL’, ‘SETVAL’, ‘SHM_DEST’, ‘SHM_INFO’, ‘SHM_LOCK’, ‘SHM_LOCKED’, ‘SHM_R’, ‘SHM_RDONLY’, ‘SHM_REMAP’, ‘SHM_RND’, ‘SHM_STAT’, ‘SHM_UNLOCK’, ‘SHM_W’, ‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__name__’, ‘_ipc’, ‘_newclass’, ‘_object’, ‘_swig_getattr’, ‘_swig_setattr’, ‘ftok’, ‘ipc_perm’, ‘ipc_permPtr’, ‘msgbuf’, ‘msgbufPtr’, ‘msgctl’, ‘msgget’, ‘msginfo’, ‘msginfoPtr’, ‘msgrcv’, ‘msgsnd’, ‘msqid_ds’, ‘msqid_dsPtr’, ’semctl’, ’semget’, ’semid_ds’, ’semid_dsPtr’, ’seminfo’, ’seminfoPtr’, ’semop’, ’semun’, ’semunPtr’, ’shm_info’, ’shm_infoPtr’, ’shmat’, ’shmctl’, ’shmdt’, ’shmget’, ’shmid_ds’, ’shmid_dsPtr’, ’shminfo’, ’shminfoPtr’]
建立消息队列:
>>> msgid = ipc.msgget(0×10012,0666|ipc.IPC_CREAT)
>>> msgid
65538
此时用ipcs 查看,可以看到消息队列已经创建成功
发送消息:
>>> mbuf = ipc.msgbuf()
>>> mbuf
>>> mbuf.mtype = 1
>>> mbuf.mtext = ‘abc’
>>> ipc.msgsnd(msgid,mbuf,3,0)                     
0
此时用ipcs 查看,消息队列中有一个消息
接收消息队列
>>> rbuf = ipc.msgbuf()
>>> ipc.msgrcv(msgid,rbuf,2048,1,0)
3
>>> rbuf.mtype
1
>>> rbuf.mtext
‘abc\x00\x00\x00′后面省略…

此时用ipcs 查看,消息队列中有一个消息接收消息队列

No comment »

用Python来在IP地址和数字之间转换

同事要把数据库中整数表示的ip地址转换为标准的数字ip。要找个工具,我说可以用python写个脚本来完成。应该很简单的,结果就有了下面的代码:
>>> import socket
>>> import struct
>>> socket.inet_ntoa(struct.pack(’I',socket.htonl(-591250144)))
‘220.194.61.32′

>>> socket.ntohl(struct.unpack(”I”,socket.inet_aton(’220.194.61.32′))[0])
-591250144

简单吧,python真是个好东东。工作的好帮手啊!

 

No comment »

AIX下安装Python2.2

有个同事要在AIX5下使用Python脚本,折腾了半天,算是安装上了,总结如下:
1、
 AIX Toolbox for Linux Applications :
http://www-1.ibm.com/servers/aix/products/aixos/linux/download.html
下载:

Python-devel 2.2 License RPM Source The libraries and header files needed for Python extension development.
readline-devel 4.3 License RPM Source Development files for programs which will use the readline library.

2、下载如下文件:

ftp://ftp.software.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc/db/db-3.3.11-3.aix4.3.ppc.rpm
ftp://ftp.software.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc/expat/expat-1.95.2-4.aix5.1.ppc.rpm
ftp://ftp.software.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc/gdbm/gdbm-1.8.0-5.aix4.3.ppc.rpm
ftp://ftp.software.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc/readline/readline-4.3-1.aix5.1.ppc.rpm

3,安装:
登录AIX,用root执行如下命令:
   rpm -ivh db-3.3.11-3.aix4.3.ppc.rpm
   rpm -ivh expat-1.95.2-4.aix5.1.ppc.rpm
   rpm -ivh gdbm-1.8.0-5.aix4.3.ppc.rpm
   rpm -ivh readline-4.3-1.aix5.1.ppc.rpm
环境准备好后,用rpm -ivh 安装Python相关rpm即可。
  

No comment »

下一页 »