CallBack

Ramkumar Nagabhushanam ramkumar at aapt.net.au
Sat Mar 4 23:54:42 CET 2006


Hello All,
I am trying to integrate the ftpclient (in ftplib) with an application that
has been written I C/C++.
I am able to delete,send change working directories etc. When I receive data
back from the server i.e. Retrieve files, get a directory listing) I want to
pass a callback function to the ftpclient so that the output does not get
displayed on stdout. The code I have written is as shown below. Can anyone
out there give me advise on how to write "c_function" that is called in
ftpTest::DoesFileExist ?Any help will be most appreciated. The code runs
under windows and is compiled with VC++ 6.0. THe code is as shown below.
Regards,
Ram
 
>>>>>>>>>>>>>>>>>>> CODE START >>>>>>>>>>>>>>>>>>>
// test.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <direct.h>
#include "C:\Python24\include\Python.h"
 
Class ftpTest
{
Private:
 PyObject *_FTP;
 PyObject *_callBack;
 
Public:
 FtpTest (const char * ipAddress, const char * username, const char *
password); // Open the FTP connection, Initialize python interpreter
 ~ftpTest (); // Close connection and finalize python interpreter
 Int Close ();
 Int GetFile (const char * fileName);
 Int PutFile (const char * fileName);
 Int ChangeDir (const char * dirName);
 Int DoesFileExist (const char * fileName);
 Int DeleteFile (const char * fileName);
 Int ChangePermissions (const char * fileName, const char * permissions);
};
 
FtpTest::ftpTest (const char * ipAddress, const char * username, const char
* password)
{
 PyObject *modname, *mod, *func, *mdict, *pArgs, *pValue;
 
 // Initialize the python interpreter
 Py_Initialize();
 
 Modname = PyString_FromString("ftplib");
 
 If (modname == NULL)
 {
  // Log an error
 }
 Else
 {
  Mod = PyImport_Import (modname);
 }
 
 If (mod)
 {
  Mdict = PyModule_GetDict (mod);
  Func = PyDict_GetItemString (mdict, "FTP");
  If (func)
  {
   Printf ("I am here -- 1\n");
  }
 
  Const char *arguments[] = {ipAddress, username, password};
 
  PArgs = PyTuple_New (3);
        For (int I = 0; I < 3; ++I) 
  {
            PValue = PyString_FromString(arguments[I]);
            If (!pValue) 
   {
                Py_DECREF(pArgs);
                Py_DECREF(mod);
            }
            /* pValue reference stolen here: */
            PyTuple_SetItem(pArgs, I, pValue);
        }
 
  _FTP = PyObject_CallObject (func, pArgs);
 
  If (_FTP != NULL)
  {
   Printf ("I am here -- 2\n");
  }
 }
}
 
FtpTest::~ftpTest ()
{
 //Py_Finalize();
}
 
Int ftpTest::Close ()
{
 Return (1);
}
 
Int ftpTest::ChangeDir (const char * dirName)
{
 PyObject *t = PyObject_CallMethod (_FTP, "cwd", "(S)", dirName);
 Return (1);
}
 
Int ftpTest::DeleteFile (const char * fileName)
{
 PyObject *t = PyObject_CallMethod (_FTP, "delete", "(S)", fileName);
 Return (1);
}
 
Int ftpTest::PutFile (const char * fileName)
{
 PyObject * py_file = PyFile_FromString ((char *)fileName, "rb"); 
 Std::string S("STOR ");
 s += fileName;
 PyObject * t = PyObject_CallMethod (_FTP, "storlines", "(sO)", S.c_str(),
py_file);
 Return (1);
}
 
Int ftpTest::ChangePermissions (const char * fileName, const char *
permissions)
{
 Std::string S("SITE");
 s += " chmod ";
 s += permissions;
 s += " ";
 s += fileName;
 PyObject * t = PyObject_CallMethod (_FTP, "sendcmd", "(S)", S.c_str());
 Return (1);
}
 
Int ftpTest::GetFile (const char * fileName)
{
// TODO: Implement callback
#if 0
 PyObject *t = PyObject_CallMethod (_FTP, "retrlines", "(S)", "RETR ram.txt
);
#endif
 Return (1);
}
 
PyObject * c_function(char * data)
{
 // Process the data received in "data"
 Return (NULL);
}
 
Int ftpTest::DoesFileExist (const char * fileName)
{
 // Just get the list
 PyObject *t = PyObject_CallMethod (_FTP, "retrlines", "(sO)", "LIST",
c_function);
 If (t == NULL)
 {
  Printf ("I am here -- 4\n");
 }
 Return (1);
}
 
Int main ()
{
 FtpTest ftpClient("127.0.0.1", "xxx", "yyyy");
 FtpClient.DoesFileExist(NULL);
 Return (1);
}

>>>>>>>>>>>>>>>>>>> CODE END >>>>>>>>>>>>>>>>>>>>>



More information about the Python-list mailing list