Python Small Example

Source: Internet
Author: User

1, statistical system remaining memory.

2, according to the existing MAC address, write a MAC address 1 larger than the previous MAC address.

1, Statistics system remaining memory (Linux for example)

#!/usr/bin/env python#-*-coding:utf-8-*-with open ('/proc/meminfo ') as F: For line in    F.xreadlines ():        if Line.startswith (' Memtotal '): Total            = Int (Line.split () [1])            total/=1024.0            continue        if Line.startswith (' Memfree '): Free            = Int (Line.split () [1])            free/=1024.0            breakprint ' machine total memory%.2fm '% print ' remaining memory total%.2fm '% (free) print ' remaining memory consumption percent%.2f '% (free/total)

  

2, according to the existing MAC address, write a MAC address 1 larger than the previous MAC address.

#!/usr/bin/env python#-*-coding:utf-8-*-macaddr = ' 00:16:3d:00:30:ad ' #获取mac地址最后一段end = macaddr[-2:] #将16进制转换为10进制n = Int (end,16) #在10进制数值的基础上加1n = n + # Converts the newly produced 10-digit number to 16-binary, and the 16-binary starts with 0x, so remove it. New_end = Hex (n). Strip (' 0x ') #判断new_end的长度. Because if the last paragraph is 01 such that less than 10 of the 16 binary, it means that my newly generated 16 binary is removed after 0x will be only 1 digits, so write to the MAC address is one less. If Len (new_end) <2:new_end = ' 0 ' +  new_end new_macaddr = macaddr[:-2] + new_end.upper () print new_macaddr

  

The last problem is to figure out 16 binary and 10 binary conversion is no problem.

1, convert the 16 binary to 10 binary int (' a ', +) int (' 0xa ',)  0x is the 16-binary front identifier 2, convert 10 to 16 hex (10)

  

Python Small Example

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.