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.
我需要编写一个客户端 - 服务器应用程序。我想用python编写它,因为我熟悉它,但我想知道python代码是否可以从C运行。我打算有两个C项目,一个包含服务器代码,一个包含客户端代码。
Is it possible to eval the python code and run it ? Is there another way of doing this?
是否可以评估python代码并运行它?还有另一种方法吗?
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. ).
底线是python代码必须从C运行,并且它的行为必须与在python解释器下运行完全相同。我现在问这个,因为我不想浪费时间编写python代码,以便稍后发现我无法实现这一点。作为旁注,我只打算使用基本的python模块(socket,select等)。
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 ?
编辑:也许这个编辑是有序的。我之前没有在C中嵌入python,我不知道它的行为是什么。问题是,服务器将有一个选择循环,因此将“永远”运行。 C会让我这样做吗?
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.
EDIT2:这就是我需要这样做的原因。在学校,老师要求我们在C中做一个非常复杂的客户端 - 服务器应用程序。我要作弊,在python中编写代码并将其嵌入到C中。
3 个解决方案
#1
here's a nice tutorial for doing exactly that http://www.linuxjournal.com/article/8497
这是一个很好的教程,正是为了完成http://www.linuxjournal.com/article/8497
#2
It's called embedding Python -- it's well covered in the Python docs. See https://docs.python.org/extending/embedding.html
它被称为嵌入Python - 它在Python文档中有很好的介绍。请参阅https://docs.python.org/extending/embedding.html
See how do i use python libraries in C++?
看看我如何在C ++中使用python库?
#3
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.
是的,您可以通过在程序中嵌入解释器来从C运行Python代码。您可以将部分C代码暴露给Python,并从Python调用暴露的C代码,就好像它们是普通的Python函数一样。
A good start is the Embedding section in the Python docs. Also have a look at the article linked to by cobbal.
一个好的开始是Python文档中的嵌入部分。另请查看cobbal链接的文章。
#1
here's a nice tutorial for doing exactly that http://www.linuxjournal.com/article/8497
这是一个很好的教程,正是为了完成http://www.linuxjournal.com/article/8497
#2
It's called embedding Python -- it's well covered in the Python docs. See https://docs.python.org/extending/embedding.html
它被称为嵌入Python - 它在Python文档中有很好的介绍。请参阅https://docs.python.org/extending/embedding.html
See how do i use python libraries in C++?
看看我如何在C ++中使用python库?
#3
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.
是的,您可以通过在程序中嵌入解释器来从C运行Python代码。您可以将部分C代码暴露给Python,并从Python调用暴露的C代码,就好像它们是普通的Python函数一样。
A good start is the Embedding section in the Python docs. Also have a look at the article linked to by cobbal.
一个好的开始是Python文档中的嵌入部分。另请查看cobbal链接的文章。