看CookBook就像看小说,挑感兴趣的先学习。
所以继《文本》之后,开始《系统管理》。
同样,请善用目录。
发现一个cookbook:好地址
from random import choice import string def GenPasswd(length = 8,chars = string.letters+string.digits): return ''.join([choice(chars) for i in range(length)]) for i in range(6): print GenPasswd(12)结果:
>>> fnkdz5dPA3zu TZF9HzndkrSb fiGAfBU8y2rk QRmtzTzNnxFR RJp8Cv6CsEHV 9kPHiFUJ7uWz >>>
拓展阅读:
Python中的random模块 - Capricorn.python - 博客园(random的常见操作,很清晰)
import random,string class password(object): def __init__(self,filename): self.data = open(filename).read().lower() def renew(self,n = 8,maxmem = 3): chars = [] for i in range(n): rmdIndex = random.randrange(len(self.data)) self.data = self.data[rmdIndex:]+self.data[:rmdIndex] where = -1 locate = chars[-maxmem:] while where < 0 and locate: where = self.data.find(str(locate)) locate = locate[1:] ch = self.data[where+len(locate)+1] if not ch.islower(): ch = random.choice(string.lowercase) chars.append(ch) return ''.join(chars) p = password("c:\\LICENSE.txt") print p.renew(12)
思想:
(1)对于每一个随机字符,我们都按照以下流程处理一遍
(2)随机找到中间点,然后分成两段,翻转合并
(3)用最后的结果的倒数maxmem个字符来做locate标记
(4)每次找到这个locate处,若找不到,locate缩减为locate[1:],继续找,直到找到为止
(5)我们要的本次的随机字符就是找到的locate处的后一个字符self.data[where+len(locate)+1],如果locate为空,那么ch就是(2)后的第一个字符,也是随机的
拓展阅读:
import poplib def checkPOP3User(host,user,password): try: pop = poplib.POP3(host) except: raise RuntimeError("Could not establish connection" "to %r for password check" % host) try: pop.user(user) pop.pass_(password) count,size = pop.stat() assert type(count) == type(size) == int print count,size pop.quit() except: raise RuntimeError("Could not verify identity. \n" "User name %r or password incorrect." % user) pop.quit() checkPOP3User("pop.qq.com","411187674@qq.com","123456")
Ps:看了RuntimeError之后才发现,字符串可以s = "123""abc"这么写
思想:
(1)POP3.user(username)
Send user command, response should indicate that a password is required.
(2)POP3.pass_(password),请注意我不是pass,而是pass_
Send password, response includes message count and mailbox size. Note: the mailbox on the server is locked until quit() is called.
(3)POP3.stat()
Get mailbox status. The result is a tuple of 2 integers: (message count, mailbox size).
(4)记得用raise ,对了那个密码是我乱写的,别猜了,但是qq是对的
拓展阅读:
python中的异常处理-kouyanghao-ChinaUnix博客(异常的各种用法)
import re def calcuteApacheIpHits(logfile_pathname): ip_specs = r'\.'.join([r'\d{1,3}']*4) re_ip = re.compile(ip_specs) ipHitListing = {} contents = open(logfile_pathname,"r") for line in contents: match = re_ip.match(line) if match : ip = match.group() #检查正确性 try: quad = map(int,ip.split('.')) except ValueError: pass else: #如果ip存在就+1,不存在就设置为1 if len(quad) == 4 and min(quad) >= 0 and max(quad) <= 255: ipHitListing[ip] = ipHitListing.get(ip,0) + 1 return ipHitListing print calcuteApacheIpHits("log.txt")
思想:
def clientCachePercentage(logfile_pathname): contents = open(logfile_pathname,"r") totalRequests = 0 cachedRequests = 0 for line in contents: totalRequests += 1 if line.split(" ")[8] == "304": cachedRequests += 1 return int(0.5+float(100 * cachedRequests)/totalRequests) print clientCachePercentage("1.txt")
思想:
import sys,os,tempfile def what_editor(): editor = os.getenv('VISUAL') or os.getenv('EDITOR') if not editor: if sys.platform == 'windows': editor = 'Notepad.Exe' else: editor = 'vi' return editor def edited_text(starting_text = ''): temp_fd,temp_filename = tempfile.mkstemp(text = True) os.write(temp_fd,starting_text) os.close(temp_fd) editor = what_editor() x = os.spawnlp(os.P_WAIT,editor,editor,temp_filename) if x: raise RuntimeError, "Can't run %s %s (%s)" % (editor,temp_filename,x) result = open(temp_filename).read() os.unlink(temp_filename) return result if __name__ == '__main__': text = edited_text('''aaaaaaaa bbbbbbb cccccccc''') print 'Edited text is:',text
import sys,os,shutil,filecmp MAXVERSIONS = 100 def backup(tree_top,bakdir_name = 'bakdir'): for root,subdirs,files in os.walk(tree_top): #join链接出每个root下的子目录bakdir backup_dir = os.path.join(root,bakdir_name) #确保每个root下都有个子目录叫bakdir if not os.path.exists(backup_dir): os.makedirs(backup_dir) #bakdir下的不递归处理 subdirs[:] = [d for d in subdirs if d != bakdir_name] for file in files: filepath = os.path.join(root,file) destpath = os.path.join(backup_dir,file) #检查版本,共有MAXVERSIONS个版本 for index in xrange(MAXVERSIONS): backup = "%s.%2.2d" % (destpath,index) if not os.path.exists(backup): break if index > 0: old_backup = "%s.%2.2d" % (destpath,index-1) #abspath = os.path.abspath(filepath)#filepath本来就是绝对路径 try: #如果备份和源文件一致,continue不处理 if os.path.isfile(old_backup) and filecmp.cmp(filepath,old_backup,shallow = False): continue except OSError: pass try: shutil.copy(filepath,backup) except OSError: pass if __name__ == '__main__': backup("c:\\test")
import winsound try: winsound.PlaySound("*",winsound.SND_ALIAS) except RuntimeError,e: print 'Sound system has problems,',e else: print 'Sound system is OK'
from ctypes import windll dll = windll[r'C:\\WINDOWS\\system32\\alrsvc.dll'] result = dll.DllRegisterServer() result = dll.DllUnregisterServer()
from ctypes import oledll dll = oledll[r'C:\\WINDOWS\\system32\\alrsvc.dll'] result = dll.DllRegisterServer() result = dll.DllUnregisterServer()
Ps:暂时没用到。先记录下来。
# -*- coding: cp936 -*- import _winreg as wr def how_many_tasks_at_logon(): areg = wr.ConnectRegistry(None,wr.HKEY_LOCAL_MACHINE) akey = wr.OpenKey(areg,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run') try: for i in xrange(1024): try: name,value,int_type = wr.EnumValue(akey,i) print i,name,value,int_type except EnvironmentError: print "You have",i,"tasks strating at logon" break finally: wr.CloseKey(akey) def wirte_to_registry(root_key,arg,value_name,value): areg = wr.ConnectRegistry(None,root_key) akey = wr.OpenKey(areg,arg,0,wr.KEY_WRITE) try: try: wr.SetValueEx(akey,value_name,0,wr.REG_SZ,value) except EnvironmentError: print "Encountered problems writing into the Registry" raise finally: wr.CloseKey(akey) def delete_from_registry(root_key,arg,value_name): areg = wr.ConnectRegistry(None,root_key) akey = wr.OpenKey(areg,arg,0,wr.KEY_WRITE) try: try: wr.DeleteValue(akey,value_name) except EnvironmentError: print "Encountered problems deleting key from Registry" raise finally: wr.CloseKey(akey) root_key = wr.HKEY_LOCAL_MACHINE arg = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run' value_name = "myOwnKey" value = r"C:\Program Files\多玩英雄联盟盒子\LOLBox.exe" print "=================reading=============== " how_many_tasks_at_logon() print "=================writing=============== " wirte_to_registry(root_key,arg,value_name,value) print "=================reading=============== " how_many_tasks_at_logon() print "=================deleting============== " delete_from_registry(root_key,arg,value_name) print "=================reading=============== " how_many_tasks_at_logon()
结果:
Ps:一开始编程把EnumValue写成EnumKey,一直报错,囧。还有把DeleteValue写成DeleteKey,囧。
import win32net import win32netcon import os print('>>>正在运行共享文件夹设置程序...') shinfo = {} folder = "c:\\test" shinfo['netname'] = os.path.basename(folder) shinfo['type'] = win32netcon.STYPE_DISKTREE shinfo['remark'] = 'data files' shinfo['premissions'] = 0 shinfo['max_uses'] = -1 shinfo['current_uses'] = 0 shinfo['path'] = 'c:\\test' shinfo['passwd'] = '' server = r'\\.' win32net.NetShareAdd(server,2,shinfo)
思想:
from win32com.client import Dispatch from win32gui import GetClassName ShellWindowsCLSID = '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}' ShellWindows = Dispatch(ShellWindowsCLSID) for shellwindow in ShellWindows: #筛选,Windows Explorer和Internet Explorer 是一样的,一个本地,一个网络 #这里要IE if GetClassName(shellwindow.HWND) == 'IEFrame': print shellwindow,shellwindow.LocationName,shellwindow.LocationURL
结果: