티스토리 뷰

C에서 함수호출하여 파이썬 FTP 전송

사전준비작업 : http://petra.tistory.com/1235

한글폴더이름 사용가능하지만, 한글이름 파일 전송은 실패한다.

<fftp.py 코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from ftplib import FTP
import os
 
def ppftp(sourcePath, destPath, strFileName, strServerAddr, strPort, strID, strPWD):
    ftp = FTP()
    ftp.connect(strServerAddr, int(strPort))
    ftp.login(strID, strPWD)
    ftp.cwd(destPath)
    os.chdir(sourcePath)
    myfile = open(strFileName, 'rb')
    ftp.storbinary('STOR ' + strFileName, myfile)
    myfile.close()
    ftp.close()
 
cs

<MFC 코드>

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//================ 파이썬36
#ifdef _DEBUG
#pragma comment(lib, "Python36/python36_d.lib"// 배포할떄는 문제 없나???
#else
#pragma comment(lib, "Python36/python36.lib")
#endif
 
#include "Python36/Python.h"
//================
 
TCHAR* Append_Script_Path() // 파이썬 모듈을 검색할 폴더에 Script 폴더를 추가한다.
{
    TCHAR szCurrentDir[MAX_PATH] = { 0, };
    TCHAR szScriptDir[MAX_PATH] = { 0, };
    GetCurrentDirectory(MAX_PATH, szCurrentDir);
    //_stprintf_s(szScriptDir, MAX_PATH, _T("%s\\Script"), szCurrentDir);
    _stprintf_s(szScriptDir, MAX_PATH, _T("C:\\Users\\USER\\Documents\\Python"), szCurrentDir);
    PySys_SetPath(szScriptDir);
    return szScriptDir;
 
    /*
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append(\"/path/to/python/module/here\")");
    */
}
 
void DontWriteByteCode(bool bYes)
{
    if (true == bYes)
        PySys_SetObject("dont_write_bytecode", Py_True);
    else
        PySys_SetObject("dont_write_bytecode", Py_False);
}
 
void* CMFCRESTTelegramDlg::문자열코드변환(CString str)
{
    PyObject* pPyObject = NULL;
 
#ifdef _UNICODE
    pPyObject =  PyUnicode_FromUnicode(str, str.GetLength());
#else
    USES_CONVERSION;
    wchar_t* wString = T2W(str.GetBuffer(0));
    pPyObject = PyUnicode_FromUnicode(wString, wcslen(wString));
#endif
 
    return (void*)pPyObject;
}
 
void CMFCRESTTelegramDlg::OnBnClickedButtonFtp()
{
    // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
    PyObject* pModule = NULL;
    PyObject* pArgument = NULL;
    PyObject* pReturn = NULL;
    PyObject* pFtp = NULL;
 
    Py_Initialize(); // Lib 초기화
    //Append_Script_Path();
    DontWriteByteCode(true); // 바이너리코드 저장여부
    pModule = PyImport_ImportModule("fftp"); // 파이썬 로딩
    if (NULL == pModule)
    {
        PyErr_Print();
        AfxMessageBox(TEXT("파이썬 코드 불러오기 실패!!!"));
        Py_Finalize(); // Lib 해제
        return;
    }
    pFtp = PyObject_GetAttrString(pModule, "ppftp"); // 함수 얻기
    if (NULL == pFtp)
    {
        PyErr_Print();
        AfxMessageBox(TEXT("파이썬 함수 찾기 실패!!!"));
        if (NULL != pModule)
            Py_DECREF(pModule); // 파이썬 리소스 해제
        Py_Finalize(); // Lib 해제
        return;
    }
 
    CString strSourcePath = TEXT("C:\\Users\\USER\\Documents\\Python\\새 폴더"); // 한글폴더 사용가능
    CString strDestPath = TEXT(".\\StockData");
    CString strFile = TEXT("test.py"); // 한글 파일명은 전송 안됨.
    CString strServerAddr = TEXT("FTP서버주소");
    CString  strPort = TEXT("21");
    CString strID = TEXT("FTP id");
    CString strPWD = TEXT("FTP 암호");
 
    PyObject* pSourcePath = (PyObject*)문자열코드변환(strSourcePath);
    PyObject* pDestPath = (PyObject*)문자열코드변환(strDestPath);
    PyObject* pFile = (PyObject*)문자열코드변환(strFile);
    PyObject* pServerAddr = (PyObject*)문자열코드변환(strServerAddr);
    PyObject* pPortNo = (PyObject*)문자열코드변환(strPort);
    PyObject* pID = (PyObject*)문자열코드변환(strID);
    PyObject* pPWD = (PyObject*)문자열코드변환(strPWD);
 
    //pArgument = PyTuple_Pack(3, PyUnicode_FromFormat("%s", pszToken), PyUnicode_FromFormat("%s", pszUser), PyUnicode_FromFormat("%s", strMsg)); // 한글깨짐
    pArgument = PyTuple_Pack(7, pSourcePath, pDestPath, pFile, pServerAddr, pPortNo, pID, pPWD);    // MFC 프로젝트가 유니코드/멀티바이트 상관없이 한글 파일명은 안깨진다.
    pReturn = PyObject_CallObject(pFtp, pArgument);
    if (pArgument != NULL)
        Py_DECREF(pArgument);
    pArgument = NULL;
    //pReturn = PyObject_CallObject(pSendSMS, NULL); // 인수 없을때
    if (Py_None != pReturn) // 리턴없을때
    {
        PyErr_Print();
        AfxMessageBox(TEXT("파이썬 함수 호출 실패!!!"));
    }
    if (NULL != pModule)
        Py_DECREF(pModule); // 파이썬 리소스 해제
    if (pArgument != NULL)
        Py_DECREF(pArgument);
    if (pReturn != NULL)
        Py_DECREF(pReturn);
    if (pFtp != NULL)
        Py_DECREF(pFtp);
    Py_Finalize(); // Lib 해제
}
cs

<실행>

 

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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 31