I recently came across little troubles with python variables affectations. I affected the same variable with different types. For instance:
hello = 1
print(hello)
hello = "Hello"
print(hello)
The output is what I expect: it displays 1 and then hello.
But I have a problem with an echo server test script:
skt_o = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
skt_o.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
skt_o.bind(('', 7777))
skt_o.listen(1)
lst_skt = []
while(1):
lst, _, _ = select.select(lst_skt+[skt_o], [], [])
for s in lst:
if(s == skt_o):
skt, _ = s.accept()
lst_skt.append(skt)
else:
res = s.recv(1500)
if(len(dat) == 0):
print("[R-Close] %s" % s)
s.close()
lst_skt.remove(s)
break
for c in lst_skt:
if(c != s):
res = c.send(res)
I use nc localhost 7777 for connecting two terminals to the listening port. When I send a message everything work as expected. The two terminals can communicate. But when I launch a third terminal and try to send a message, I get a TypeError (int does not support the buffer interface).
if I replace the res variable as shown below everything work well. I can connect three and more terminals which can communicate:
dat = s.recv(1500)
if(len(dat) == 0):
print("[R-Close] %s" % s)
s.close()
lst_skt.remove(s)
break
for c in lst_skt:
if(c != s):
res = c.send(dat)
What is the problem with the first echo script?
I know that the problem comes from this line:
res = c.send(res)
But I cannot explain why.
nb: I use Python 3.4.2
Thanks for your answers.
result = ''; result += sock.read(2048). Don't do that. BADTIE - bytes are decode, text is encoded. Or it may be something else, given that you don't know what the error actually is.