简体   繁体   English

Python 字节似乎与套接字编程中应有的不同

[英]Python bytes seem different than it should be in socket programming

I've been trying to learn socket programming and my aim is to send request from client to the server, also send back a response from server to client despite client's request.我一直在尝试学习套接字编程,我的目标是将请求从客户端发送到服务器,尽管有客户端的请求,也将响应从服务器发送回客户端。 In the code, i am trying to send \x02\x03\x35\x36\x37\x01\x00\05\x02\x75 and \x02\x05\x01\x35\x36\x01\x01\05\x02\x76 datas but in my output i see \x02\x03567\x01\x00\x05\x02u and \x02\x05\x0156\x01\x01\x05\x02v instead of them.在代码中,我试图发送 \x02\x03\x35\x36\x37\x01\x00\05\x02\x75 和 \x02\x05\x01\x35\x36\x01\x01\05\x02\x76数据,但在我的 output 中,我看到 \x02\x03567\x01\x00\x05\x02u 和 \x02\x05\x0156\x01\x01\x05\x02v 而不是它们。 Am i doing something wrong?难道我做错了什么? Here are my codes:这是我的代码:

server.py服务器.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1236))

s.listen(5)
print("Waiting for connection...")

server_message = b"\x02\x05\x01\x35\x36\x01\x01\05\x02\x76"

while True:
    clientsocket, addr = s.accept()
    print(f"Successfully connected with client from {addr}", addr)

    client_message = clientsocket.recv(1024)            # Get request from the client.
    print("Request from the Client:", client_message)

    if client_message.__eq__(b"\x02\x03\x35\x36\x37\x01\x00\05\x02\x75"): # If request is appropriate for response then;
        clientsocket.send(server_message)           # Send response to the client.
        print("Response to the Client:", server_message)

    clientsocket.close()

client.py客户端.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1236))

client_message = b"\x02\x03\x35\x36\x37\x01\x00\05\x02\x75"

try:
    s.send(client_message)      # Send request to the server.

    server_message = s.recv(1024)       # Get response from the server.

    print("Request to the Server:", client_message)
    print("Response from the Server:", server_message)

    s.close()

except socket.error as error_message:
    print("Server is not working", error_message)

You're not doing anything wrong, and they only seem different.你没有做错任何事,他们只是看起来不同而已。 As you will quickly see see if you do this:正如您很快就会看到的那样,看看您是否这样做:

>>> version1 = b"\x02\x03\x35\x36\x37\x01\x00\05\x02\x75"
>>> version2 = b'\x02\x03567\x01\x00\x05\x02u'
>>> version1==version2
True

The reason is because \x35 is the byte value of the character 5 , and when Python displays bytestrings it renders any printable character as that character, and if it is not printable but there is a string escape, then it renders it as the string escape, so \x0d which is a carriage return will appear as \r , and it only renders a byte as a hex escape as a last resort.原因是因为\x35是字符5的字节值,当 Python 显示字节串时,它将任何可打印字符呈现为该字符,如果它不可打印但存在字符串转义,则将其呈现为字符串转义,因此作为回车符的\x0d将显示为\r ,并且它仅将一个字节呈现为十六进制转义作为最后的手段。 There are various reasons for this, but one of the most obvious is that bytestrings often contain character data, and b'Hello world' is friendlier than b'\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64' .这有多种原因,但最明显的原因之一是字节串通常包含字符数据,而b'Hello world'b'\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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