python和C获取cpu 序列号

17 篇文章 2 订阅

C语言获取 cpu序列

get_cpu_info.c

    #include <stdio.h>       
   void cpu_id(char *id)  
    {  
        char s[17];  
        unsigned   int   s1,s2;
        char   sel;     
        asm volatile     
        ( "movl $0x01 , %%eax ; \n\t"  
        "xorl %%edx , %%edx ;\n\t"  
        "cpuid ;\n\t"   
        "movl %%edx ,%0 ;\n\t"  
        "movl %%eax ,%1 ; \n\t"  
        :"=m"(s1),"=m"(s2)  
        );        
        sprintf(s,"%08X%08X",s1,s2);  
        for(int i=7;i>=0;i--)
        {
            id[2*(7-i)] = s[i*2];
            id[2*(7-i)+1] = s[i*2+1];
        }
        id[16] = '\0'; 
    }  
    int   main(int   argc,   char*   argv[])     
    {     
        char id[17];  
        cpu_id(id);  
        printf("%s\n",id);  
        return   0;     
    }    

python没找到方便的获取CPU 序列号的方式,所以采用调用C库的方式实现

执行gcc -shared -Wl,-soname,cpu_info -o cpu_info.so -fPIC get_cpu_info.c,将上面的c语言转成so文件

在python中执行
 

from ctypes import *

buf = create_string_buffer('hello'.encode('gbk'),17)

cpu = CDLL("./cpu_info.so")

cpu.cpu_id(buf)

print(buf.value)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值