99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python實現(xiàn)運行其他程序的四種方式實例分析
Python實現(xiàn)運行其他程序的四種方式實例分析
2017-09-12
收藏

Python實現(xiàn)運行其他程序的四種方式實例分析

本文實例講述了Python實現(xiàn)運行其他程序的四種方式。分享給大家供大家參考,具體如下:

在Python中,可以方便地使用os模塊來運行其他腳本或者程序,這樣就可以在腳本中直接使用其他腳本或程序提供的功能,而不必再次編寫實現(xiàn)該功能的代碼。為了更好地控制運行的進程,可以使用win32process模塊中的函數(shù),如果想進一步控制進程,則可以使用ctype模塊,直接調(diào)用kernel32.dll中的函數(shù).

【方式一】使用os.system()函數(shù)運行其他程序
os模塊中的system()函數(shù)可以方便地運行其他程序或者腳本,模式如下: 
os.system(command)

command: 要執(zhí)行的命令,如果要向腳本傳遞參數(shù),可以使用空格分割程序及多個參數(shù)。

示例如下: 
>>> import os
>>> os.system('notepad')    # 打開記事本程序.
0
>>> os.system('notepad 1.txt') # 打開1.txt文件,如果不存在,則創(chuàng)建.
0
【方式二】使用ShellExecute函數(shù)運行其他程序
除了使用os.system()函數(shù)外,還可以使用win32api模塊中的ShellExecute()函數(shù)來運行其他程序,格式如下: 
ShellExecute(hwnd, op, file, args, dir, show)

hwnd:          父窗口的句柄,如果沒有父窗口,則為0
op  :          要運行的操作,為open,print或者為空
file:          要運行的程序,或者打開的腳本
args:          要向程序傳遞的參數(shù),如果打開的是文件則為空
dir :          程序初始化的目錄
show:          是否顯示窗口

示例如下:
>>> import win32api
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '', '', 0)      # 后臺執(zhí)行
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '', '', 1)      # 前臺打開
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '1.txt', '', 1)   # 打開文件
>>> win32api.ShellExecute(0, 'open', 'http://www.sohu.com', '', '', 1)  # 打開網(wǎng)頁
>>> win32api.ShellExecute(0, 'open', 'D:\\Opera.mp3', '', '', 1)     # 播放視頻
>>> win32api.ShellExecute(0, 'open', 'D:\\hello.py', '', '', 1)     # 運行程序
使用ShellExecute函數(shù),就相當于在資源管理器中雙擊文件圖標,系統(tǒng)會打開相應程序運行。
NOTE:
win32api安裝 http://sourceforge.net/projects/pywin32/files/pywin32/ 因我的是64的操作系統(tǒng),所以下載了這個:pywin32-216.win-amd64-py2.7
【方式三】使用ShellExecute函數(shù)運行其他程序
創(chuàng)建進程:
為了便于控制通過腳本運行的程序,可以使用win32process模塊中的CreateProcess()函數(shù)創(chuàng)建

一個運行相應程序的進程。其函數(shù)格式為: 
CreateProcess(appName, cmdLine, proAttr, threadAttr, InheritHandle, CreationFlags, newEnv, currentDir, Attr)

appName         可執(zhí)行文件名
cmdLine         命令行參數(shù)
procAttr        進程安全屬性
threadAttr      線程安全屬性
InheritHandle  繼承標志
CreationFlags  創(chuàng)建標志
currentDir      進程的當前目錄
Attr             創(chuàng)建程序的屬性
示例如下:
>>> win32process.CreateProcess('C:\\Windows\\notepad.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW,
None, None, win32process.STARTUPINFO())
(<PyHANDLE:892>, <PyHANDLE:644>, 21592, 18780) # 函數(shù)返回進程句柄、線程句柄、進程ID以及線程ID
結束進程:
可以使用win32process.TerminateProcess函數(shù)來結束已創(chuàng)建的進程, 函數(shù)格式如下:
TerminateProcess(handle, exitCode)

handle     要操作的進程句柄
exitCode   進程退出代碼

或者使用win32event.WaitForSingleObject等待創(chuàng)建的線程結束,函數(shù)格式如下:
WaitForSingleObject(handle, milisecond)

handle     : 要操作的進程句柄
milisecond: 等待的時間,如果為-1,則一直等待.

示例如下: 
>>> import win32process
>>> handle = win32process.CreateProcess('C:\\Windows\\notepad.exe', '', None, None, 0, win32process
.CREATE_NO_WINDOW, None, None, win32process.STARTUPINFO())      # 打開記事本,獲得其句柄
>>> win32process.TerminateProcess(handle[0], 0)            # 終止進程
或者   
>>> import win32event
>>> handle = win32process.CreateProcess('C:\\Windows\\notepad.exe', '', None, None, 0,
win32process.CREATE_NO_WINDOW, None, None, win32process.STARTUPINFO()) # 創(chuàng)建進程獲得句柄
>>> win32event.WaitForSingleObject(handle[0], -1)           # 等待進程結束
0                                   # 進程結束返回值
【方式四】使用ctypes調(diào)用kernel32.dll中的函數(shù)
使用ctypes模塊可以讓Python調(diào)用位于動態(tài)鏈接庫的函數(shù)。
ctypes模塊為Python提供了調(diào)用動態(tài)鏈接庫中函數(shù)的功能。使用ctypes模塊可以方便地調(diào)用由C語言編寫的動態(tài)鏈接庫,并向其傳遞參數(shù)。ctypes模塊定義了C語言中的基本數(shù)據(jù)類型,并且可以實現(xiàn)C語言中的結構體和聯(lián)合體。ctypes模塊可以工作在Windows,Linux,Mac OS等多種操作系統(tǒng),基本上實現(xiàn)了跨平臺。
示例:
Windows下調(diào)用user32.dll中的MessageBoxA函數(shù)。 
>>> from ctypes import *
>>> user32 = windll.LoadLibrary('user32.dll')
>>> user32.MessageBoxA(0, str.encode('Ctypes is so smart!'), str.encode('Ctypes'), 0)
1

ctype模塊中含有的基本類型與C語言類似,下面是幾個基本的數(shù)據(jù)類型的對照:
---------------------------------------
Ctypes數(shù)據(jù)類型           C數(shù)據(jù)類型
---------------------------------------
c_char                    char
c_short                   short
c_int                     int
c_long                    long
c_float                   float
c_doule                   double
c_void_p                  void *
---------------------------------------

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }