python轉換IP/Port成Inet和mac數值轉換



判斷 endian

https://stackoverflow.com/questions/1346034/
import sys
sys.byteorder
'little'

IP/Port和INET之間的轉換

http://blog.csdn.net/fan_hai_ping/article/details/8435140

 socket.inet_aton("192.168.1.1")
'\xc0\xa8\x01\x01'

 addr = socket.inet_aton("192.168.1.1")
 type(addr)


import socket
 addr = socket.inet_aton('127.0.0.1')
 addr
'\x7f\x00\x00\x01'
>>> socket.inet_ntoa(addr)
'127.0.0.1'

轉換port

socket.htons(0x1234)
13330
socket.ntohs(0x1234)
13330
 socket.ntohs(1234)
53764
 socket.htons(1234)
53764

取得目前host ip

https://www.pythonsheets.com/notes/python-socket.html

 import socket
 socket.gethostname()
'MacBookPro-4380.local'
 hostname = socket.gethostname()
 socket.gethostbyname(hostname)
'172.20.10.4'
 socket.gethostbyname('localhost')
'127.0.0.1'

MAC數值轉換

import binascii
 mac = '00:11:32:3c:c3:0b'
 byte = binascii.unhexlify(mac.replace(':',''))
 byte
'\x00\x112<\xc3\x0b'
 binascii.hexlify(byte)
'0011323cc30b'

https://stackoverflow.com/questions/2329571/c-libcurl-get-output-into-a-string



留言