#title Python Tip & Tech
[[TableOfContents]]

==== pip install시 SSLCertVerificationError ====
{{{
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1108)'))
}}}

{{{
pip --trusted-host pypi.org --trusted-host files.pythonhosted.org install
}}}

{{{
pip install <package> --trusted-host pypi.org --trusted-host files.pythonhosted.org --proxy="<IP>:<port>"
}}}

https://aileen93.tistory.com/72

==== 한글 깨짐 ====
{{{
from matplotlib import font_manager, rc
font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name()
rc('font', family=font_name)
matplotlib.rcParams['axes.unicode_minus'] = False
}}}

==== sftp에서 파일 다운로드 ====
1개 파일만..
{{{
import paramiko
from base64 import decodebytes
host = "192.168.100.100"
port = 22
id = "id"
pw = "pw"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()

client.connect(host, username=id, password=pw, port=port, timeout=14400) #큰 파일의 경우 timeout을 세팅하지 않으면 금방 끊어진다.
tr = client.get_transport()
#tr.default_max_packet_size = 214748364 #이것을 빼야 한다.
tr.default_window_size = 2147483647 #이것을 세팅하지 않으면 너무 느리다 초당 10kb
paramiko.sftp_file.SFTPFile.MAX_REQUEST_SIZE = pow(2, 22) #이것을 세팅하지 않으면 소켓이 끊어진다.
sftp = client.open_sftp()


remonte_file_path = '/remote_dir/sample.log.gz'
local_file_path = '/local_dir/sample.log.gz'

sftp.get(remonte_file_path, local_file_path)
client.close()
}}}

디렉토리에 있는거 모두 다운로드
{{{
import os
import sys
import paramiko
from stat import S_ISDIR

host = "192.168.100.100"
port = 22
id = "id"
pw = "pw"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()

client.connect(host, username=id, password=pw, port=port, timeout=14400) #timeout을 세팅하지 않으면 금방 끊어진다.
tr = client.get_transport()
#tr.default_max_packet_size = 214748364 #이것을 빼야 한다.
tr.default_window_size = 2147483647 #이것을 세팅하지 않으면 너무 느리다 초당 10kb
paramiko.sftp_file.SFTPFile.MAX_REQUEST_SIZE = pow(2, 22) #이것을 세팅하지 않으면 소켓이 끊어진다.
sftp = client.open_sftp()

remonte_file_path = sys.argv[1] #/remote_dir/
local_file_path = sys.argv[2] #/local_dir/


def sftp_walk(remotepath):
    path=remotepath
    files=[]
    folders=[]
    for f in sftp.listdir_attr(remotepath):
        if S_ISDIR(f.st_mode):
            folders.append(f.filename)
        else:
            files.append(f.filename)
    if files:
        yield path, files
    for folder in folders:
        new_path=os.path.join(remotepath,folder)
        for x in sftp_walk(new_path):
            yield x

os.system("mkdir " + local_file_path)
for path, files in sftp_walk(remonte_file_path):
	for file in files:
		print(os.path.join(os.path.join(path,file)))
		sftp.get(os.path.join(os.path.join(path,file)), local_file_path+file)

client.close()
}}}