gpt4 book ai didi

python - struct.error : argument for's' must be a bytes object

转载 作者:行者123 更新时间:2023-12-03 12:08:48 31 4
gpt4 key购买 nike

通过使用以下代码:

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: argument for's' must be a bytes object"



这里是哪个对象?

最佳答案

在字符串前面加上b,使它们成为字节,例如:

ifreq = struct.pack('16sH2s4s8s', iface, socket.AF_INET, b'\x00' * 2, bin_ip, b'\x00' * 8)

setIpAddr(b'em1', '192.168.0.1')

关于python - struct.error : argument for's' must be a bytes object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48394741/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com