Python Forum
Error "an integer is required" in getpass.getuser().
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error "an integer is required" in getpass.getuser().
#1
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.
Reply
#2
Try adding a return True to your function.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
(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 :)
Reply
#4
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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 721 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  error : "Microsoft Visual C++ 14.0 is required. " Even its installed Barak 4 3,924 Oct-13-2021, 10:39 PM
Last Post: Underscore
  getpass.getpass error Juice1277 3 2,677 Jan-21-2021, 10:08 PM
Last Post: Juice1277
  getpass ComputerAstronaut 1 1,565 Nov-16-2020, 01:16 AM
Last Post: bowlofred
  how to solve "a bytes-like object is required, not 'str'" error maiya 2 3,730 Jul-28-2020, 07:03 PM
Last Post: bowlofred
  Why is this an error? (string and integer) pcsailor 4 3,428 Feb-05-2020, 08:26 AM
Last Post: DeaD_EyE
  authentication required error with urlopen Ranjirock 2 2,697 Sep-11-2019, 01:01 PM
Last Post: Ranjirock
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 21,687 Jul-13-2019, 10:21 AM
Last Post: jaycuff13
  Error when entering letter/character instead of number/integer helplessnoobb 2 6,983 Jun-22-2019, 07:15 AM
Last Post: ThomasL
  missing 1 required positional argument error nikos 3 7,392 Feb-28-2019, 12:53 PM
Last Post: nikos

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020