gpt4 book ai didi

python - 我从 Python 3.6.2 : TypeError: getsockaddrarg: AF_INET address must be tuple, not bytes 收到错误

转载 作者:行者123 更新时间:2023-12-01 02:24:03 26 4
gpt4 key购买 nike

我在 StackOverflow 上进行了搜索,但提到的技巧都不起作用。这是我的代码。

    from socket import *
import time

address= ( '10.1.15.243', '5000') #Server data
client_socket =socket(AF_INET, SOCK_DGRAM) #Socket klaarzetten
client_socket.settimeout(1) #Response time

while(1):

data = "Positie" #Data request = Positie

client_socket.sendto( (b'10.1.15.243'), (b'5000') ) #Data aanvragen

try:

rec_data, addr = client_socket.recvfrom(2048) #Read response from arduino
temp = float(rec_data) #Convert string rec_data naar float temp
with open('resultaten.txt', 'w') as f: #Data in TXT opslaan
print('Positie:', filename, file=f) #Data confirmen
except:
pass

最佳答案

documentation说:

A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.

sendto 需要消息和地址,所以有 2 个参数。你正在传递这个:

client_socket.sendto( (b'10.1.15.243'), (b'5000') )

因此从sendto来看,消息是b'10.1.15.243',地址是b'5000'b"5000" 是一个字节对象,它解释了错误消息。这里的括号没有任何作用(即使它们确实定义了元组,那仍然是不正确的)。而且,你还忘记传递你的有效负载......

由于 address 是一个正确的地址元组 (('10.1.15.243','5000')),只需执行以下操作:

data = b"Positie"  # needs to be bytes object
client_socket.sendto(data, address)

(地址已在上面正确定义)

关于python - 我从 Python 3.6.2 : TypeError: getsockaddrarg: AF_INET address must be tuple, not bytes 收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47580776/

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