使用此代码:
import socket, struct, fcntl
SIOCSIFADDR = 0x8916
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def setIpAddr(iface, ip):
bin_ip = socket.inet_aton(ip)
ifreq = struct.pack('16sH2s4s8s', iface, socket.AF_INET, '\x00'*2, bin_ip,'\x00'*8)
fcntl.ioctl(sock, SIOCSIFADDR, ifreq)
setIpAddr('em1', '192.168.0.1')
我收到了
“struct.error:'的参数'必须是字节对象”
这是指哪个对象?
答案 0 :(得分:0)
在b
前面添加字符串,使其成为字节:
ifreq = struct.pack('16sH2s4s8s', iface, socket.AF_INET, b'\x00' * 2, bin_ip, b'\x00' * 8)
setIpAddr(b'em1', '192.168.0.1')