티스토리 뷰

반응형

키움증권 API 사용시 로그인을 수동으로 해야 되는 불편함이 있습니다.

이는 보안상의 문제를 방지하기 위한다고 하는데, API로 개발하는 입장에서는 사용자 개입 없이 자동으로 거래되는 시스템을 만들려고 하는 목적도 있기 때문에 수동 로그인은 참 불편한 사항입니다.

 

그래서 자동 로그인 기능을 만들었습니다.

아이디와 비밀번호를 소스에 심어야 하기 때문에, 키움에서 자동 로그인을 지원하는 것보다 더 보안이 취약해 진다는 문제는 있습니다. 비밀번호를 암호화 하는 등의 추가 노력이 필요합니다.

 

아래 소스중 자신의 화면 크기에 따라 로그인 창의 입력란 좌표를 확인하여 수정해 주어야 합니다.

  • pyautogui.moveTo(1335, 685)
  • pyautogui.moveTo(1250, 650)

파이참에서 디버그 모드, 일반 모드, CMD에서 실행할 때 약간식 특성이 있어서 동작하지 않을 수 있으니 sleep() 값도 적당히 수정해 주세요.

 

파일명 : AutoLogin.py
import sys
import time
import ctypes
import pyautogui

import threading as tr


class AutoLogin:
    def __init__(self, user_pass, account_pass) :
        self.user_pass = user_pass
        self.account_pass = account_pass
        self.repeat_count = 0
        self.user_login = False
        self.account_login = False

    def run(self):
        self.enter_user_password()

    def enter_user_password(self):
        if not self.user_login:
            hwnd = ctypes.windll.user32.FindWindowW(None, "영웅문W Login")
            if hwnd == 0:
                self.repeat_count += 1
                if self.repeat_count < 180:
                    tr.Timer(1, self.enter_user_password).start()
                else:
                    exit()
                return

            time.sleep(2)
            ctypes.windll.user32.SetForegroundWindow(hwnd)
            time.sleep(1)

            pyautogui.moveTo(1335, 685)
            pyautogui.click()
            # time.sleep(0.1)
            pyautogui.write(self.user_pass)
            pyautogui.press('enter')

            self.user_login = True
            self.repeat_count = 0
            self.enter_account_password()

    def enter_account_password(self):
        if not self.account_login:
            hwnd = ctypes.windll.user32.FindWindowW(None, "계좌번호관리")
            if hwnd == 0:
                self.repeat_count += 1
                if self.repeat_count < 180:
                    tr.Timer(1, self.enter_account_password).start()
                else:
                    exit()
                return

            time.sleep(0.1)
            pyautogui.moveTo(1250, 650)
            pyautogui.click()
            # time.sleep(0.1)
            pyautogui.write(self.account_pass)
            pyautogui.press('enter')
            time.sleep(0.1)

            ctypes.windll.user32.PostMessageA(hwnd, 0x0010, 0, 0)

            self.account_login = True
            exit()


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("비밀번호를 입력해 주세요.")
        exit(0)

    user_pass, account_pass = sys.argv[1], sys.argv[2]
    auto = AutoLogin(user_pass, account_pass)
    auto.run()

 

위의 AutoLogin.py 호출해 주는 부분입니다.

subprocess.Popen() 를 이용하여 호출해 줘야 백그라운드 실행이 됩니다.

일반 파이썬 함수 호출하듯이 하면 동작하지 않습니다.

로그인창(ShowAccountWindow)을 띄우기 전에 호출을 해줘야 백그라운드 프로그램이 로그인 창을 감지하고 비밀번호를 자동 입력해 줍니다.

self.logging('Opened Login Pannel')

subprocess.Popen(["python", "AutoLogin.py", self.sLoginPwd, self.sAccPwd])
            
self.kiwoom.CommConnect()

self.kiwoom.dynamicCall("GetCommonFunc(QString, QString", "ShowAccountWindow", "")

 

※ pyautogui 특성상 화면이 잠긴 상태에서는 동작하지 않습니다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함