利用python帮助自己输入用户名密码
本帖最后由 ypcgamelife 于 2021-4-8 09:28 编辑在正常办公情况下,需要经常登录各种系统,每次记密码都是一个麻烦事,还需要每次输入很长的用户名和密码。
本人现在利用python开发出自动用户名密码输入程序,将光标放在输入用户名的地方,按热键会自动输入用户名,密码,
目前设置了4个热键,可以输入4个用户名、密码。大家根据源程序开发出自己需要的用户名,密码数量。(注册热键代码来自互联网)
我将用户名密码放在同目录下的文件中并通过加密函数加密,大家可以自行赋值用户名,密码也可以。
有问题可以私信。
# -*- coding: utf-8 -*-
import win32con
import ctypes
import ctypes.wintypes
from threading import Thread, activeCount, enumerate,current_thread
import time
import mymessagebox as mybox
import pyautogui
import sys
import os
import pyperclip
class Hotkey(Thread):
user32 = ctypes.windll.user32
hkey_list = {}
hkey_flags = {}# 按下
hkey_running = {}# 启停
_reg_list = {}# 待注册热键信息
def regiskey(self, hwnd=None, flagid=0, fnkey=win32con.MOD_ALT, vkey=win32con.VK_F9):# 注册热键,默认一个alt+F9
return self.user32.RegisterHotKey(hwnd, flagid, fnkey, vkey)
def get_reginfo(self):
return self._reg_list
def get_id(self, func):
self_id = None
for id in self.get_reginfo():
if self.get_reginfo()["func"] == func:
self_id = id
break
if self_id:
self.hkey_running = True
return self_id
def get_running_state(self, self_id):
if self.hkey_running.get(self_id):
return self.hkey_running
else:
return False
def reg(self, key, func, args=None):
id = int(str(round(time.time() * 10))[-6:])
fnkey = key
vkey = key
info = {
"fnkey": fnkey,
"vkey": vkey,
"func": func,
"args": args
}
self._reg_list = info
# print(info) #这里待注册的信息
time.sleep(0.1)
return id
def fast_reg(self, id, key=(0, win32con.VK_HOME), func=lambda: print('热键注册开始')):
if not self.regiskey(None, id, key, key):
print("热键注册失败")
return None
self.hkey_list = func
self.hkey_flags = False
return id
def callback(self):
def inner(self=self):
for flag in self.hkey_flags:
self.hkey_flags = False
while True:
for id, func in self.hkey_list.items():
if self.hkey_flags:
args = self._reg_list["args"]
if args:
# print(args)#这里打印传入给注册函数的参数
thread_it(func, *args)
else:
thread_it(func)
self.hkey_flags = False
return inner
def run(self):
for id in self._reg_list:
reg_info = self._reg_list
fnkey = reg_info["fnkey"]
vkey = reg_info["vkey"]
func = reg_info["func"]
self.fast_reg(id, (fnkey, vkey), func)
fn = self.callback()
thread_it(fn)# 启动监听热键按下线程
try:
msg = ctypes.wintypes.MSG()
while True:
if self.user32.GetMessageA(ctypes.byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if msg.wParam in self.hkey_list:
self.hkey_flags = True
self.user32.TranslateMessage(ctypes.byref(msg))
self.user32.DispatchMessageA(ctypes.byref(msg))
finally:
for id in self.hkey_list:
self.user32.UnregisterHotKey(None, id)
def thread_it(func, *args):
t = Thread(target=func, args=args)
t.setDaemon(True)
t.start()
def checkfile(myfile):
if not os.path.isfile(myfile):
#mybox.mymessage('没有维护用户名和密码,请维护 %s ,第一行用户名,第二行密码。'%(myfile))
print('没有维护用户名和密码,请维护 %s ,第一行用户名,第二行密码。'%(myfile))
sys.exit(0)
#quit()
f = open(myfile)
suser = f.readline().strip()
spass = f.readline().strip()
f.close()
if len(suser) < 32:
f = open(myfile, 'w')
f.write(mybox.myencrypt(suser))
f.write('\n')
f.write(mybox.myencrypt(spass))
f.close()
else:
suser = mybox.mydecrypt(suser).strip()
spass = mybox.mydecrypt(spass).strip()
return suser,spass
def inputuser(suser,spass):
pyautogui.hotkey('ctrlleft', 'a')
pyautogui.press('delete')
pyautogui.typewrite(suser, 0.25)
pyautogui.press('tab')
pyautogui.typewrite(spass, 0.25)
pyautogui.press('enter')
def jumpf9(func, hotkey):
self_id = hotkey.get_id(func)
#while hotkey.get_running_state(self_id):
# print(f"{self_id : } 你正在1秒1次的跳动")
# sleep(1)
x, y = pyautogui.position()
#pyautogui.moveTo(x,y)
#pyautogui.click()
#pyautogui
myfile = 'altf9.txt'
suser,spass=checkfile(myfile)
inputuser(suser,spass)
return
def jumpf10(func, hotkey):
self_id = hotkey.get_id(func)
#while hotkey.get_running_state(self_id):
# sleep(1)
x, y = pyautogui.position()
#pyautogui.moveTo(x,y)
#pyautogui.click()
#pyautogui
myfile = 'altf10.txt'
suser,spass=checkfile(myfile)
inputuser(suser,spass)
return
def jumpf11(func, hotkey):
self_id = hotkey.get_id(func)
x, y = pyautogui.position()
#pyautogui.moveTo(x,y)
#pyautogui.click()
#pyautogui
myfile = 'altf11.txt'
suser, spass = checkfile(myfile)
inputuser(suser, spass)
return
#中文用复制方式
#pyperclip.copy(content)
#pyautogui.hotkey('ctrlleft', 'v')
def jumpf12(func, hotkey):
self_id = hotkey.get_id(func)
x, y = pyautogui.position()
#pyautogui.moveTo(x,y)
#pyautogui.click()
#pyautogui
myfile = 'altf12.txt'
suser, spass = checkfile(myfile)
inputuser(suser, spass)
return
def stop_jump(start_id, hotkey):
hotkey.hkey_running = False
print(f"{start_id} 即将停止,退出系统")
time.sleep(1)
#print(f'当前线程列表:{activeCount()}', enumerate())
os._exit(0)
def main():
hotkey = Hotkey()
start_id = hotkey.reg(key=(win32con.MOD_ALT, win32con.VK_F9), func=jumpf9, args=(jumpf9, hotkey))# alt f9键 开始
start_id10 = hotkey.reg(key=(win32con.MOD_ALT, win32con.VK_F10), func=jumpf10, args=(jumpf10, hotkey))# alt f10键 开始
start_id11 = hotkey.reg(key=(win32con.MOD_ALT, win32con.VK_F11), func=jumpf11, args=(jumpf11, hotkey))# alt f11键 开始
start_id12 = hotkey.reg(key=(win32con.MOD_ALT, win32con.VK_F12), func=jumpf12, args=(jumpf12, hotkey))# alt f12键 开始
hotkey.reg(key=(win32con.MOD_ALT, win32con.VK_F8), func=stop_jump, args=(start_id, hotkey))# alt f8键 退出程序
hotkey.start()# 启动热键主线程
print(f"当前总线程数量:{activeCount()}")
print('当前线程列表:', enumerate())
print('热键注册初始化完毕,尝试按组合键alt+f9-12输入不同用户名密码 或者alt+f8退出程序')
def myexit():
while True:
if time.strftime("%H:%M:%S", time.localtime()) >= "17:55":
print('收拾东西准备下班,自动结束程序!!!')
os._exit(0)
if __name__ == '__main__':
#多线程 一个线程运行热键,一个线程特定条件退出程序
threads=[]
threads.append(Thread(target=main))
threads.start()
threads.append(Thread(target=myexit))
threads.start()
threads.join()
threads.join()
print(current_thread().name)
os._exit(0)
#main()
应大家需要,补充完善自己的收集的包。mymessagebox.py
import tkinter
from tkinter import messagebox
import base64
from cryptography.fernet importFernet
import cv2
import pyscreenshot as ImageGrab
import time
#import threading
from threading import Thread, Timer, activeCount, enumerate,current_thread
import sys
import win32con
import ctypes
def prnscreen(src_obj,sx1,sy1,sx2,sy2):
# im = ImageGrab.grab(bbox=(0,0,300,600))
if sx2>0 and sy2>0:
im = ImageGrab.grab(bbox=(sx1,sy1,sx2,sy2))
im.save(src_obj)
else:
im = ImageGrab.grab()
im.save(src_obj)
#比较2张图片,如果目标图片与原图片相似度打85%,返回坐标,否则返回-1
def find_image_cv(obj_path, src_path):
# basefolder = os.path.abspath('.') + "\\source\\"
#ImageGrab.grab().save(src_path)
source = cv2.imread(src_path,1)
template = cv2.imread(obj_path,1)
result = cv2.matchTemplate(source, template, cv2.TM_CCOEFF_NORMED)
#print(result)
pos_start = cv2.minMaxLoc(result)
test = cv2.minMaxLoc(result)
#print(test)
#print(pos_start)
x = int(pos_start) + int(template.shape / 2)
y = int(pos_start) + int(template.shape / 2)
similarity = cv2.minMaxLoc(result)
if similarity < 0.85:
#print(-1)
return (-1, -1)
else:
#print("pass")
return (x, y)
def myencrypt(content):
#return base64.b64encode(content.encode()).decode()
c_key = b'5c7cQIvgXRBAW7qtLdZSi2yBwzjeCMmElvgtcYHHf4Q='
# c_key=Fernet.generate_key()
cipher = Fernet(c_key)
mytest = str.encode(content)
e_text = cipher.encrypt(mytest)
return bytes.decode(e_text)
def mydecrypt(content):
#return base64.b64decode(content).decode()
c_key = b'5c7cQIvgXRBAW7qtLdZSi2yBwzjeCMmElvgtcYHHf4Q='
# c_key=Fernet.generate_key()
cipher = Fernet(c_key)
mytest = str.encode(content)
d_text = cipher.decrypt(mytest)
return(bytes.decode(d_text))
def mymessage(mess):
root=tkinter.Tk()
root.attributes("-topmost",True)
root.withdraw() #隐藏窗口
messagebox.showinfo("提示信息",mess)
def mymessageok(mess):
root=tkinter.Tk()
root.attributes("-topmost",True)
root.withdraw() #隐藏窗口
return messagebox.askyesno("提示信息",mess)
#创建应用程序窗口,设置标题和大小,等待多少秒后自动关闭窗口
def displayinfo(info, waittime):
#print(info)
#print(waittime)
global root
global waittime_all
if waittime<1:
waittime_all=3
else:
waittime_all=waittime
root=tkinter.Tk()
root.title('提示信息')
root['width']=600
root['height']=80
root.attributes("-topmost", True)
#print(root.winfo_pathname(root.winfo_id()))
#root.eval('tk::PlaceWindow %s center' % root.winfo_pathname(root.winfo_id()))
#不允许改变窗口大小
root.resizable(False,False)
root.geometry('%dx%d+%d+%d' % (600 , 80 ,(root.winfo_screenwidth() - 600) / 2 ,(root.winfo_screenheight() - 80) / 2))
#创建Text组件,放置一些文字
#richText=tkinter.Text(root,width=380)
#richText.place(x=10,y=10,width=380,height=30)
#richText.insert('0.0','假设阅读这些文字需要10秒钟时间')
#显示倒计时的Label
lbTime=tkinter.Label(root,fg='black',anchor='w')
lbTime.place(x=10,y=10,width=380)
lbTime['text']=info
def autoClose():
global root
#if waittime<1:
# waittime=3
time.sleep(waittime_all)
#for i in range(waittime):
# #lbTime['text']='距离窗口关闭还有{}秒'.format(10-i)
# time.sleep(1)
#root.destroy()
root.withdraw()
root.quit()
#sys.exit(1)
#print('xx1')
#创建并启动线程
global t
t=Thread(target=autoClose)
t.start()
#t.join()
root.mainloop()
#print('xx2')
#sys.exit(0)
#root.destroy()
#root.eval('tk::PlaceWindow %s center' % root.winfo_pathname(root.winfo_id()))
#time.sleep(5)
#root.destroy()
#注册热键
class Hotkey(Thread):
user32 = ctypes.windll.user32
hkey_list = {}
hkey_flags = {}# 按下
hkey_running = {}# 启停
_reg_list = {}# 待注册热键信息
def regiskey(self, hwnd=None, flagid=0, fnkey=win32con.MOD_ALT, vkey=win32con.VK_F9):# 注册热键,默认一个alt+F9
return self.user32.RegisterHotKey(hwnd, flagid, fnkey, vkey)
def get_reginfo(self):
return self._reg_list
def get_id(self, func):
self_id = None
for id in self.get_reginfo():
if self.get_reginfo()["func"] == func:
self_id = id
break
if self_id:
self.hkey_running = True
return self_id
def get_running_state(self, self_id):
if self.hkey_running.get(self_id):
return self.hkey_running
else:
return False
def reg(self, key, func, args=None):
id = int(str(round(time.time() * 10))[-6:])
fnkey = key
vkey = key
info = {
"fnkey": fnkey,
"vkey": vkey,
"func": func,
"args": args
}
self._reg_list = info
# print(info) #这里待注册的信息
sleep(0.1)
return id
def fast_reg(self, id, key=(0, win32con.VK_HOME), func=lambda: print('热键注册开始')):
if not self.regiskey(None, id, key, key):
print("热键注册失败")
return None
self.hkey_list = func
self.hkey_flags = False
return id
def callback(self):
def inner(self=self):
for flag in self.hkey_flags:
self.hkey_flags = False
while True:
for id, func in self.hkey_list.items():
if self.hkey_flags:
args = self._reg_list["args"]
if args:
# print(args)#这里打印传入给注册函数的参数
thread_it(func, *args)
else:
thread_it(func)
self.hkey_flags = False
return inner
def run(self):
for id in self._reg_list:
reg_info = self._reg_list
fnkey = reg_info["fnkey"]
vkey = reg_info["vkey"]
func = reg_info["func"]
self.fast_reg(id, (fnkey, vkey), func)
fn = self.callback()
thread_it(fn)# 启动监听热键按下线程
try:
msg = ctypes.wintypes.MSG()
while True:
if self.user32.GetMessageA(ctypes.byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if msg.wParam in self.hkey_list:
self.hkey_flags = True
self.user32.TranslateMessage(ctypes.byref(msg))
self.user32.DispatchMessageA(ctypes.byref(msg))
finally:
for id in self.hkey_list:
self.user32.UnregisterHotKey(None, id)
def thread_it(func, *args):
t = Thread(target=func, args=args)
t.setDaemon(True)
t.start()
'''
调用办法
热键1实现功能
def jump(func, hotkey):
self_id = hotkey.get_id(func)
mybox.displayinfo("本程序有gamelife编制,仅供参考,有问题及时联系。F10停止监测,alt+F10显示本说明!",5)
#while hotkey.get_running_state(self_id):
# print(f"{self_id : } 你正在1秒1次的跳动")
# sleep(1)
热键2实现功能
def stop_jump(start_id, hotkey):
global mark
hotkey.hkey_running = False
print(f"{start_id} 即将停止")
sleep(1)
mark=True
print(f'当前线程列表:{activeCount()}', enumerate())
mybox.displayinfo("程序将停止监测邮件并退出,如需监测邮件请重新运行本程序!!!",5)
os._exit(0)
#调用类hotkey来注册热键
def main():
hotkey = Hotkey()
#start_id = hotkey.reg(key=(win32con.MOD_ALT, win32con.VK_HOME), func=jump, args=(jump, hotkey))# alt home键 开始
#hotkey.reg(key=(0, win32con.VK_END), func=stop_jump, args=(start_id, hotkey))# alt end键 结束
注册热键1 功能模块
start_id = hotkey.reg(key=(win32con.MOD_ALT, win32con.VK_F11), func=jump, args=(jump, hotkey))# alt f11键 开始
注册热键2 功能模块
hotkey.reg(key=(0, win32con.VK_F10), func=stop_jump, args=(start_id, hotkey))# f10键 结束
hotkey.start()# 启动热键主线程
print(f"当前总线程数量:{activeCount()}")
print('当前线程列表:', enumerate())
print('热键注册初始化完毕,尝试按组合键alt+f11 或者单键f10看效果')
''' 本帖最后由 ypcgamelife 于 2021-4-7 23:33 编辑
imcuer 发表于 2021-4-7 23:27
import mymessagebox出错
mymessagebox是我自己汇总的包,需要的话,我可以提供给你. 没有mymessagebox,你用户名,密码直接自己赋值文本也可以。 厉害,学习了 import mymessagebox出错 厉害厉害,加油💪 厉害厉害收藏了 这年头学不会爬虫是硬伤了 挺好,学以致用! cy,慢慢学习
页:
[1]
2