如何获取本地mac地址? [英] How to get the local mac address?
问题描述
我试过了:
导入ctypes
导入套接字
导入结构
def get_macaddress(host):
"""返回网络主机的MAC地址,需要> = WIN2K。
"""
#检查API可用性
尝试:
SendARP = ctypes.windll.Iphlpapi.SendARP
除外:
引发NotImplementedError(''仅用于Windows 2000年和
以上'')
#不能使用环回,但让我们试试并帮助。
如果主机==''127.0.0.1''或host.lower()==''localhost'':
host = socket.gethostname()
#gethostbyname块,所以明智地使用它。
尝试:
inetaddr = ctypes.windll.wsock32.inet_addr(主持人)
如果inetaddr在(0,-1)中:
提高异常
除外:
hostip = socket.gethostbyname(host)
inetaddr = ctypes.windll.wsock32.inet_addr(hostip)
buffer = ctypes.c_buffer(6)
addlen = ctypes.c_ulong (ctypes.sizeof(buffer))
如果SendARP(inetaddr,0, ctypes.byref(buffer),ctypes.byref(addlen))
!= 0:
引发WindowsError(''重新定位mac地址(%s) - 失败' '%
主持人)
#将二进制数据转换为字符串。
macaddr =''''
struct.unpack中的intval
(''BBBBBB'',缓冲区):
if intval> 15:
replacestr ='''0x''
else:
replacestr =''x''
macaddr =''''。join([macaddr,hex(intval).replace(replacestr,
'''')])
返回macaddr .upper()
if __name__ ==''__ main__'':
print''你的mac地址是%s''%get_macaddress('' localhost'')
它在W2K及以上版本下运行完美。但我想在
Win98上运行它。任何帮助?
谢谢
Daniel
Hi, I tried:
import ctypes
import socket
import struct
def get_macaddress(host):
""" Returns the MAC address of a network host, requires >= WIN2K.
"""
# Check for api availability
try:
SendARP = ctypes.windll.Iphlpapi.SendARP
except:
raise NotImplementedError(''Usage only on Windows 2000 and
above'')
# Doesn''t work with loopbacks, but let''s try and help.
if host == ''127.0.0.1'' or host.lower() == ''localhost'':
host = socket.gethostname()
# gethostbyname blocks, so use it wisely.
try:
inetaddr = ctypes.windll.wsock32.inet_addr(host)
if inetaddr in (0, -1):
raise Exception
except:
hostip = socket.gethostbyname(host)
inetaddr = ctypes.windll.wsock32.inet_addr(hostip)
buffer = ctypes.c_buffer(6)
addlen = ctypes.c_ulong(ctypes.sizeof(buffer))
if SendARP(inetaddr, 0, ctypes.byref(buffer), ctypes.byref(addlen))
!= 0:
raise WindowsError(''Retreival of mac address(%s) - failed'' %
host)
# Convert binary data into a string.
macaddr = ''''
for intval in struct.unpack(''BBBBBB'', buffer):
if intval > 15:
replacestr = ''0x''
else:
replacestr = ''x''
macaddr = ''''.join([macaddr, hex(intval).replace(replacestr,
'''')])
return macaddr.upper()
if __name__ == ''__main__'':
print ''Your mac address is %s'' % get_macaddress(''localhost'')
It works perfect under W2K and above. But I would like to run it on
Win98 too. Any help?
Thanks
Daniel
推荐答案
Daniel Crespo写道:
Daniel Crespo wrote:
我试过:...
#将二进制数据转换成字符串。
macaddr =''''
对于struct.unpack中的intval(''BBBBBB'',缓冲区):
如果是intval> 15:
replacestr =''0x''
否则:
replacestr =''x''
macaddr =''''。join([macaddr,hex(intval) .replace(replacestr,'''')])
Hi, I tried: ... # Convert binary data into a string.
macaddr = ''''
for intval in struct.unpack(''BBBBBB'', buffer):
if intval > 15:
replacestr = ''0x''
else:
replacestr = ''x''
macaddr = ''''.join([macaddr, hex(intval).replace(replacestr, '''')])
将以上内容替换为:
返回''%02X''* 6 %struct.unpack(''BBBBBB'',缓冲区)
(虽然没有帮助win98)
-
-Scott David Daniels
sc ******** ***@acm.org
斯科特,
感谢您的回答
Hi, Scott,
Thanks for your answer
我试过了:......
#将二进制数据转换成字符串。
macaddr =''''
对于struct.unpack中的intval(''BBBBBB'',缓冲区):
如果是intval> 15:
replacestr =''0x''
否则:
replacestr =''x''
macaddr =''''。join([macaddr,hex(intval) .replace(replacestr,'''')])
将以上内容替换为:
返回''%02X''* 6%struct.unpack(''BBBBBB'',缓冲区)
Hi, I tried: ...
# Convert binary data into a string.
macaddr = ''''
for intval in struct.unpack(''BBBBBB'', buffer):
if intval > 15:
replacestr = ''0x''
else:
replacestr = ''x''
macaddr = ''''.join([macaddr, hex(intval).replace(replacestr, '''')])
Replace the above by:
return ''%02X'' * 6 % struct.unpack(''BBBBBB'', buffer)
替换以上所有内容?我的意思是这一切:
#将二进制数据转换为字符串。
macaddr =''''
for intval in struct.unpack(''BBBBBB'',缓冲区):
if intval> 15:
replacestr ='''0x''
else:
replacestr =''x''
macaddr =''''。join([macaddr,hex(intval).replace(replacestr,
'''')])
?
(虽然没有帮助win98)
Replace all the above? I mean all this:
# Convert binary data into a string.
macaddr = ''''
for intval in struct.unpack(''BBBBBB'', buffer):
if intval > 15:
replacestr = ''0x''
else:
replacestr = ''x''
macaddr = ''''.join([macaddr, hex(intval).replace(replacestr,
'''')])
?
(doesn''t help with win98, though)
对不起,但我不明白......我想要它运行
Win98。我知道我必须在这里更改一些代码,但不知道是什么:(
Sorry, but I don''t understand... I would like it to get running on
Win98. I know I have to change some code here, but don''t know what :(
Daniel Crespo写道:
Daniel Crespo wrote:
我试过了:
导入ctypes
import socket
import struct
def get_macaddress(host):
"""返回网络主机的MAC地址,需要> = WIN2K。
"""
#检查api可用性>尝试:
SendARP = ctypes.windll.Iphlpapi.SendARP
除了:
引发NotImplementedError(''仅用于Windows 2000和
上面'')
< #不会使用环回,但让我们试试并帮助。
如果host ==''127.0.0.1''或host.lower()==''localhost'':
host = socket.gethostname()
#gethostbyname blocks,所以明智地使用它。
尝试:
inetaddr = ctypes.windll.wsock32.inet_addr(host)
如果inetaddr in(0,-1):
引发异常
除了:
hostip = socket.gethostbyname(host)
inetaddr = ctypes.windll.wsock32.inet_addr(hostip)<缓冲区= ctypes.c_buffer(6)
addlen = ctypes.c_ulong(ctypes.sizeof(buffer))
如果SendARP(inetaddr,0,ctypes.byref(buffer), ctypes.byref(addlen))
!= 0:
引发WindowsError(''重新定位mac地址(%s) - 失败''%
主持人)
>#将二进制数据转换为字符串。
macaddr =''''
用于struct.unpack中的intval('''BBBBBB'',缓冲区):
如果intval> 15:
replacestr =''0x''
否则:
replacestr =''x''
macaddr =''''。join([macaddr,hex(intval) .replace(replacestr,
'''')])
返回macaddr.upper()
如果__name__ ==''__ main __'':
print''你的mac地址是%s''%get_macaddress(''localhost'')
它在W2K及以上版本下运行完美。但是我想在Win98上运行它。有什么帮助?
Hi, I tried:
import ctypes
import socket
import struct
def get_macaddress(host):
""" Returns the MAC address of a network host, requires >= WIN2K.
"""
# Check for api availability
try:
SendARP = ctypes.windll.Iphlpapi.SendARP
except:
raise NotImplementedError(''Usage only on Windows 2000 and
above'')
# Doesn''t work with loopbacks, but let''s try and help.
if host == ''127.0.0.1'' or host.lower() == ''localhost'':
host = socket.gethostname()
# gethostbyname blocks, so use it wisely.
try:
inetaddr = ctypes.windll.wsock32.inet_addr(host)
if inetaddr in (0, -1):
raise Exception
except:
hostip = socket.gethostbyname(host)
inetaddr = ctypes.windll.wsock32.inet_addr(hostip)
buffer = ctypes.c_buffer(6)
addlen = ctypes.c_ulong(ctypes.sizeof(buffer))
if SendARP(inetaddr, 0, ctypes.byref(buffer), ctypes.byref(addlen))
!= 0:
raise WindowsError(''Retreival of mac address(%s) - failed'' %
host)
# Convert binary data into a string.
macaddr = ''''
for intval in struct.unpack(''BBBBBB'', buffer):
if intval > 15:
replacestr = ''0x''
else:
replacestr = ''x''
macaddr = ''''.join([macaddr, hex(intval).replace(replacestr,
'''')])
return macaddr.upper()
if __name__ == ''__main__'':
print ''Your mac address is %s'' % get_macaddress(''localhost'')
It works perfect under W2K and above. But I would like to run it on
Win98 too. Any help?
很抱歉,我不能以任何方式帮助你,但我自己也有问题。
在Python中有一种独立于操作系统的方式来获取这个东西(无论如何以b $ b格式化它)?我知道这可能无关紧要,如果您想要的只是
Windows但是只有另一个线程在谈论应该写一个独立于操作系统的
程序。
这是python网络库的问题吗?
Sorry that I can''t help you in any way but have a question myself. Is
there an OS independent way to get this thing(regardless of how to
format it) in Python ? I know this may not matter if all you want is
Windows but there is just another thread talking about one should write
programs that is OS independent.
Is this a problem of the network library of python ?
这篇关于如何获取本地mac地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!