How can I make a C program and a Python Program to talk to each other?
-
I have a C code in file1.c and a python code in http://file2.py Now I want to communicate each other. what is the simplest way and what is the best way. please suggest both the ways. PS: I heared about Cython. Is it simplest way or best way. Please tell the method in which i can communicate from either file. and one more thing. how can i write a C code in python file(*.py) so that when i compile that python file it compiles without error and ouput the desire result and same for vice-versa (I mean A python code in C file) If possible?
-
Answer:
Programs communicate via what we call: http://en.wikipedia.org/wiki/Inter-process_communication. Sockets sound like a good idea, for example. Code doesn't 'communicate'. Code is static files, text files, that needs to be compiled. Therefore, to address things from one 'file' to another, you need a compiler that can handle both, or you need two compilers. Python probably has some ways to invoke C code, I don't know if there are easy ways to invoke python programs from C. If you want to have code from a language embedded in code of another language, it's not a good idea, usually. Even if you have a compiler that does that , the code becomes unreadable and you will want to isolate it anyway.
Dorin LazÄr at Quora Visit the source
Other answers
First of all you need to create a "shared library file" in *Nix case it's identified by extension ".so" if you are using GCC then create a shared library for you C code. Example: //file1.c #include<stdio.h> void hello(const char *name) { printf("\nHello......%s",name); } --------------------------------------------------------- To make it shared library, $ gcc -c -Wall file1.c $ gcc -shared -o libfile.so file1.o --------------------------------------------------------- Now, open up your python shell, go to directory where shared file resides from ctypes import cdll sh_obj=cdll.LoadLibrary('./Page on Libfile') #NOTE "./" sh_obj.hello('Python from C') #C function name, with parameters You done it. Answer to second question: To write a C code in Python No Idea. But you can do some Python stuff in C using Python Development library[3]. For references: [1] http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html [2] http://docs.python.org/2/library/ctypes.html [3] http://docs.python.org/2/extending/extending.html
Akash Shende
You need to mention what kind of communication you are trying to achieve. If you want to make use of C functions in Python, try Swig ( http://www.swig.org/ ). You can try a client-server mechanism, where a client gives requests for the server to execute. ( You can start a web server in python in a jiffy; http://web.py , tornado etc.; Simple curl commands can be requests ) If its just going to be some data exchange and you want to do it without much ado; try using a third-party server like redis; everything is gonna be in memory and their respective language clients can get/put data ( so should be pretty fast )
Anonymous
Related Q & A:
- How can I make a myspace comment into a picture caption?Best solution by Yahoo! Answers
- How can I make a hole through a stone or crystal?Best solution by Yahoo! Answers
- How can I design a workout program for myself?Best solution by Yahoo! Answers
- How can I make a phone call to a cruise ship?Best solution by traveltips.usatoday.com
- How can I make a good clay for a school project?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.