Chinaunix首页 | 论坛 | 博客
  • 博客访问: 520629
  • 博文数量: 102
  • 博客积分: 950
  • 博客等级: 准尉
  • 技术积分: 1094
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-28 16:07
文章分类

全部博文(102)

文章存档

2020年(2)

2016年(5)

2015年(15)

2014年(25)

2013年(18)

2012年(19)

2011年(18)

我的朋友

分类: Python/Ruby

2012-09-07 16:06:45

 

日期时间

date=(datetime.date.today() datetime.timedelta(days=0)).isoformat()             #今天

date=(datetime.date.today() datetime.timedelta(days=1)).isoformat()             #明天

date=(datetime.date.today() datetime.timedelta(days=-1)).isoformat()            #前天

 

#睡觉

time.sleep(0.001)

 

导入各种模块

from xml.dom import minidom

import re

import client_0_8 as amqp                 #rabbitMQ 高级消息队列

 

 

全局变量

  global game,date

 

脚本参数传递和异常处理

    try:

        opts,args=getopt.getopt(sys.argv[1:],'hg:d:')

        if len(sys.argv)<=1:

            Usage()

            sys.exit(2)

    except getopt.GetoptError, err:

        Usage()

        sys.exit(2)

    for o,a in opts:

        if o=='-h':

            Usage()

            sys.exit()

        elif o=='-g':

            game=a

        else:

            print "unhandled option"

            Usage()

            sys.exit(3)

print game,date

 

 

连接、进入、申明队列、交换申明、绑定队列                                              

conn= amqp.Connection(host="220.18.66.1:35873", userid="wb",password="pnle", virtual_host=game, insist=False)

chan = conn.channel()

    dir(chan)

    chan.queue_declare(queue=game "_csv_box", durable=True,exclusive=False, auto_delete=False)

    chan.exchange_declare(exchange=game "_sorting_room", type="direct", durable=True,auto_delete=False,)

chan.queue_bind(queue=game '_csv_box', exchange=game '_sorting_room',routing_key='jason')

 

将队列_csv_box 和交换机 sorting_room  绑定在一起

 

交换机类型:

  • Fanout: 不处理路由键,将消息广播给绑定到该交换机的所有队列。
  • Direct: 处理路由键,对消息路径进行全文匹配。消息路由键 "dog" 只能匹配 "dog" 绑定,不匹配 "dog.puppy" 这类绑定。
  • Topic: 处理路由键,按模式匹配路由键。模式符号 "#" 表示一个或多个单词,"*" 仅匹配一个单词。如 "audit.#" 可匹配 "audit.irs.corporate",但 "audit.*" 只匹配 "audit.irs"

 

#发送messagerabbitMQ队列

def Trans(message):

        msg = amqp.Message(message "\n")   #

        msg.properties["delivery_mode"] = 2   #发送模式=2表示要持久化日志,重启后可以恢复未处理的日志

        chan.basic_publish(msg,exchange=game "_sorting_room",routing_key="jason")

发布消息到_sorting_room交换机上,jason字符串

 

要持久化消息的步骤如下:

1.     将交换机设成 durable

2.     将队列设成 durable

3.     将消息的 Delivery Mode 设置成2

 

 

文件打开、写入操作

   info='/home/wb/tmp/' game '_realtime' '.info'

    f=open(info,'w')

f.write(game ',' date '\n')

 

 

f = file(conffile)

line = f.readline()

 

for i in os.popen("/sbin/ifconfig " interface).readlines():

match = re.search('\s inet\s addr:(\d \.\d \.\d \.\d ). ', i) #正则匹配re模块

      if match:

                result = re.findall('\s inet\s addr:(\d \.\d \.\d \.\d ). ',i)

                #print result[0]

r = re.findall('zone\[(\d )\]="' result[0] '[^\d] ',line)

 

#回车符去掉

nowline = f.readline()

                nowline.strip('\n')

 

#查看文件大小

exec('st_size_' fileid[filename] '= os.stat(filename)[6]')

#等于执行st_size_2=os.stat(/home/wb/Goup.cf)[6]

 

#seek读取到文件某个位置

exec('file_' fileid[filename] '.seek(int(nowline))')

等于= file_3.seek(int(12398)) #文件的12398行)

 

#告诉

exec('where = file_' fileid[filename] '.tell()')

 

判断文件是否存在

    if not os.path.isfile(xmlfile):

        print xmlfile,"is not in exits"

        sys.exit(2)

 

分析XML

                                                                                                                                                  

from xml.dom import minidom

xmldoc=minidom.parse(xmlfile)

 

   configure=xmldoc.getElementsByTagName('configure')[0]  #获得Tag

    script=configure.getElementsByTagName('script')                                  #

interface=configure.attributes['interface'].value                                               #

 

 

字符串替换

     sinfile=sinfile.replace('#Y-m-d#',date)  #把字符串#Y-m-d# 替换成date变量内容

        if game == 'g' or game == 'sj' or game == 'x' or game == 'xlz':

                sinfile=sinfile.replace('_','-').replace('#Y-m-d#',date).replace('-','_')

留下一个问题?代码不通用

怎么将代码中的业务逻辑判断去掉

在配置文件中加入字段,通过字段的标识来做不同逻辑处理,而不是在代码中这么硬编码

 

parsekey=line.replace(',',':').split(':')[4]

parsekey=parsekey.replace('\s ','')

 

数组的操作

filelist=[]

filelist.append(sinfile)

 

哈希列表的操作

name={}

name[sid sfparse_key]=sfname

 

 

 

 

阅读(833) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~