zoukankan      html  css  js  c++  java
  • platform模块和ctypes模块

    一、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!
    '
    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
  • 相关阅读:
    centos7 使用postgres
    centos7 Authentication failure
    centos 安装 jdk PostgreSQL
    JS遍历对象或者数组
    PHP简单 对象(object) 与 数组(array) 的转换
    IDEA 配置Tomcat 跑Jeecg项目
    kettle 安装mysql 驱动
    : Could not open a connection to your authentication agent
    java_Ninja实战过程
    设备判断
  • 原文地址:https://www.cnblogs.com/angelyan/p/11504990.html
Copyright © 2011-2022 走看看