0

I have the following python socket client. Which sends a small message to a socket server. In Python2.7 this works without flaw. However in 3.4 I get the following Traceback. Has the implementation changed? Whats the approach I should take to debug?

Traceback (most recent call last):
  File "echo-client.py", line 19, in <module>
    sockobj.send(line)
TypeError: 'str' does not support the buffer interface

import sys
from socket import *


_server = 'localhost'
_port = 50007

message = ['Hello network world']

if len(sys.argv) > 1:
    _server = sys.argv[1]
    if len(sys.argv) > 2:
        _port = sys.argv[2]

sockobj = socket(AF_INET, SOCK_STREAM)
sockobj.connect((_server, _port))

for line in message:
    sockobj.send(line)
    data = sockobj.recv(1024)
    print('Client received: {}'.format(data))

sockobj.close()
dcrearer
  • 1,972
  • 4
  • 24
  • 48

1 Answers1

1

Change

sockobj.send(line)

to

sockobj.send(line.encode())

In Python 3.x send works on bytes not str. Or we could say, Python 3.x rightly(my opinion) ceased to coerce str -> bytes.

C Panda
  • 3,297
  • 2
  • 11
  • 11