Python生成随机MAC地址

yipeiwu_com6年前Python基础

利用python代码生成一个随机的MAC地址,使用python网络编程时或可用上,如果使用scapy模块则可直接利用RandMAC()函数来生成MAC。

python

复制代码 代码如下:

import random
Maclist = []
for i in range(1,7):
    RANDSTR = "".join(random.sample("0123456789abcdef",2))
    Maclist.append(RANDSTR)
RANDMAC = ":".join(Maclist)
print RANDMAC
--------------------------------运行结果-----------------------------------
e4:13:0e:1a:73:f5

下列的Fake_HW是用struct打包成二进制格式的mac地址

复制代码 代码如下:

import random
import struct
mac_bin_list = []
mac_hex_list = []
for i in range(1,7):
    i = random.randint(0x00,0xff)
    mac_bin_list.append(i)
Fake_HW = struct.pack("BBBBBB",mac_bin_list[0], mac_bin_list[1], mac_bin_list[2], mac_bin_list[3], mac_bin_list[4], mac_bin_list[5])
for j in mac_bin_list:
    mac_hex_list.append(hex(j))
Hardware = ":".join(mac_hex_list).replace("0x","")
print Hardware
--------------------结果-----------------------------
24:c7:6f:92:2c:42

以上就是本文的全部内容了,希望大家能够喜欢。

相关文章

python、PyTorch图像读取与numpy转换实例

Tensor转为numpy np.array(Tensor) numpy转换为Tensor torch.Tensor(numpy.darray) PIL.Image.Image转换成nu...

Python实现快速排序和插入排序算法及自定义排序的示例

一、快速排序     快速排序(Quicksort)是对冒泡排序的一种改进。由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要...

谈谈Python中的while循环语句

谈谈Python中的while循环语句

前言 python中有两种循环,while和for,两种循环的区别是,while循环之前,先判断一次,如果满足条件的话,再循环,for循环的时候必须有一个可迭代的对象,才能循环,比如说得...

python实现小球弹跳效果

python实现小球弹跳效果

本文实例为大家分享了python实现小球弹跳效果的具体代码,供大家参考,具体内容如下 import pygame, sys pygame.init() screenGameC...

Python中的几种矩阵乘法(小结)

一.  np.dot() 1.同线性代数中矩阵乘法的定义。np.dot(A, B)表示: 对二维矩阵,计算真正意义上的矩阵乘积。 对于一维矩阵,计算两者的内积。 2...