- 论坛徽章:
- 0
|
本帖最后由 lkk2003rty 于 2011-07-06 13:38 编辑
1 a = [11, 22, 33, 11, 22]
a = list(set(a))
2- def fun(num, pos=0):
- import locale
- locale.setlocale(locale.LC_ALL, "")
- return locale.format("%.*f", (pos, num), True)
-
复制代码 34
- import os
- os.system('df -h')
复制代码 5- #!/usr/bin/env python
- import urllib2,sys
- mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
- mgr.add_password(None, 'http://192.168.1.12/auth/index.html', "emacle", "galaxy")
- handler = urllib2.HTTPBasicAuthHandler(mgr)
- opener = urllib2.build_opener(handler)
- urllib2.install_opener(opener)
- try:
- resp = urllib2.urlopen('http://192.168.1.12/auth/index.html')
- except (urllib2.URLError, urllib2.HTTPError) as ex :
- print "Fail to get page!"
- if hasattr(ex, 'code'):
- print "Error Code:",ex.code
- if hasattr(ex, 'reason'):
- print "Reason:",ex.reason
- except:
- print "Unexpected error:", sys.exc_info()
- else:
- print resp.msg, "\n", resp.read()
复制代码 6- #!/usr/bin/env python
- import paramiko,sys
- cli = paramiko.SSHClient()
- cli.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
- try:
- cli.connect('192.168.1.22', username='super', password='super123')
- stdin, stdout, stderr = cli.exec_command( 'date' );
- except (paramiko.SSHException, paramiko.AuthenticationException, paramiko.BadHostKeyException) as ex:
- print "Fail to get time!"
- if hasattr(ex, 'code'):
- print "Error Code:",ex.code
- if hasattr(ex, 'reason'):
- print "Reason:",ex.reason
- except:
- print "Unexpected error:", sys.exc_info()
- else:
- print stdout.readlines()
复制代码 7apache 日志配置为- LogFormat "%h %l %u %t \"%r\" %>s %b" common
- CustomLog "logs/access_log" common
复制代码 python脚本为- #!/usr/bin/env python
- import os,sys,re
- if len(sys.argv) < 2:
- print "Usage:./ana.py file"
- sys.exit(1)
- if not os.path.isfile(sys.argv[1]):
- print sys.argv[1], "is not file"
- sys.exit(1)
- f = open(sys.argv[1], 'r')
- d = dict()
- for line in f:
- m = re.search('([^ ]+)\sHTTP', line)
- if m:
- url = m.group(1)
- if url in d:
- d[url] += 1
- else:
- d[url] = 1
- f.close()
- cnt = 0
- for k,v in sorted(d.items(), key = lambda d:d[1],reverse = True):
- if cnt < 10:
- print k,v
- cnt += 1
- else:
- break
复制代码 8- #! /usr/bin/env python
- urls = ["http://www.google.com",
- "http://www.baidu.com",
- "http://www.sina.com.cn",
- "http://www.qq.com",
- "http://bbs.chinaunix.net",
- "http://baike.baidu.com",
- "http://zhidao.baidu.com",
- "http://tieba.baidu.com",
- "http://www.cnbeta.com",
- "http://www.python.org"]
- import eventlet
- from eventlet.green import urllib2
- def fetch(url):
- resp = urllib2.urlopen(url)
- return url,resp
- pool = eventlet.GreenPool(200)
- for u,i in pool.imap(fetch, urls):
- print u,"\t",i.code
复制代码 搞定~~~~~ |
|