I have a program in python that is connected via TCP-IP to a matlab program, where the python code is the client and its receiving numbers like:
1
2
5
6
7
etc..
(the numbers I receive are only: 1, 2, 3, 4, 5, 6, 7) in a random order. The error I'm getting is: ValueError: invalid literal for int() with base 10: b'1\n5\n'. My code is:
# 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)
#print(i)
#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
The numbers that I'm receiving are generated at real time in the matlab program. The thing is when I simulate with standard values in matlab the python program works just fine, which leads me to believe that is something because of the real-time values generated in the matlab.
Also when I comment the part of #feedbak offline (for the user to know which are the words) until the end, the program receives the numbers and does the i = int (s) without any problem, only when i uncomment the rest it gives me the errors. When I print the values that I'm receiving is like:b'1\n' b'7\n' b'4\n' b'2\n' b'6\n' b'3\n' b'1\n' b'5\n' (etc..) -> it never says it receives 2 values at the same time, like it does when i uncomment the rest of the program
The all python program that i posted, works for the first 2/3 numbers and then gives me the error, here is the traceback:
>>>
RESTART: C:\Users\meca\Desktop\Python_Exercises\seq_tcp_offline-TCP-CLIENT.py
b'1\n'
b'7\n'
b'4\n'
Traceback (most recent call last):
File "C:\Users\meca\Desktop\Python_Exercises\seq_tcp_offline-TCP-CLIENT.py", line 139, in <module>
sequencia()
File "C:\Users\meca\Desktop\Python_Exercises\seq_tcp_offline-TCP-CLIENT.py", line 44, in sequencia
i = int(s)
ValueError: invalid literal for int() with base 10: b'2\n6\n'
This is very strange to me, any of you have any ideas? Many thanks
\n
is a newline character. int()
is having trouble when there are multiple newline characters in the bytes object.
A single number followed by a newline character can be converted.
>>> b = b'1\n'
>>> int(b)
1
When you receive a stream of numbers separated by newline characters you need to split the bytes object on the whitespace before converting.
>>> b = b'1\n5\n'
>>> b.split()
[b'1', b'5']
>>> for c in b.split():
... print(int(c))
1
5
>>>
or
>>> [int(n) for n in b.split()]
[1, 5]
>>>
Alternatively, you could try reading only 2 bytes on each iteration. Currently you are passing a value of 2000
to the buffsize parameter.
s = so.recv(2)
You would want to test this though to make sure you don't use data - I don't know how the socket handles data that piles up if you only read 2 bytes at a time. .