A way to get MAC addresses under Python windows

Source: Internet
Author: User
Tags uuid

I recently had a project that uses Python to develop a COM build under Win32, one of which is to get the MAC address of the local computer.

The requirements are simple, although I'm new to Python, but I'm still using search engines.

Baidu, found that most of the introduction using the import UUID to get the MAC address, or use Os.popen ("Ipconfig/all") way to get. The latter are susceptible to the English-language environment in the operating system.

As this article: http://www.cnblogs.com/Jerryshome/archive/2011/11/30/2269365.html

So, I'm good at using the first method that is recommended by most Web pages,

Def get_mac_address (): Import uuid node = Uuid.getnode () mac = uuid. UUID (int = node). HEX[-12:] Return mac

Luckily, I can use it directly.

Then write a program that accesses COM in C + +.

But here's the problem (I won't ask the excavator which strong-_-///)

Actually popped C RUNTIME!!

---------------------------Microsoft Visual C + + runtime Library---------------------------Runtime error! Program:e:\blender Foundation\blender.exer6034an application has made a attempt to load the C runtime library Incorrectl Y.please Contact the application ' s support team for more information.


Winxp/win7 32-bit This problem is present!!

After a scientific internet discovery, found on the following pages:

https://developer.blender.org/T27666

After the UUID is imported, the Python program is called by a program such as C + +, and the bug is not fixed and there is no good solution. The reason is: ctypes loads wrong version of C runtime, leading to error message box from system

The buglist of the official website is: http://bugs.python.org/issue17213

OK, unexpectedly official website can not solve, that can only change way.

After a round of science on the Internet, find the Netifaces library, the method and UUID, the same can be common to obtain MAC address.

Library Address: Https://pypi.python.org/pypi/netifaces

There are related tutorials, very simple, I posted the demo program I wrote, as follows:

Def networkinfo (self):     import netifaces    devlist =  []    tempList = []    tmpdict = {}     devices = netifaces.interfaces ()     for dev in  Devices:        devlist.append (Dev)                  for devinfo in devlist:         infos = netifaces.ifaddresses (DevInfo)          if len (infos)  < 2:             continue        ip = infos[ netifaces.af_inet][0][' addr ']        if ip !=  ':             if ip !=  ' 127.0.0.1 ':                 tmpdict["IP"] = ip                 tmpdict["Mac"] =  infos[netifaces.af_link][0][' addr ']                 break                     templist.append (tmpdict)          return templist

Here I get the local computer's network IP and the IP's Nic mac address. Details do not introduce, we can crossing network introduction.

A way to get MAC addresses under Python windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.