[PyWin]PyWin实现windows下以管理员身份启动cmd

</pre><pre name="code" class="python">import subprocess
import win32api
import win32con
import win32gui

key_code_table ={'a': 65,
                 'b': 66,
                 'c': 67,
                 'd': 68,
                 'e': 69,
                 'f': 70,
                 'g': 71,
                 'h': 72,
                 'i': 73,
                 'j': 74,
                 'k': 75,
                 'l': 76,
                 'm': 77,
                 'n': 78,
                 'o': 79,
                 'p': 80,
                 'q': 81,
                 'r': 82,
                 's': 83,
                 't': 84,
                 'u': 85,
                 'v': 86,
                 'w': 87,
                 'x': 88,
                 'y': 89,
                 'z': 90,
                 '\\': 220,
                 '=': 187,
                 'Space': 32,
                 'Enter': 13,
                 '‘ “': 222,
                 '/': 111,
                 '.': 110,
                 'Shift': 16,
                 '1': 49,
                 '2': 50,
                 '3': 51,
                 '4': 52, 
                 '5': 53,
                 '6': 54,
                 '7': 55,
                 '8': 56}

def test():
    win_cmd = subprocess.Popen(["runas","/user:spss\wujz","cmd"])
    time.sleep(5)
    win_name = "C:\\windows\\system32\\runas.exe"
    ps = win32gui.FindWindow(0,win_name)
    win32gui.SetForegroundWindow(ps)

    ch = 'p'
    string = 'ass5678'
    list1 = [ch for ch in string]

    win32api.keybd_event(key_code_table['Shift'],0,0,0)
    win32api.keybd_event(key_code_table['p'],0,0,0)
    win32api.keybd_event(key_code_table['p'],0,win32con.KEYEVENTF_KEYUP,0)
    win32api.keybd_event(key_code_table['Shift'],0,win32con.KEYEVENTF_KEYUP,0)
    
    code_list = list()
    for item in list1:
        print(item)
        print(key_code_table[item])
        code_list.append(key_code_table[item])

    for item in code_list:
        win32api.keybd_event(item,0,0,0)
        win32api.keybd_event(item,0,win32con.KEYEVENTF_KEYUP,0)
    win32api.keybd_event(13,0,0,0)
    win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0)
    
    

if __name__ == '__main__':  
    test()




posted @ 2015-05-25 15:53  IronJJ  阅读(227)  评论(0编辑  收藏  举报