加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

platform模块和ctypes模块

发布时间:2020-12-14 00:39:07 所属栏目:Linux 来源:网络整理
导读:一、ctypes模块 Python 的 ctypes 要使用 C 函数,需要先将 C 编译成动态链接库的形式,即 Windows 下的 .dll 文件,或者 Linux 下的 .so 文件。先来看一下 ctypes 怎么使用 C 标准库。 Windows 系统下的 C 标准库动态链接文件为 msvcrt.dll (一般在目录 C:

一、ctypes模块

Python 的 ctypes 要使用 C 函数,需要先将 C 编译成动态链接库的形式,即 Windows 下的 .dll 文件,或者 Linux 下的 .so 文件。先来看一下 ctypes 怎么使用 C 标准库。

Windows 系统下的 C 标准库动态链接文件为 msvcrt.dll (一般在目录 C:WindowsSystem32 和 C:WindowsSysWOW64 下分别对应 32-bit 和 64-bit,使用时不用刻意区分,Python 会选择合适的)

Linux 系统下的 C 标准库动态链接文件为 libc.so.6 (以 64-bit Ubuntu 系统为例, 在目录 /lib/x86_64-linux-gnu 下)

代码:

#例如,以下代码片段导入 C 标准库,并使用 printf 函数打印一条消息
import platform
from ctypes import *

if platform.system() == Windows:
    libc = cdll.LoadLibrary(msvcrt.dll)
    # libc = windll.LoadLibrary(‘msvcrt.dll‘)
elif platform.system() == Linux:
    libc = cdll.LoadLibrary(libc.so.6)

string=Hello ctypes!n
libc.printf(string.encode("utf-8"))

二、platform模块

该模块用来访问平台相关属性。

常见属性和方法

# 返回平台架构
print(platform.machine())# AMD64

# 获取网络名称
print(platform.node())# DESKTOP-NMIUQ2D

# 获取系统版本
print(platform.platform())# Windows-10-10.0.17763-SP0

# 获取处理器名称
print(platform.processor())# Intel64 Family 6 Model 142 Stepping 10,GenuineIntel

# 获取系统名称
print(platform.system())# Windows

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读