0

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.

5
  • 2
    I doubt the errors are "random". What errors are you talking about? Commented Dec 21, 2016 at 4:11
  • You still haven't said what the error is. Commented Dec 21, 2016 at 4:15
  • I am trying to reproduce it don't remember the exact sentence but the compiler claimed that he could not affect the variable with a new type. Commented Dec 21, 2016 at 4:18
  • It was a TypeError when using sockets. I will post the code tonight, need to go to work. @Carcigenicate I doubt the errors are "random". Me too I may have made a mistake. Well, we will see tonight. Commented Dec 21, 2016 at 4:45
  • If it was a TypeError it's likely you were doing something like 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. Commented Dec 21, 2016 at 5:51

1 Answer 1

1

This is because in python 3.4.x the send method of the socket object accepts a 'byte' sequence type as an argument. Thus you need to convert the argument you are passing to a byte by using a 'b' prefix with normal string syntax: b'python'. This converts the string python to a byte sequence type. So in your code you need to convert the res you are passing into the send method to a byte. Use something like this in your code instead. res = c.send(b'some-string-or-integer') check the python docs on built-in types, section 5.6 via the link below: https://docs.python.org/3.1/library/stdtypes.html

Also from the python docs too....

Also, while in previous Python versions, byte strings and Unicode strings could be exchanged for each other rather freely (barring encoding issues), strings and bytes are now completely separate concepts. There’s no implicit en-/decoding if you pass an object of the wrong type. A string always compares unequal to a bytes or bytearray object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.