试题:
1. 去除list中的重复数据 [11, 22, 33, 11, 22] => [11, 22, 33]
2. 格式化输出,三位一逗号 1234567 -> 1,234,567
3. 字符反转 abcde -> edcba
4. 打印当前系统的挂载点和空间大小(Linux)
5. 模拟登录401验证网站(Basic HTTP Authentication)
6. 获取远程某机器的系统当前日期时间(Linux)
7. 打印Apache日志中访问次数最多的10条URL。
8. 并发抓取10个URL,打印出HTTP状态码。
公布试题答案:
print '1.'
a = [11, 22, 33, 11, 22]
b = list(set(a))
b.sort()
print '%s => %s' % (a, b)
print
print '2.'
import re
a = '1234567'
b = re.sub(r"(?<=\d)(?=(\d{3})+\b)",",",a)
print '%s => %s' % (a, b)
print
print '3.'
a = 'abcde'
b = a[::-1]
print '%s => %s' % (a, b)
print
print '4.'
import commands
a = commands.getoutput('df -h')
for b in a.split('\n')[1:]:
c = b.split()
print '%-10s => %s' % (c[-1], c[1])
print
print '5.'
import urllib2
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, "http://xxxxx", "smallfish", "***" )
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
try:
f = opener.open("http://xxxxx/dddd" )
f.close()
print 'login ok'
except urllib2.HTTPError:
print 'login error'
print
print '6.'
import commands
a = commands.getoutput("ssh smallfish@haaku.net 'date'" )
print 'now:', a
print
print '7.'
f = open("/var/log/apache2/access.log" )
a = {}
for line in f:
url = re.findall(r'".*?"', line)[0].split()[1]
a[url] = a.get(url, 0) + 1
b = sorted(a.items(), key=lambda x: x[1], reverse=True)
b = b[:10]
print b
print
print '8.'
urls = ['http://www.qq.com']
urls.append('http://www.163.com')
urls.append('http://www.sina.com.cn')
urls.append('http://www.sohu.com')
import gevent
from gevent import monkey
monkey.patch_socket()
import urllib2
import time
def get_http_status(url):
f = urllib2.urlopen(url)
c = f.getcode()
return url, c
jobs = [gevent.spawn(get_http_status, url) for url in urls]
gevent.joinall(jobs, timeout=3)
for job in jobs:
print job.value
ChinaUnix技术实践之五—Python编程大赛试题和答案
转载GNU_CTO 博主文章分类:Python运维开发
上一篇:Debian命令行的配制工具
下一篇:iptables中文man文档

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python数据库编程全指南SQLite和MySQL实践
本文介绍了使用Python进行数据库连接与操作的多种方法和技术。
SQL mysql Python SQLite