目录

Python tips

set()

>>>basket = ['apple','orange','apple','pear','apple','banana']
 
>>>fruit=set(basket)
 
>>>fruit
set(['orange', 'pear', 'apple', 'banana'])
 
>>>'orange' in fruit
True
 
>>>a=set('abracadabew')
>>>a
set(['a', 'c', 'b', 'e', 'd', 'r', 'w'])
 
>>>b=set('wajgwaoihwb')
>>>b
set(['a', 'b', 'g', 'i', 'h', 'j', 'o', 'w'])
 
>>>a-b    #差
set(['c', 'r', 'e', 'd'])
 
>>>a|b   #并
set(['a', 'c', 'b', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r', 'w'])
 
>>>a&b   #交
set(['a', 'b', 'w'])
 
>>>a^b   #(并-交)
set(['c', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r'])

进程锁

只启动一个进程
lockfp = file("/home/atlantis/trans2hadoop/mod.lck","w”)
fcntl.flock(lockfp.fileno(),fcntl.LOCK_EX | fcntl.LOCK_NB)

除去列表重复元素

>>> a = [11,22,33,44,11,22]
>>> b = set(a)
>>> b
set([33, 11, 44, 22])
>>> c = [i for i in b]
>>> c
[33, 11, 44, 22]

格式转换

[(1,), (2,), (3,)] 如何转为 [1, 2, 3]?

a = [(1,), (2,), (3,)]
[tuple(i)[0] for i in a]

测试服务器端口

python -m SimpleHTTPServer 8888

python 服务器

#/bin/env python
#coding:utf-8
 
import socket,select,sys,time
import thread
 
s_list = []
 
def loop(cs,addr,s_ip,s_port):
    print '%s %d connected.' % addr
    ts = socket.socket()
 
    try:
        ts.connect((s_ip,s_port))
    except:
        cs.close()
        print '%s %d closed.' % addr
        sys.exit(0)
 
    while True:
 
        rl,wl,xl = select.select([cs.fileno(),ts.fileno()],[],[cs.fileno(),ts.fileno()])
 
        if len(xl) > 0:
            cs.close()
            ts.close()
            print '%s %d closed.' % addr
            sys.exit(0)
 
        if len(rl) > 0:
            if rl[0] == cs.fileno():
                rs = ts
                ws = cs
            else:
                rs = cs
                ws = ts
 
            try:
                buffer = ws.recv(10000)
                if len(buffer) == 0:
                    raise
                rs.send(buffer)
            except:
                rs.close()
                ts.close()
                print '%s %d closed.' % addr
                sys.exit(0)
 
def mainserver(l_port,s_ip,s_port):
    global s_list
    try:
        ss = socket.socket()
        ss.bind(('0.0.0.0',l_port))
        ss.listen(10)
        s_list.append((l_port,s_ip,s_port))
    except:
        sys.exit(0)
 
    while True:
        cs,addr = ss.accept()
 
        thread.start_new_thread(loop,(cs,addr,s_ip,s_port))
 
def manager(l_port):
    global start,s_list
 
    ss = socket.socket()
    ss.bind(('0.0.0.0',l_port))
    ss.listen(10)
 
    while True:
        cs,addr = ss.accept()
        cs.send("""trans server 1.0\r\ntype 'help' to get help\r\n""")
        buffer = ''
        while True:
            buf = cs.recv(10000)
            if len(buf) == 0:
                cs.close()
                break
            if buf[-1] not in ('\r','\n'):
                buffer += buf
                continue
            buffer += buf
            cmd = buffer.strip()
            buffer = ''
            if cmd == 'exit':
                cs.close()
                break
            elif cmd == 'stop':
                start = 0
                cs.close()
                sys.exit(0)
            elif cmd == 'list':
                b = ''
                for l in s_list:
                    b += '%4d %s:%d\r\n' % l
 
                if len(b) > 0:
                    cs.send(b)
            elif cmd in ('help','?'):
                cs.send("""-------------------------------------------\r
exit\r
    exit telnet\r
start localport serverip:serverport\r
    start a new server\r
list\r
    list all server\r
-------------------------------------------\r
""")
            else:
                cmds = cmd.split(" ",1)
                if len(cmds) > 1 and cmds[0] == 'start':
                    args = cmds[1].strip().split(" ",1)
                    if len(args) != 2:
                        cs.send('start localport serverip:serverport\r\n')
                        continue
                    arg = args[1].split(":",1)
                    if len(arg) != 2:
                        cs.send('start localport serverip:serverport\r\n')
                        continue
 
                    try:
                        l_port = int(args[0])
                        s_ip = arg[0]
                        s_port = int(arg[1])
                    except:
                        cs.send('start localport serverip:serverport\r\n')
                        continue
 
                    thread.start_new_thread(mainserver,(l_port,s_ip,s_port))
                    cs.send('start OK!\r\n')
                else:
                    cs.send('no command [%s]\r\n' % cmd)
                    continue
 
def main():
    global start
 
    if len(sys.argv) == 3:
        try:
            l_port = int(sys.argv[1])
            s_ip,s_port = sys.argv[2].split(":")
            s_port = int(s_port)
            thread.start_new_thread(mainserver,(l_port,s_ip,s_port))
        except:
            pass
 
    start = 1
 
    thread.start_new_thread(manager,(9000,))
 
    while start:
        time.sleep(1)
 
if __name__ == '__main__':
 
    start = 0
 
    main()

How to get a Month Name in Python

>>> import datetime
>>> named_month = lambda month_num:datetime.date(1900,month_num,1).strftime('%B')
>>> print named_month(5)
'May'

Python 取IP

>>>import socket
>>>print socket.gethostbyname(socket.gethostname())    #windows版和mac版相同

邮件群发

#!/usr/bin/env python
# -*- coding: utf8 -*-
#包含收件人列表,一行一个地址,保存为list.txt
#群发邮件内容,保存为mail.eml
 
import smtplib
import time
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import email
 
#邮件发送服务器地址
smtp_server = '127.0.0.1'
 
#发件人地址
from_usr = 'test@abc.com'
 
#邮件标题
title = 'Test Mail'
 
#发一封歇息多久
delay = 0.1
 
#smtp服务器是否需要验证
#需要验证为1不需要为0
auth = 0
 
#如果需要验证,请在下面输入用户名和密码
log_usr=""
log_passwd=""
 
def gingerMail(smtp_server,from_usr,to_usr,title,msg,auth,log_usr,log_passwd):
   server = smtplib.SMTP(smtp_server)
#   server.set_debuglevel(1)
   if auth == 1:
       server.login(log_usr,log_passwd)
   subject= to_usr.split('@')[0]+', Look this: '+title
   msg.replace_header('Subject',email.Header.Header(subject, 'utf-8'))
   msg.replace_header('Date',time.ctime())
   msg.replace_header('From',from_usr)
   msg.replace_header('To',to_usr)
   server.sendmail(from_usr, to_usr, msg.as_string())
   server.quit()
 
f_list='list.txt';
fp=open('mail.eml','r')
msg=email.message_from_file(fp)
fp.close()
 
try:
   f=open(f_list,'r')
   lines=f.readlines()
   #邮件计数变量
   cnt = 0
   for line in lines:
       to_usr = line.strip()
       cnt = cnt + 1
       gingerMail(smtp_server,from_usr,to_usr,title,msg,auth,log_usr,log_passwd)
       print "Email No." + str(cnt) + " has been sent to: " + to_usr + ""
       time.sleep(delay)
   f.close()
   print "......All Finished!!!"
except IOError msg:
   print 'error:', msg[0]

目录统计

################################################################################
##############
#  SpaceFinder.py v.1  07/01/2004
#  Plagerized from many sources by: triggernum5
#  If you see your code in here, then by all means claim the credit
#  Use:  Enter the name of the root directory you wish scanned  in the name field on line #67
#          Press White button to view output.
#  Notice:  May take a while when run on huge directories
#  Future versions will incorporate a browsing interface, and directory depth options.
#  For now please bear in mind that I began stealing this code today
################################################################################
##############
 
import os
 
listG = []
 
def GetTotalFileSize(dummy_param, directory, list_of_files):
'''Given a list of files and the directory they're in, add the
total size and directory name to the global list listG.
'''
global listG
currdir = os.getcwd()
os.chdir(directory)
total_size = 0
if len(list_of_files) != 0:
  for file in list_of_files:
  if file == ".." or file == ".": continue
  try:
    size = os.path.getsize(file)
    total_size = total_size + size
  except:
    continue
listG.append([total_size, directory])
os.chdir(currdir)
 
def GetSize(directory):
'''Returns a list of the form [ [a, b], [c, d], ... ] where
a, c, ... are the number of total bytes in the directory and
b, d, ... are the directory names. The indicated directory
is recursively descended and the results are sorted by directory
size with the largest directory at the beginning of the list.
'''
import os
global listG
listG = []
os.path.walk(directory, GetTotalFileSize, "")
listG.sort()
listG.reverse()
 
def ShowBiggestDirectories(directory):
import regsub
GetSize(directory)
# Get total number of bytes
total_size = 0
for dir in listG:
  total_size = total_size + dir[0]
if total_size != 0:
  print "For directory '%s': " % directory,
  print "[total bytes = %.1f MB]" % (total_size / (1024.0*1024))
  print "Size            -    Directory Name"
  print ""
  print "---------------- " + "-" * 50
  not_shown_count = 0
  for dir in listG:
  dirsize = dir[0] / (1024*1024)
  dir[0] = 100.0 * dir[0] / total_size
  dir[1] = regsub.gsub("\\\\", "/", dir[1])
  print "%6.1fMB %s" % (dirsize, dir[1])
 
if __name__ == '__main__':
import sys
name = 'f:\\games\\'
ShowBiggestDirectories(name)