83

The following is the code that tries to modify the input supplied by a user by using sockets:

from socket import *

serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = input('Input lowercase sentence:')
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print (modifiedMessage)
clientSocket.close()

When I execute it and supply input the following error occurs:

Input lowercase sentence:fdsgfdf
Traceback (most recent call last):
  File "C:\srinath files\NETWORKS\UDPclient.py", line 6, in <module>
    clientSocket.sendto(message,(serverName, serverPort))
TypeError: a bytes-like object is required, not 'str'

What can I do to solve this?

user229044
  • 232,980
  • 40
  • 330
  • 338
sri
  • 831
  • 1
  • 6
  • 3
  • 8
    The first argument (`message`) needs to be bytes, but you're passing a string. You should encode it before sending e.g. `message.encode('utf-8')` – mgilson Oct 07 '15 at 22:34
  • 1
    but the thing is i need to pass string to the server not byte – sri Oct 07 '15 at 22:57
  • 1
    from socket import * serverName = 'hostname' serverPort = 12000 clientSocket = socket(AF_INET, SOCK_DGRAM) message = input('Input lowercase sentence:') message.encode('utf-8') clientSocket.sendto(message,(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print (modifiedMessage) clientSocket.close() – sri Oct 07 '15 at 22:57
  • i got the same error – sri Oct 07 '15 at 22:58
  • 5
    The thing that goes on the wire is _always_ bytes. The server needs to be able to make sense of those bytes somehow. – mgilson Oct 07 '15 at 22:58
  • 3
    That's because `message` is still a string. You need something like `clientSocket.sendto(message.encode('utf-8'), ...)` – mgilson Oct 07 '15 at 22:59

6 Answers6

97

This code is good for Python 2. But in Python 3, results in bit encoding error. I was trying to make a simple TCP server and encountered the same problem. Encoding solves this. Try this with sendto command.

clientSocket.sendto(message.encode(),(serverName, serverPort))

Similarly you should use .decode() to receive the data on the UDP server side, if you want to print it exactly as it was sent.

Umair47
  • 1,102
  • 1
  • 8
  • 12
  • would encoding and decoding on both sides of the connection increase latency? If so would python2 actually be faster at sending data than python3 – Max Oct 12 '17 at 16:14
  • In my opinion, this will not affect the performance such as latency etc. However, I haven't tried experimented with it myself so I can't be sure about that. – Umair47 Oct 19 '17 at 17:32
  • Did python do this just to support emoji in their native String class? I guess good for internationalization re: `message.encode('utf-16')` and – Tomachi Nov 05 '21 at 09:04
34

Encoding and decoding can solve this in Python 3:

Client Side:

>>> host='127.0.0.1'
>>> port=1337
>>> import socket
>>> s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> s.connect((host,port))
>>> st='connection done'
>>> byt=st.encode()
>>> s.send(byt)
15
>>>

Server Side:

>>> host=''
>>> port=1337
>>> import socket
>>> s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> s.bind((host,port))
>>> s.listen(1)
>>> conn ,addr=s.accept()
>>> data=conn.recv(2000)
>>> data.decode()
'connection done'
>>>
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
Ahmed Motawea
  • 341
  • 3
  • 3
14

A bit of encoding can solve this:

Client Side:

message = input("->")
clientSocket.sendto(message.encode('utf-8'), (address, port))

Server Side:

data = s.recv(1024)
modifiedMessage, serverAddress = clientSocket.recvfrom(message.decode('utf-8'))
Wlliam
  • 175
  • 4
  • 12
5

Simply replace message parameter passed in clientSocket.sendto(message,(serverName, serverPort)) to clientSocket.sendto(message.encode(),(serverName, serverPort)). Then you would successfully run in in python3

Evgeniy Mironov
  • 777
  • 6
  • 22
TestStart
  • 93
  • 1
  • 9
4

Whenever you encounter an error with this message use my_string.encode().

(where my_string is the string you're passing to a function/method).

The encode method of str objects returns the encoded version of the string as a bytes object which you can then use. In this specific instance, socket methods such as .send expect a bytes object as the data to be sent, not a string object.

Since you have an object of type str and you're passing it to a function/method that expects an object of type bytes, an error is raised that clearly explains that:

TypeError: a bytes-like object is required, not 'str'

So the encode method of strings is needed, applied on a str value and returning a bytes value:

>>> s = "Hello world"
>>> print(type(s))
<class 'str'>
>>> byte_s = s.encode()
>>> print(type(byte_s))
<class 'bytes'>
>>> print(byte_s)
b"Hello world"

Here the prefix b in b'Hello world' denotes that this is indeed a bytes object. You can then pass it to whatever function is expecting it in order for it to run smoothly.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
1

add "b" in your sending data like this: request = client.send(b"GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")

shivaay
  • 11
  • 2