As mentioned in Alex Martelli's answer (although the link in the mailing-list message was broken, it should be http://www.python.org/doc/2.5.1/ext/pure-embedding.html).. The C way of calling..
print urllib.urlopen("http://google.com").read()
- Add the Python.framework to your project (Right click
External Frameworks..
, Add > Existing Frameworks
. The framework in in /System/Library/Frameworks/
- Add
/System/Library/Frameworks/Python.framework/Headers
to your "Header Search Path" (Project > Edit Project Settings
)
The following code should work (although it's probably not the best code ever written..)
#include <Python.h>
int main(){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Py_Initialize();
// import urllib
PyObject *mymodule = PyImport_Import(PyString_FromString("urllib"));
// thefunc = urllib.urlopen
PyObject *thefunc = PyObject_GetAttrString(mymodule, "urlopen");
// if callable(thefunc):
if(thefunc && PyCallable_Check(thefunc)){
// theargs = ()
PyObject *theargs = PyTuple_New(1);
// theargs[0] = "http://google.com"
PyTuple_SetItem(theargs, 0, PyString_FromString("http://google.com"));
// f = thefunc.__call__(*theargs)
PyObject *f = PyObject_CallObject(thefunc, theargs);
// read = f.read
PyObject *read = PyObject_GetAttrString(f, "read");
// result = read.__call__()
PyObject *result = PyObject_CallObject(read, NULL);
if(result != NULL){
// print result
printf("Result of call: %s", PyString_AsString(result));
}
}
[pool release];
}
Also this tutorial is good