tags:

views:

185

answers:

3

I need to write a client-server application. I want to write it in python, because I'm familiar with it, but I would like to know if the python code can be ran from C. I'm planning to have two C projects, one containing the server code, and one containing the client code.

Is it possible to eval the python code and run it ? Is there another way of doing this?

The bottom line is that the python code must run from C, and it must behave exactly as if ran under the python interpreter. I'm asking this now, because I don't want to waste time writing the python code just to find out later that I can't achieve this . As a sidenote, I only plan on using basic python modules ( socket,select, etc. ).

EDIT: maybe this edit is in order. I haven't embedded python in C before, and I don't know what the behaviour will be. The thing is, the server will have a select loop, and will therefore run "forever". Will C let me do that ?

EDIT2: here is why I need to do this. At school, a teacher asked us to do a pretty complex client-server app in C. I'm going to cheat, write the code in python and embed it in C.

+4  A: 

here's a nice tutorial for doing exactly that http://www.linuxjournal.com/article/8497

cobbal
This is a really good article. I would not actually do this in this particular case but nevertheles, you answered it correctly I believe. :)
BobbyShaftoe
+1  A: 

Yes you can run the Python code from C by embedding the interpreter in your program. You can expose portions of your C code to Python and call your exposed C code from Python as if they were normal Python functions.

A good start is the Embedding section in the Python docs. Also have a look at the article linked to by cobbal.

Skurmedel
+2  A: 

It's called embedding Python -- it's well covered in the Python docs. See http://www.python.org/doc/2.5.2/ext/embedding.html

See http://stackoverflow.com/questions/297112/how-do-i-use-python-libraries-in-c

S.Lott