python --version : 3.7.0
update : 2018/09/24
六層標(biāo)題備忘 : (一) / 1. / (1) / <1> / (i) /<i>
(一)編碼問題
1. 'abc' 和 b'abc'
>>> 'abc'.encode()
b'abc'
>>> b'abc'.decode()
'abc'
python3有兩種字符序列類型: "str"和"bytes" 跺讯,分別對(duì)應(yīng)字符串的"文本狀態(tài)"和"字節(jié)流狀態(tài)"首繁。
python3中的字符串使用Unicode字符集進(jìn)行存儲(chǔ)隐锭,當(dāng)其類型為"str"時(shí),直接輸出會(huì)顯示對(duì)應(yīng)的文本狀態(tài); 而需要查看其對(duì)應(yīng)的字節(jié)流(二進(jìn)制)時(shí)渣磷,需要對(duì)"str"進(jìn)行編碼(encode)魄懂,需要選擇方案飘言,如utf-8,GBK等(python3中默認(rèn)編碼方案為utf-8); 反之由"bytes"類型向"str"轉(zhuǎn)換(decode)時(shí)峦睡,需要聲明之前的編碼方案。
>>> s = '中'
>>> type(s)
<class 'str'>
>>> sb = s.encode() # equal to "sb = s.encode(encoding = "utf-8")"
>>> sb
b'\xe4\xb8\xad'
>>> type(sb)
<class 'bytes'>
>>> sb.decode() # means "sb.decode(encoding = "utf-8")"
'中'
2. base64編碼
3 str * 8 bit --> 4 str * 6 bit
>>> import base64
>>> base64.b64encode('Life is short, you need python.'.encode())
b'TGlmZSBpcyBzaG9ydCwgeW91IG5lZWQgcHl0aG9uLg=='
>>> base64.b64decode(b'TGlmZSBpcyBzaG9ydCwgeW91IG5lZWQgcHl0aG9uLg==')
b'Life is short, you need python.'
(二)數(shù)制轉(zhuǎn)換
1. python中的整數(shù)
python中的整數(shù)不設(shè)上限嘉抒,可任意大铁蹈,處理起來非常方便。
python中的整數(shù)有4種常用的表示形式众眨,分別為十進(jìn)制握牧,二進(jìn)制,八進(jìn)制娩梨,十六進(jìn)制沿腰,其中二、八狈定、十六進(jìn)制的前綴分別為'0b'颂龙、'0o'、'0x'纽什,四種形式輸入IDLE措嵌,都被解釋為十進(jìn)制整數(shù),是一回事芦缰。
>>> 65
65
>>> 0b1000001
65
>>> 0o101
65
>>> 0x41
65
2. 擁有字符形式的整數(shù)
設(shè)整數(shù)的十進(jìn)制形式為n企巢, 當(dāng)32<=n<=126時(shí),其也可以表示可見字符的ASCII碼让蕾,可與對(duì)應(yīng)字符相互轉(zhuǎn)換浪规。
>>> chr(65)
'A'
>>> ord('A')
65
3.內(nèi)置數(shù)制轉(zhuǎn)換函數(shù)
可以將上述任意數(shù)制轉(zhuǎn)換為二、八探孝、十六進(jìn)制的函數(shù)為bin(x),oct(x),hex(x)笋婿,輸出為str。
>>> bin(65)
'0b1000001'
>>> oct(65) #八進(jìn)制對(duì)應(yīng)的str無前綴
'0101'
>>> hex(65)
'0x41'
>>> hex(0b1000001)
'0x41'
4.其他數(shù)制轉(zhuǎn)換方法
(1) 將任意數(shù)制(str)轉(zhuǎn)換為十進(jìn)制 : int('num', base)
這一函數(shù)常用于將str強(qiáng)制類型轉(zhuǎn)換成int顿颅。
>>> int('0x41',16)
65
>>> int('41',16)
65
>>> int('41') #默認(rèn)base = 10
41
(2) 不帶前綴的格式化輸出 : "{:base}".format(x)
>>> "{:b}".format(65)
'1000001'
>>> "{:x}".format(65)
'41'
>>> "{:o}".format(65)
'101'
5.字符串轉(zhuǎn)字節(jié)流
(1) binascii.hexlify()
有時(shí)需要將字符串與對(duì)應(yīng)的十六進(jìn)制字節(jié)流相互轉(zhuǎn)換(如rtf文檔的構(gòu)造或解析), 使用自帶庫binascii的函數(shù)可輕松解決缸濒。
>>> import binascii
>>> binascii.hexlify(b'abc')
b'616263'
>>> binascii.unhexlify(b'616263')
b'abc'
>>> binascii.b2a_hex(b'abc')
b'616263'
>>> binascii.a2b_hex(b'616263')
b'abc'
hexlify()和b2a_hex()都可將字符串轉(zhuǎn)為16進(jìn)制字節(jié)流,
unhexlify()和a2b_hex()都可將16進(jìn)制字節(jié)流轉(zhuǎn)為字符串。
(2) python2的技巧(python3不適用)
在python2中一行代碼即可搞定庇配,注意此方法在python3中會(huì)報(bào)錯(cuò)("hex"編解碼器已被刪除)斩跌。
>>> "abc".encode("hex")
'616263'
>>> '616263'.decode("hex")
'abc'
(三)輸入輸出
1. 格式化輸出需加括號(hào)
print("My name is %s" %("yourDaddy"))
2. 格式化輸出字符串的兩種方式
(1) %
>>> n = 123
>>> s = "%d" % n # 等價(jià)于強(qiáng)制類型轉(zhuǎn)換 s = str(n)
>>> s
'123'
(2) .format
<1> 常規(guī)填充
>>> "{} - {} cm".format('sandy',18)
'sandy - 18 cm'
<2> 數(shù)制轉(zhuǎn)換
>>> '{:b}'.format(18) # to 2
'10010'
>>> '{:x}'.format(18) # to 16
'12'
2. python3 取消了raw_input, 但還可以用input接收輸入
>>>message = input()
123
>>> message
'123'
3. sys.stdin.read()可以實(shí)現(xiàn)標(biāo)準(zhǔn)輸入, IDLE下"enter + ctrl + d"可終止輸入(linux下同樣適用),windows cmd下為"enter + ctrl + z + enter"
>>>import sys
>>>s=sys.stdin.read()
123
>>>s
'123\n'
(四)命令行下輸入?yún)?shù)的處理技巧
1. 獲取命令行下輸入的參數(shù) : sys.argv
命令行下執(zhí)行py腳本讨永,后加一堆參數(shù),則用sys.argv可將這些參數(shù)以列表形式存儲(chǔ)遇革,每個(gè)參數(shù)都是"str"類型卿闹,且列表首元素sys.argv[0] = "pythonFileName"
(列表長(zhǎng)度 = 輸入?yún)?shù) + 1)。
#test.py
import sys
parse = sys.argv
print(parse)
---
shell > test.py 1 2 3
['C:\\Users\\Administrator\\Desktop\\python3\\test.py','1','2','3']
shell >
2. 帶選項(xiàng)的輸入 : getopt庫
(1) 短選項(xiàng)模式
#getoptShortMode.py
import getopt
inputt = '-n cos -e -d 18cm add'.split()
print("Input: %s" % (inputt))
opts, args = getopt.getopt(inputt, 'n:ed:')
print("opts: %s" % (opts))
print("args: %s" % (args))
---
shell > getoptShortMode.py
Input: ['-n', 'cos', '-e', '-d', '18cm', 'add']
opts: [('-n', 'cos'), ('-e', ''), ('-d', '18cm')]
args: ['add']
短選項(xiàng)模式假定py文件后面跟的都是形如"-選項(xiàng) 參數(shù)"的格式萝快,其中"選項(xiàng)"為單字母(這就是為什么叫短選項(xiàng))锻霎,"參數(shù)"可以為空;
短模式函數(shù)引用形式為opts,args = getopt(input,shortOpts)
,input
就是輸入的"選項(xiàng)-參數(shù)"流,上例中的shortOpts = "n:ed:"
揪漩, 其首先將所有選項(xiàng)的字母收集進(jìn)來旋恼,選項(xiàng)后如果有參數(shù),則選項(xiàng)后跟冒號(hào)(如"n:"),否則不跟(如"e");
getopt依據(jù)shortOpts的規(guī)則將Input分成tuple奄容,存入opts冰更,Input中沒在shortOpts中出現(xiàn)的部分劃到args里。
注意shortopts里選項(xiàng)的順序不一定非要和input里的選項(xiàng)順序一致昂勒,最后opts里tuple的順序是跟input里選項(xiàng)出現(xiàn)的先后順序一致蜀细;
但沒有選項(xiàng)的參數(shù)(如'add')一定要放到最后,否則其后的選項(xiàng)不被識(shí)別戈盈。
(3) 長(zhǎng)選項(xiàng)模式
#getoptLongMode.py
import getopt
inputt = '--name=cos --dick=18cm --r unkown'.split()
print("Input: %s" % (inputt))
opts, args = getopt.getopt(inputt, '', ['name=', 'dick=', 'r'])
print("opts: %s" % (opts))
print("args: %s" % (args))
---
shell > getoptLongMode.py
Input: ['--name=cos', '--dick=18cm', '--r', 'unkown']
opts: [('--name', 'cos'), ('--dick', '18cm'), ('--r', '')]
args: ['unkown']
長(zhǎng)選項(xiàng)假定后面跟的都是形如"--選項(xiàng)=參數(shù)"或"--選項(xiàng)"的格式奠衔,注意shortOpt=''
。
(3) 混合模式
#mixMode.py
import getopt
inputt = '--name=cos -f handsome --rich --dick=18cm -h 185 -r add unkown'.split()
opts, args = getopt.getopt(inputt, 'f:rh:', ['name=', 'rich', 'dick='])
print("input: %s " % (inputt))
print("opts: %s " % (opts))
print("args: %s " % (args))
---
shell > mixMode.py
input: ['--name=cos', '-f', 'handsome', '--rich', '--dick=18cm', '-h', '185', '-r', 'add', 'unkown']
opts: [('--name', 'cos'), ('-f', 'handsome'), ('--rich', ''), ('--dick', '18cm'), ('-h', '185'), ('-r', '')]
args: ['add', 'unkown']
shell >
3. 自動(dòng)生成使用幫助 : argparse庫
python 2.7以后棄用optparse模塊塘娶,改用argparse替代归斤。
(1) 用法
#test2.py
import argparse
parser = argparse.ArgumentParser(description = "argparse test")
parser.add_argument("-t", help="target IP")
cmdInput = parser.parse_args()
---
shell> test2.py -h
usage: test2.py [-h] [-t T]
argparse test
optional arguments:
-h, --help show this help message and exit
-t T target IP
命令行下輸入".py文件" + "-h"自動(dòng)生成用法幫助信息;
關(guān)鍵函數(shù)是add_args,其向命令行添加輸入選項(xiàng)刁岸,幫助信息存放于help參數(shù)中(當(dāng)然也可以不要)脏里。
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("-t")
_StoreAction(option_strings=['-t'], dest='t', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
(2) add_args函數(shù)參數(shù)詳解
<1> 可選參數(shù) : option_strings = "-t" 或 option_strings = "-p", "--port"
用法: shell> test2.py -p 4444
或 shell>test2.py --port 4444
#test2.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-t")
parser.add_argument("-p","--port")
cmdInput = parser.parse_args()
print("target input: %s %s" % (cmdInput.t,type(cmdInput.t)))
print("port input: %s" % (cmdInput.port)
shell>test2.py -t 127.0.0.1 --port 4444
target input: 127.0.0.1 <class 'str'>
port input: 4444
當(dāng)指定選項(xiàng)名為形如"-t"這樣的短選項(xiàng)時(shí),后面跟的具體參數(shù)會(huì)以字符串類型保存于"parse_args().t"中虹曙;
而當(dāng)選項(xiàng)名為"-p"+"--port"長(zhǎng)短結(jié)合的形式膝宁,參數(shù)會(huì)保存于"parse_args().port"中,當(dāng)然長(zhǎng)短形式的選項(xiàng)都可以使用根吁。
<2> 位置參數(shù) : option_strings = "target"
用法: shell> test2.py 127.0.0.1
#test2.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("target")
cmdInput = parser.parse_args()
print("target: %s " % (cmdInput.target))
---
shell> test2.py 127.0.0.1
target: 127.0.0.1
使用位置參數(shù)時(shí)员淫,無需輸入選項(xiàng),直接輸入?yún)?shù)击敌,參數(shù)會(huì)保存在"parse_args().target"中介返。
<3> 不需要參數(shù)的選項(xiàng) : action = "store_true"
用法: shell> test2.py -t
#test2.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-t", action="store_true")
cmdInput = parser.parse_args()
print(cmdInput.t)
print(type(cmdInput.t))
---
shell> test2.py -t
True
<class 'bool'>
參數(shù)action是用來處理當(dāng)捕獲到輸入?yún)?shù)后下一步的動(dòng)作,默認(rèn)為 action = "store",即將參數(shù)保存到指定變量圣蝎;
action = "store_true"表示選項(xiàng)后無需輸入?yún)?shù)刃宵,默認(rèn)參數(shù)為bool型的"True",將其存入變量中徘公。
<4> 其他參數(shù)
- type = int : 函數(shù)默認(rèn)輸入?yún)?shù)保存為字符類型牲证,使用type可將輸入?yún)?shù)保存為其他類型。
- required = True : 默認(rèn)該參數(shù)為"False"关面,賦值后選項(xiàng)變?yōu)楸仨気斎腠?xiàng)坦袍,不輸入則報(bào)錯(cuò)
(五)進(jìn)程處理技巧
1. 進(jìn)程中開啟多線程 : threading庫
- 進(jìn)程(process)就是一個(gè)應(yīng)用程序的一次執(zhí)行過程,線程(thread)是進(jìn)程中執(zhí)行運(yùn)算的最小單位等太。
- 操作系統(tǒng)把資源分配給進(jìn)程捂齐,而線程可以在其中獨(dú)立運(yùn)行和調(diào)度。
- 一個(gè)進(jìn)程可以有多個(gè)線程缩抡,一個(gè)線程必須屬于某個(gè)進(jìn)程奠宜。
- 若一個(gè)進(jìn)程是單線程的,若此進(jìn)程要執(zhí)行三個(gè)任務(wù)瞻想,則只能串行執(zhí)行(即單線程按任務(wù)順序依次執(zhí)行)压真;多線程是指若一進(jìn)程有三個(gè)線程,要執(zhí)行三個(gè)任務(wù)蘑险,則三個(gè)線程可同時(shí)并行處理(CPU在同一時(shí)間只能處理一條線程榴都,之所以可以多線程并行處理CPU在多條線程間可以快速切換,造成了同時(shí)進(jìn)行的假象)漠其。
(1) 用法
#Usage:
import threading
threadd = threading.Thread(target = function, args = (funcParameter1,funcParameter1)) #create thread嘴高,execute target(function) with/without arguments
threadd.start()
(2) 簡(jiǎn)單示例
#multiThread.py
import threading
def printNum(Num,Counts):
for i in range(Counts):
print(str(Num)*Counts)
counts = 5
thread1 = threading.Thread(target = printNum, args = (1,counts))
thread2 = threading.Thread(target = printNum, args = (0,counts))
thread1.start()
thread2.start()
---
========== RESTART: C:\Users\Administrator\Desktop\python3\multiThread.py ==========
11111
11111
1111100000
>>>
11111
11111
00000
00000
00000
00000
2. 由窗口到進(jìn)程 : ctypes.windll.user32.GetWindowThreadProcessId()
- PID : 進(jìn)程在被創(chuàng)建時(shí)系統(tǒng)內(nèi)核會(huì)為其分配一個(gè)固定不變的序列號(hào)(PID),供函數(shù)或其他進(jìn)程調(diào)用和屎,進(jìn)程終止后會(huì)被回收進(jìn)行二次分配拴驮。
- 進(jìn)程句柄(號(hào)) : 是指針,動(dòng)態(tài)變化柴信,當(dāng)訪問該進(jìn)程時(shí)得到套啤,用完必須釋放。
- 窗口 : 一個(gè)進(jìn)程可以有多個(gè)窗口(想象firefox.exe開兩個(gè)窗口上網(wǎng))随常,窗口也有對(duì)應(yīng)的窗口句柄(號(hào))供系統(tǒng)調(diào)用潜沦,同樣是指針,每次調(diào)用也動(dòng)態(tài)變化绪氛。
#tryCtypes.py
import ctypes
#get window handle
handle = ctypes.windll.user32.GetForegroundWindow()
#get window title
windowTitle = ctypes.create_string_buffer(("\x00" * 512).encode())
length = ctypes.windll.user32.GetWindowTextA(handle, ctypes.byref(windowTitle), 512)
#get PID
pid = ctypes.c_ulong(0)
ctypes.windll.user32.GetWindowThreadProcessId(handle, ctypes.byref(pid))
#get process name
peName = ctypes.create_string_buffer(("\x00" * 512).encode())
hProcess = ctypes.windll.kernel32.OpenProcess(0x400 | 0x10, False, pid)
ctypes.windll.psapi.GetModuleBaseNameA(hProcess,None, ctypes.byref(peName), 512)
kernel32.CloseHandle(handle)
kernel32.CloseHandle(hProcess)
print("[*] current window:")
print("[*] window handle: %s" % handle)
print("[*] pid(process ID): %s" % pid.value)
print("[*] process name: %s" % peName.value.decode())
print("[*] window title: %s" % windowTitle.value.decode())
---
>>>
========== RESTART: C:\Users\Administrator\Desktop\python3\tryCtypes.py ==========
[*] current window:
[*] window handle: 2360406
[*] pid(process ID): 4172
[*] process name: pythonw.exe
[*] window title: *Python 3.7.0 Shell*
>>>
(六) windows進(jìn)階
1. windows消息機(jī)制和鉤子 : pyHook庫
(1) windows的窗口程序基于消息機(jī)制唆鸡,并由事件驅(qū)動(dòng)。
- 事件(event) : 事件是由用戶通過外部輸入設(shè)備觸發(fā)的動(dòng)作枣察,如點(diǎn)擊鼠標(biāo)左鍵争占,敲擊鍵盤都會(huì)產(chǎn)生一個(gè)事件燃逻。
(除此之外事件還可能由諸如button控件、file菜單觸發(fā)臂痕,也有來自windows內(nèi)部的事件) - 消息(message) : 事件產(chǎn)生消息伯襟。當(dāng)一個(gè)事件被觸發(fā)后,會(huì)被windows翻譯(量化)成一個(gè)消息握童。
消息是一個(gè)結(jié)構(gòu)體數(shù)據(jù)姆怪,里面包含窗口句柄(消息所屬窗口),消息編號(hào)澡绩,和兩個(gè)DWORD消息參數(shù)稽揭。 - windows消息機(jī)制
<1> 當(dāng)用戶運(yùn)行一個(gè)應(yīng)用程序時(shí),通過點(diǎn)擊鼠標(biāo)或者敲鍵盤英古,產(chǎn)生了一些事件淀衣;
<2> 事件被windows監(jiān)控到昙读,量化為消息召调,消息被windows捕獲到之后放到windows的消息隊(duì)列蛮浑;
<3> windows在分析該消息之后得知它應(yīng)該屬于哪個(gè)應(yīng)用程序,就把它存到該程序自己的消息隊(duì)列中沮稚;
<4> 每個(gè)程序都有一個(gè)消息循環(huán),不斷從自己的消息隊(duì)列中讀取消息蕴掏,并將其分配給相應(yīng)的線程或窗口,交由窗口處理函數(shù)處置盛杰。
(2) 鉤子(函數(shù))
- 前文提到,當(dāng)一個(gè)消息被windows分析之后要發(fā)往相應(yīng)程序的消息隊(duì)列即供。
鉤子就是(在消息被發(fā)往程序的消息隊(duì)列之前)截獲消息的函數(shù)。 - windows的原生函數(shù)SetWindowsEx允許我們自定義鉤子逗嫡,從而去截獲特定事件生成的消息。
pyHook庫就是基于這一函數(shù)進(jìn)行封裝編寫的驱证。 - pyHook是第三方庫延窜,但無法pip快捷安裝。
[pyHook下載地址]
下載完成后輸入pip install xxx.whl
安裝即可抹锄。
注意64位windows7下安裝的python有可能是32位的(使用platform.architecture()
查看python架構(gòu))需曾。
(3) 應(yīng)用 : 鍵盤/鼠標(biāo)記錄
#example.py
import pyHook
def OnMouseEvent(event):
print('MessageName:',event.MessageName)
print('Message:',event.Message)
print('Time:',event.Time)
print('Window:',event.Window)
print('WindowName:',event.WindowName)
print('Position:',event.Position)
print('Wheel:',event.Wheel)
print('Injected:',event.Injected)
print('---')
# return True to pass the event to other handlers
# return False to stop the event from propagating
return True
def OnKeyboardEvent(event):
print('MessageName:',event.MessageName)
print('Message:',event.Message)
print('Time:',event.Time)
print('Window:',event.Window)
print('WindowName:',event.WindowName)
print('Ascii:', event.Ascii, chr(event.Ascii))
print('Key:', event.Key)
print('KeyID:', event.KeyID)
print('ScanCode:', event.ScanCode)
print('Extended:', event.Extended)
print('Injected:', event.Injected)
print('Alt', event.Alt)
print('Transition', event.Transition)
print('---')
# return True to pass the event to other handlers
# return False to stop the event from propagating
return True
# create the hook mananger
hm = pyHook.HookManager()
# register two callbacks
hm.MouseAllButtonsDown = OnMouseEvent
hm.KeyDown = OnKeyboardEvent
# hook into the mouse and keyboard events
hm.HookMouse()
hm.HookKeyboard()
if __name__ == '__main__':
import pythoncom
pythoncom.PumpMessages()
---
>>>
RESTART: C:\Program Files (x86)\Python37-32\Lib\site-packages\pyHook\example.py
MessageName: mouse left down
Message: 513
Time: 179313024
Window: 65680
WindowName: Running applications
Position: (278, 1015)
Wheel: 0
Injected: 0
---
MessageName: key down
Message: 256
Time: 179317158
Window: 1312710
WindowName: Administrator: Command Prompt
Ascii: 73 I
Key: I
KeyID: 73
ScanCode: 23
Extended: 0
Injected: 0
Alt 0
Transition 0
2. 設(shè)備描述表(未完待續(xù))
windows顯示原理 : 用戶操作 --> 設(shè)備描述表 --> 顯卡驅(qū)動(dòng)程序 --> 顯卡硬件 --> 顯示器
設(shè)備描述符(Device Context, DC) : 又稱設(shè)備上下文吗坚,用以實(shí)現(xiàn)應(yīng)用程序與硬件之間的交互。
DC是一種數(shù)據(jù)結(jié)構(gòu)呆万,其結(jié)構(gòu)的核心是位圖(bitmap)商源。
GDI(Graphics Device Interface, 圖形設(shè)備接口) ?
DC是GDI函數(shù)內(nèi)部保留的數(shù)據(jù)結(jié)構(gòu)
- 應(yīng)用 : 屏幕快照
#pywin32 library
import win32gui
import win32ui
import win32con
import win32api
#1.獲取當(dāng)前桌面的句柄和像素尺寸
hdesktop = win32gui.GetDesktopWindow()
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
#2.獲取當(dāng)前桌面的設(shè)備描述表,用以創(chuàng)建截圖用的設(shè)備描述表
desktopDC = win32gui.GetWindowDC(hdesktop)
imgDC = win32ui.CreateDCFromHandle(desktopDC)
memDC = imgDC.CreateCompatibleDC()
screenshot = win32ui.CreateBitmap()
screenshot.CreateCompatibleBitmap(imgDC, width, height)
memDC.SelectObject(screenshot)
memDC.BitBlt((0, 0), (width, height), imgDC, (left, top), win32con.SRCCOPY)
screenshot.SaveBitmapFile(memDC, 'C:\\Users\\Administrator\\Desktop\\screenshot.bmp')
memDC.DeleteDC()
win32gui.DeleteObject(screenshot.GetHandle())
python實(shí)現(xiàn)全屏截圖(兩個(gè)程序?qū)Ρ戎茨奔酰^續(xù)學(xué)習(xí)這部分內(nèi)容)
import time
import os, win32gui, win32ui, win32con, win32api
def window_capture():
hwnd = 0
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC=win32ui.CreateDCFromHandle(hwndDC)
saveDC=mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
MoniterDev=win32api.EnumDisplayMonitors(None,None)
w = MoniterDev[0][2][2]
h = MoniterDev[0][2][3]
print w,h
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
saveDC.SelectObject(saveBitMap)
saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY)
bmpname=win32api.GetTempFileName(".","")[0]+'.bmp'
saveBitMap.SaveBitmapFile(saveDC, bmpname)
return bmpname
os.system(window_capture())
3. pywin32庫(pip)
import pythoncom
import win32clipboard
4. ctypes庫
5. 命令行下與shell交互的處理技巧
(1) 如何與命令行交互(腳本命令放入命令行執(zhí)行牡彻,結(jié)果返回)
(2) subprocess庫
(七)導(dǎo)入模塊的三種方法的區(qū)別
1. import module
方法型引用
>>> import math
>>> math.pi
3.141592653589793
2. from module import *
將模塊下所有名字引入當(dāng)前名稱空間,直接引用出爹。
>>> from math import *
>>> pi
3.141592653589793
3. import module as xxx
用xxx來代替module進(jìn)行引用
>>> import math as m
>>> m.pi
3.141592653589793
(八)待研究問題
1.深拷貝與淺拷貝
- !!!!!!!!!!!! 類 class !!!!!
- py文件封裝成exe : XXX庫
- 爬蟲
- GUI編程 : PyQt
- json
- 正則表達(dá)式