我的代码有问题,我有一个TCP连接,我是客户端,收到的数字如下:
1
4
6
3
..
(我收到的数字仅为:1、2、3、4、5、6、7)以随机顺序排列。 我得到的错误是:ValueError:以10为底的int()的无效文字:b'2 \ n6 \ n \ n'。
引用此部分的代码是:
# TCP connection
try:
so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err:
print ("socket creation failed with error %s" %(err))
# default port for socket
port = 2000
# default time out
so.settimeout(1000000)
try:
host_ip = socket.gethostbyname('localhost')
except socket.gaierror:
# this means could not resolve the host
print ("there was an error resolving the host")
sys.exit()
# connecting to the server
so.connect((host_ip,port))
# MATLAB INFORMATION FOR OFFLINE EXPERIMENT
Nepoch = 10 #nr de epochs por trial
Nwords = 7 #nr de palavras (SIM, NAO, FOME, SEDE, URINAR, AR, POSICAO)
SeqTrain = [1, 3, 5, 7, 2, 4, 6, 1, 3, 5, 7, 2, 4, 6] #sequencia offline de treino
# read the TCP sequence received
def sequencia():
num = 0
for i in range(0,999):
s = so.recv(port) + b'\n' #since the sequence received is : 1\n 2\n 5\n etc
i = int(s)
#feedbak offline (for the user to know which are the words)
if (num in (0, Nepoch*Nwords+1, Nepoch*Nwords*2+2, Nepoch*Nwords*3+3, Nepoch*Nwords*4+4, Nepoch*Nwords*5+5,\
Nepoch*Nwords*6+6)):
labels1[i-1].configure(foreground="white")
root.update()
elif (num in (Nepoch*Nwords*7+7, Nepoch*Nwords*8+8, Nepoch*Nwords*9+9, Nepoch*Nwords*10+10,\
Nepoch*Nwords*11+11, Nepoch*Nwords*12+12, Nepoch*Nwords*13+13)):
labels2[i-1].configure(foreground="white")
root.update()
else:
labels[i-1].configure(background="green",foreground="red")
root.update()
winsound.PlaySound(sounds[i-1], winsound.SND_FILENAME)
labels[i-1].configure(background="gray",foreground="white")
root.update()
num = num + 1
其中的一部分是错误的:第44行:i = int(s)。 您有什么想法我可以解决这个问题吗?非常感谢
答案 0 :(得分:0)
这里:
so.recv(port)
您收到的电话号码不只一个。试试:
s = so.recv(port)
for i in [int(n) for n in s.split(b'\n') if n]:
...