//네이버

업비트에서 key를 발급 받았으니(안했다면 참고: https://sonjeol-kim.tistory.com/9)

로그인모듈을 만들어보았다.

 

 

프로젝트 트리 구조

 

기본적인  pyupbit 패키지에서 코드는 아래와 같다.

(참조: Pyupbit 제작자분들이 만든 가이드 https://pyupbit.readthedocs.io/en/latest/exchange.html#id1)

import pyupbit

access = "access key"
secret = "secret key"
upbit = Upbit(access, secret)

이후 아래와 같이 함수들을 이용할 수 있다.

import pyupbit

access = "access key"
secret = "secret key"
upbit = Upbit(access, secret)

balance = upbit.get_balances()  #잔고조회
print(balance)

 

하지만 main파일에서 진행하는게 아닌, 별도의 로그인.py를 만들게되면 

각기 다른 .py파일에서 매번 선언하게 될 것 같아서 class를 이용하여 아래와 같이 만들었다.

 

 

 

Login.py파일의 최종 코드는 아래와 같다.

import settings.keys
import pyupbit


class Authenticator(object):
    _instance = None

    @classmethod
    def get_instance(cls): 
        if cls._instance is None:
            cls._instance = cls()

        return cls._instance

    def __init__(self):
        access = settings.keys.access_key
        secret = settings.keys.secret_key
        self.upbit = pyupbit.Upbit(access, secret)

 

_instance라는 클래스 변수를 선언하여 초기에 None을 넣은 뒤

클래스 메소드가 호출되면 None일때만 실행하고 아니면 그냥 리턴하는 구조이다.

 

 

현재 메인 파일에선 아래와 같이 불러서 쓰도록 해두었다.

import login
import settings.list

if __name__ == '__main__':

    auth = login.Authenticator.get_instance().upbit

 

 

 

 

 

 

 

 

 

 

+ Recent posts