- 论坛徽章:
- 0
|
本帖最后由 car292516198 于 2011-07-07 18:48 编辑
来得较迟,有了前面高手猛人的代码,再突破,寻找新方法就很难了。幸好第一题,发现可用有序字典,否则没信心做下去。有序字典太旧的版本可能不支持,它与一般字典基本一样,差别是顺序存储。
1.
- import collections
- D=collections.OrderedDict()
- L=[11,22,33,11,22]
- for i in L:
- D[i]=None
- print D.keys()
复制代码
2.
print '{:,}'.format(1234567)
3.
print 'abcde'[::-1]
4.
import os
print os.popen('df -h').read()
5.
这题用路由器测试最方便,本人就是用自己路由器测试通过,但注意域realm要一字不差。- import urllib2
- ip='192.168.1.1'
- realm='Wireless Soho Router'
- auth_handler = urllib2.HTTPBasicAuthHandler()
- auth_handler.add_password(realm=realm,
- uri=ip,
- user='admin',
- passwd='admin')
- opener = urllib2.build_opener(auth_handler)
- urllib2.install_opener(opener)
- try:
- result=urllib2.urlopen('http://'+ip)
- print result.code
- except Exception,e:
- print e,'---Please check realm and ip.'
复制代码 6.
host是远程机地址。
import os
os.system("ssh host date")
7.
“d[line]=d.get(line,0)+1”字典中的get方法,如果line已在字典中,get返回相关数字,加1并设为新值;如不在字典中,get返回默认值0,并加1,相当于:
if line not in d:
d[line]=0
d[line]+=1
我日志文件的URL的索引是6,不同的请修改。- import re
- input=open('C:\\Program Files\\wamp\\logs\\access.log','r')
- d={}
- for line in input:
- line=line.split()[6]
- d[line]=d.get(line,0)+1
- D=sorted(d.items(),key=lambda d:d[1],reverse=True)[:10]
- print D
复制代码 8.- import thread,urllib2
- def do(url):
- try:
- u=urllib2.urlopen(url)
- print url,u.code
- except Exception,e:
- print url,e
-
- for i in [163,263,136,137,138,139,11,22,33,44]:
- url = 'http://www.%s.com' % i
- thread.start_new_thread(do,(url,))
复制代码 |
|