浅要分析Python程序与C程序的结合使用
Python 是一种用于快速开发软件的编程语言,它的语法比较简单,易于掌握,但存在执行速度慢的问题,并且在处理某些问题时存在不足,如对计算机硬件系统的访问,对媒体文件的访问等。而作为软件开发的传统编程语言 C 语言,却能在这些问题上很好地弥补 Python 语言的不足。因此,本文通过实例研究如何在 Python 程序中整合既有的 C 语言模块,包括用 C 语言编写的源程序和动态链接库等,从而充分发挥 Python 语言和 C 语言各自的优势。
概览
背景知识介绍
Python 语言的特点
Python 作为一门程序开发语言,被越来越多地运用到快速程序开发。Python 是一种解释型的,互动的,面向对象的编程语言,它包含了模块化的操作,异常处理,动态资料形态,以及类型的使用。它的语法表达优美易读,具有很多优秀的脚本语言的特点:解释的,面向对象的,内建的高级数据结构,支持模块和包,支持多种平台,可扩展。而且它还支持交互式方式运行,图形方式运行。它拥有众多的编程界面支持各种操作系统平台以及众多的各类函数库,利用 C 和 C++ 可以对它进行扩充。
C 语言的特点
C 语言作为最受人们欢迎的语言之一,有广泛的发展基础。简洁紧凑、灵活方便,功能强大是其特点。另外,C 语言是一门中级语言。它把高级语言的基本结构和语句与低级语言的实用性结合起来。由于可以直接访问物理地址,可以方便的对硬件进行操作。因此,很多的系统软件都是由 C 语言编写。
Python 语言与 C 语言的交互
为了节省软件开发成本,软件开发人员希望能够缩短的软件的开发时间,希望能够在短时间内开发出稳定的产品。Python 功能强大,简单易用,能够快速开发应用软件。但是由于 Python 自身执行速度的局限性,对性能要求比较高的模块需要使用效率更高的程序语言进行开发,例如 C 语言,系统的其他模块运用 Python 进行快速开发,最后将 C 语言开发的模块与 Python 开发的模块进行整合。在此背景下,基于 Python 语言与 C 语言的各自特点,用 C 语言来扩展现有的 Python 程序,显得很有意义。本文首先介绍几种常用的整合 Python 程序与 C 语言程序的方法,最后给出相应的实例。
利用 ctypes 模块整合 Python 程序和 C 程序
ctypes 模块
ctypes 是 Python 的一个标准模块,它包含在 Python2.3 及以上的版本里。ctypes 是一个 Python 的高级外部函数接口,它使得 Python 程序可以调用 C 语言编译的静态链接库和动态链接库。运用 ctypes 模块,能够在 Python 源程序中创建,访问和操作简单的或复杂的 C 语言数据类型。最为重要的是 ctypes 模块能够在多个平台上工作,包括 Windows,Windows CE,Mac OS X,Linux,Solaris,FreeBSD,OpenBSD。
接下来通过几个简单的例子来看一下 ctypes 模块如何整合 Python 程序和 C 程序。
源代码层面上的整合
利用 Python 本身提供的 ctypes 模块可以使 Python 语言和 C 语言在源代码层面上进行整合。本节介绍了如何通过使用 ctypes 库,在 Python 程序中可以定义类似 C 语言的变量。
下表列出了 ctypes 变量类型,C 语言变量类型和 Python 语言变量类型之间的关系:
表 1. ctypes,c 语言和 Python 语言变量类型关系
表 1 中的第一列是在 ctypes 库中定义的变量类型,第二列是 C 语言定义的变量类型,第三列是 Python 语言在不使用 ctypes 时定义的变量类型。
举例:
清单 1. ctypes 简单使用
>>> from ctypes import * # 导入 ctypes 库中所有模块 >>> i = c_int(45) # 定义一个 int 型变量,值为 45 >>> i.value # 打印变量的值 45 >>> i.value = 56 # 改变该变量的值为 56 >>> i.value # 打印变量的新值 56
从下面的例子可以更明显地看出 ctypes 里的变量类型和 C 语言变量类型的相似性:
清单 2. ctypes 使用 C 语言变量
>>> p = create_string_buffer(10) # 定义一个可变字符串变量,长度为 10 >>> p.raw # 初始值是全 0,即 C 语言中的字符串结束符' \0 ' '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' >>> p.value = "Student" # 字符串赋值 >>> p.raw # 后三个字符仍是' \0 ' 'Student\x00\x00\x00' >>> p.value = "Big" # 再次赋值 >>> p.raw # 只有前三个字符被修改,第四个字符被修改为' \0 ' 'Big\x00ent\x00\x00\x00'
下面例子说明了指针操作:
清单 3. ctypes 使用 C 语言指针
>>> i = c_int(999) # 定义 int 类型变量 i,值为 999 >>> pi = pointer(i) # 定义指针,指向变量 i >>> pi.contents # 打印指针所指的内容 c_long(999) >>> pi.contents = c_long(1000) # 通过指针改变变量 i 的值 >>> pi.contents # 打印指针所指的内容 c_long(1000)
下面例子说明了结构和数组的操作:
清单 4. ctypes 使用 C 语言数组和结构体
>>> class POINT(Structure): # 定义一个结构,内含两个成员变量 x,y,均为 int 型 ... _fields_ = [("x", c_int), ... ("y", c_int)] ... >>> point = POINT(2,5) # 定义一个 POINT 类型的变量,初始值为 x=2, y=5 >>> print point.x, point.y # 打印变量 2 5 >>> point = POINT(y=5) # 重新定义一个 POINT 类型变量,x 取默认值 >>> print point.x, point.y # 打印变量 0 5 >>> POINT_ARRAY = POINT * 3 # 定义 POINT_ARRAY 为 POINT 的数组类型 # 定义一个 POINT 数组,内含三个 POINT 变量 >>> pa = POINT_ARRAY(POINT(7, 7), POINT(8, 8), POINT(9, 9)) >>> for p in pa: print p.x, p.y # 打印 POINT 数组中每个成员的值 ... 7 7 8 8 9 9
Python 访问 C 语言 dll
通过 ctypes 模块,Python 程序可以访问 C 语言编译的 dll,本节通过一个简单的例子,Python 程序 helloworld.py 中调用 some.dll 中的 helloworld 函数,来介绍 Python 程序如何调用 windows 平台上的 dll。
导入动态链接库
清单 5. ctypes 导入 dll
from ctypes import windll # 首先导入 ctypes 模块的 windll 子模块 somelibc = windll.LoadLibrary(some.dll) # 使用 windll 模块的 LoadLibrary 导入动态链接库
访问动态链接库中的函数
清单 6. ctypes 使用 dll 中的函数
somelibc. helloworld() # 这样就可以得到 some.dll 的 helloworld 的返回值。
整个 helloworld.py 是这样的:
清单 7. Python hellpworld 代码
from ctypes import windll def callc(): # load the some.dll somelibc = windll.LoadLibrary(some.dll) print somelibc. helloworld() if __name__== “__main__”: callc()
在命令行运行 helloworld.py,在 console 上可以看到 some.dll 中 helloworld 的输出。
清单 8. Python hellpworld Windows command console 运行输出
C:\>python C:\python\test\helloworld.py Hello World! Just a simple test.
Python 调用 C 语言 so
通过 ctypes 模块,Python 程序也可以访问 C 语言编译的 so 文件。与 Python 调用 C 的 dll 的方法基本相同,本节通过一个简单的例子,Python 程序 helloworld.py 中调用 some.so 中的 helloworld 函数,来介绍 Python 程序如何调用 linux 平台上的 so。
导入动态链接库
清单 9. ctypes 导入 so
from ctypes import cdll # 首先导入 ctypes 模块的 cdll 子模块,注意 linux 平台上使用 cdll 的,而不是 windll。 somelibc = cdll.LoadLibrary(“./some.so”) # 使用 cdll 模块的 LoadLibrary 导入动态链接库
访问动态链接库中的函数
清单 10. ctypes 使用 so 中的函数
somelibc. helloworld() # 使用方法与 windows 平台上是一样的。
整个 helloworld.py 是这样的:
清单 11. Python helloworld 代码
from ctypes import cdll def callc(): # load the some.so somelibc = cdll.LoadLibrary(some.so) print somelibc. helloworld() if __name__== “__main__”: callc()
在命令行运行 helloworld.py,在 linux 标准输出上可以看到 some.so 中 helloworld 的输出。
清单 12. Python hellpworld Linux shell 运行输出
[root@linux-790t] python ./helloworld.py Hello World! Just a simple test.
Python 程序和 C 程序整合实例
以下我们举例用 Python 来实现一个小工具,用来实现 hash 算法,查看文件的校验和(MD5,CRC,SHA1 等等)。通过查看文件的校验和,可以知道文件在传输过程中是否被破坏或篡改。
Hash,一般翻译做“散列”,也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。
由于相对 C 语言来说,Python 的运行效率较低,因此我们的 Python 小工具利用一个已有的 C 语言的动态链接库 (hashtcalc.dll) 来实现我们的程序。本例中,我们运用 wxPython 编写简单的 GUI 界面,通过 python 调用 hashtcalc.dll 的接口计算文件的校验和,然后输出在界面上。
架构图
图 1. 工具的架构图
hashcalc.dll 接口描述
函数名:calc_CRC32
函数:char* calc_CRC32(char *filename);
参数:文件名
返回值:字符串
说明:该函数对输入的文件内容进行计算,并且返回它的 CRC32
函数名:calc_MD5
函数:char* calc_MD5(char *filename);
参数:文件名
返回值:字符串
说明:该函数对输入的文件内容进行计算,并且返回它的 MD5
函数名:calc_SHA1
函数:char* calc_SHA1 (char *filename);
参数:文件名
返回值:字符串
说明:该函数对输入的文件内容进行计算,并且返回它的 SHA1
HashcalcAdapter 代码
HashcalcAdapter.py 实现了一个 python 的 class HashcalcAdapter,HashcalcAdapter 对 hashtcalc.dl 的 C 语言接口进行了封装,使得其他 python 模块可以直接通过 HashcalcAdapter 使用 hashtcalc.dll 中实现的 hash 算法。具体的代码如下:
清单 13. HashcalcAdapter.py 代码
from ctypes import windll from ctypes import * class HashcalcAdapter(object): def __init__(self, dllpath): self._dllpath = dllpath self._libc = windll.LoadLibrary(self._dllpath) def calc_CRC32(self, filename): new_filename = c_char_p(filename) return self._libc.calc_CRC32(new_filename) def calc_MD5(self, filename): new_filename = c_char_p(filename) return self._libc.calc_MD5(new_filename) def calc_SHA1(self, filename): new_filename = c_char_p(filename) return self._libc.calc_SHA1(new_filename)
运行界面
图 2. 工具的运行界面

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
