简体   繁体   中英

typeError: a byte-like object is required, not 'str' in Python 3

I am new to python and I am experimenting things. I am trying to solve this UDP client and server program in python3 using python socket.

The objective of my UDP server program named UDpserver.py are as follows:

  • Receives a message from a client
  • Responds to the client with the same message but all in UPPER CASE.

The objective of a client program named UDpclient.py that connects to above server and receives a response from the server:

  • sends a message to the server
  • Display the message received from the server. The displayed message should be in upper case.

following is the server program's code (UDpserver.py):

from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print (“The server is ready to receive”)
while 1:
 message, clientAddress = serverSocket.recvfrom(2048)
 modifiedMessage = message.upper()
 serverSocket.sendto(modifiedMessage, clientAddress)

Following is the client program code(UDpclient.py)

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

When I run the client program(Udpclient.py) I get this error after it lets me input lowercase letters:

TypeError: a bytes-like object is required, not 'str' for line 7 ie clientSocket.sendto(message,(serverName, serverPort))

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

If anybody could please help me solve this. Thank you.

You can use message.encode() to encode the string type message. So this is how your UDPclient.py would look like:

import socket
serverName = ‘hostname’
serverPort = 12000
clientSocket = socket.socket(socket.AF_INET,
 socket.SOCK_DGRAM)
message = input(’Input lowercase sentence:’)
clientSocket.sendto(message.encode(),(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close() 

Python 3 strings fully support the unicode specification. An in-memory string must be serialized in some way to conform to some sort of encoding such as ASCII, UTF-8, various Windows code pages and etc, before being sent on the network. sendto expects an already-encoded bytes string.

Since you are transfering data between machines, its not a good idea to let each one choose its own encoding. The easiest option for a home grown protocol is to just declare that you will use UTF-8, which is the most interoperable unicode encoding.

clientSocket.sendto(message.encode("utf-8"),(serverName, serverPort))

The receiver would decode using the same encoding.

将“主机名”更改为实际 IP 并使用以下命令更新错误代码后解决了该问题

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM