您的位置:首页 > 编程语言 > Python开发

ChinaUnix技术实践之五—Python编程大赛试题和答案

2012-07-19 21:37 453 查看
试题:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python