Python Forum
Error "an integer is required" in getpass.getuser(). - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error "an integer is required" in getpass.getuser(). (/thread-2250.html)



Error "an integer is required" in getpass.getuser(). - Nirelg - Mar-02-2017

Hi all,

I am trying to write a simple "Keylogger" code using python.
I am really on the beginning of the code, yet I found a weird error.
my code:

import getpass
import pyHook
import pythoncom

def a(event):
   try:
       print getpass.getuser()
   except Exception as error:
       print error


hm = pyHook.HookManager()
hm.KeyDown = a
hm.HookKeyboard()
pythoncom.PumpMessages()
my code suppose (as much as i know) to write my user name every time i press a character on my keyboard, yet the output is:

Output:
Nirel an integer is required an integer is required an integer is required
As you can see, the first time I pressed a button, the code gave me the correct output of: Nirel (my computer name), yet every key I press since then I got an error of: "an integer is required".
I tried searching online yet didn't find anyone that had the same error as me, also tried reading the documentation of getpass, but didn't find anything useful regarding my error.


more information:
I am running this code on : pycharm
python interpreter             : 2.7
operating system              :windows 10


When i tried to add a few rows to my code:
import getpass
import pythoncom
import pyHook


def a(event):
   if a.b == None:
       a.b = event.WindowName
   if event.WindowName != a.b:
       try:
           print getpass.getuser()
           a.b = event.WindowName

       except Exception as error:
           print error

a.b = None
a.userinput = ""
hm = pyHook.HookManager()
hm.KeyDown = a
hm.HookKeyboard()
pythoncom.PumpMessages()
In this case i wanted to print my computer name each time i switch tabs on my browser, yet the function getpass.getuser() didn't work even a single time:

Output:
an integer is required an integer is required an integer is required
If anyone knows why this bug is happening and can help me it would be highly appreciated.

Thanks in advance,
Nirel.


RE: Error "an integer is required" in getpass.getuser(). - Ofnuts - Mar-02-2017

Try adding a return True to your function.


RE: Error "an integer is required" in getpass.getuser(). - Nirelg - Mar-02-2017

(Mar-02-2017, 08:44 AM)Ofnuts Wrote: Try adding a return True to your function.

Firstly, thanks for answering.
Secondly, I did what you suggested, yet now the outcome even weirder for me.

python code:
import getpass
import pythoncom
import pyHook


def a(event):
   if a.b == None:
       a.b = event.WindowName
   if event.WindowName != a.b:
       try:
           print getpass.getuser()
           a.b = event.WindowName

       except Exception as error:
           print error
           return True

a.b = None
a.userinput = ""
hm = pyHook.HookManager()
hm.KeyDown = a
hm.HookKeyboard()
pythoncom.PumpMessages()
Output:
an integer is required Nirel an integer is required Nirel an integer is required Nirel
As you can see, the first time I switched a tab and wrote a letter the error "an integer is required" still accrued,
yet when i switched a tab and wrote a letter it gave  me the right answer of "Nirel",
then, when I switched a tab this went on again, firstly the error, then it worked fine.

I still don't understand why the error is accruing?
Also can you please tell me why did the "return True" changed the outcome and made it work half the time? (why it didn't need the "integer is required" now when I used the "return True" line?

Thanks in advance :)


RE: Error "an integer is required" in getpass.getuser(). - Ofnuts - Mar-03-2017

Not an expert on the matter. Saw some sample code around the same API and they returned true on the event. Not uncommon as a convention to signal that the event was actually processed and that there is no need to process it further.


RE: Error "an integer is required" in getpass.getuser(). - Nirelg - Mar-04-2017

Well, I still haven't found a way to really fix the issue.
Using return True only partially fix my problem, leaving the first time I change a tab with an error,
making me unable of getting the first try code.


RE: Error "an integer is required" in getpass.getuser(). - nilamo - Mar-20-2017

You're still not returning, though. It's indented in the except block. Un-indent it, so you return True always, every time the function is called.