【python】SyntaxError: bytes can only contain ASCII literal c

line 27 client_socket.sendall(b"数据已接收") ^^^^^^^^

line 27 client_socket.sendall(b"数据已接收") ^^^^^^^^ SyntaxError: bytes can only contain ASCII literal characters
【python】SyntaxError: bytes can only contain ASCII literal c_第1张图片

分析

The error you’re seeing is happening in simple_tcp_gateway.py where you’re trying to create a bytes literal with non-ASCII characters (Chinese text). Python’s bytes literals (prefixed with b) can only contain ASCII characters.

To fix this, you need to encode the string properly:

# Instead of:
client_socket.sendall(b"数据已接收")

# Do this:
client_socket.sendall("数据已接收".encode('utf-8'))

This properly encodes the Chinese characters to UTF-8 bytes before sending them. This is similar to how your client code is already handling text encoding when sending data.

  • This properly encodes the Chinese characters to UTF-8 bytes before sending them. This is similar to how your client code is already handling text encoding when sending data.

你可能感兴趣的:(服务器/脚本/编译,python,c语言,android)