码迷,mamicode.com
首页 > 编程语言 > 详细

Python实战作业-day3

时间:2017-08-09 18:09:07      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:函数实现   start   传参   字符   索引   har   pytho   test   日志文件   

 

作业需求:

 

作业1:将nginx日志通过python脚本处理取出TOP10的IP地址和访问次数通过html页面的形式展现出来

 

作业2:通过python函数的方式实现用户注册、登录脚本的实现

 

作业代码1:

 1 #/usr/bin/env python
 2 #coding:utf-8
 3 
 4 import time
 5 
 6 start = time.clock()
 7 
 8 # 定义一个空字典
 9 nginxip = {}
10 
11 # 打开一个文件以readlines方式进行读取所有行
12 with open(access.txt) as f:
13     logs = f.readlines()
14 
15 # 循环日志文件以空格为分隔符截取位置为0及IP这个元素
16 for line in logs:
17     ip = line.split()[0]
18 
19     # 判断ip出现的次数分别以计数的方式写入字典里
20     if ip in nginxip:
21         nginxip[ip] += 1
22     else:
23         nginxip[ip] = 1
24 
25 # 定义一个新列表,使用sorted进行降序排列,使用key排序,截取前十个IP
26 list = sorted(nginxip.iteritems(),key=lambda x:x[1],reverse=True)[:10]
27 
28 #定义一个html文件,根据html的编排格式写到html文件,然后将k/v的值分别循环写入已经编排好的html边框里面
29 f = open(list2.html,a+)
30 f.write("<html><table whith=‘400‘ border=‘1‘ cellpadding=‘1‘‘ cellspacing=‘1‘>")
31 f.write("<title>reboot_homework</title>")
32 f.write("<th style=‘border:solid 1px‘>IP Address</th><th style=‘border:solid 1px‘>Num</th>")
33 # 遍历list列表,enumerate会将该数据对象组合为一个索引序列,同时列出数据和数据下标
34 for k,v in list:
35     f.write(<tr><td style="border:solid 1px">%s</td><td style="border:solid 1px">%s</td></tr> % (k,v))
36     print "访问IP和次数:%s --> %s" %(k,v)
37 
38 f.write("</table></html>")
39 f.close()
40 end = time.clock()
41 print "统计运行时间: %f s" % (end - start)

 

代码运行结果1:

技术分享

 

作业代码2:

 1 #!/usr/bin/env python
 2 #coding:utf-8
 3 
 4 ‘‘‘
 5 通过函数实现用户注册、登陆python脚本
 6 ‘‘‘
 7 
 8 
 9 # 定义一个登陆函数,在这里并不需要参数
10 def login():
11 
12     # 使用input获得用户输入的字符串判断是否存在test文件中
13     username = raw_input("please input your name:").strip()
14     # 定义一个文件并赋予可读的权限
15     login_file = open(/Users/guanqing/PycharmProjects/reboot15/day3/test,r)
16     # for循环这个文件并通过:的格式将用户名和密码传参到usernum和passwordnum
17     for line in login_file.readlines():
18         print line.strip().split(:)
19         usernum,passwordnum = line.strip().split(:)
20         # 判断用户以及密码是否正确,用户名和密码错误的时候有相应的提示
21         if username == usernum:
22             pass01 = raw_input("please input your password:").strip()
23             if pass01 == passwordnum:
24                 return "welcome to login."
25             else:
26                 return "your password is wrong."
27         else:
28             return "your username is wrong."
29     login_file.close()
30 
31 # 定义一个注册函数
32 def register():
33     # 定义一个文件并赋予权限读写权限
34     register_file = open(/Users/guanqing/PycharmProjects/reboot15/day3/test, a+)
35     username = raw_input("please input your name:").strip()
36     # 判断用户输入是否为空
37     if len(username) == 0:
38         return "your username can‘t empty."
39     else:
40         # 判断用户输入的密码是否正确以及是否为空
41         password = raw_input("please input your password:").strip()
42         repassword = raw_input("please input your password again:").strip()
43         if password != repassword or len(password) == 0:
44             return "the password is wrong. "
45         else:
46             # 将用户输入的账号信息写入到test文件里
47             register_file.write("%s:%s\n" %(username,password))
48             register_file.close()
49             return "congratulations, your register is success."
50 
51 # 通过input的方式判断并调用函数
52 def start():
53     action = raw_input("please select:register/login:").strip()
54     if action == "login":
55         res = login()
56     elif action == "register":
57         res = register()
58     else:
59         return "your input is wrong."
60 
61     return res
62 
63 print start()

 

Python实战作业-day3

标签:函数实现   start   传参   字符   索引   har   pytho   test   日志文件   

原文地址:http://www.cnblogs.com/guanqingye/p/7326642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!