将十六进制数据从客户端发送到服务器 - Python
Send Hex data from Client to Server - Python
我有这段代码,我需要使用 TCP 向服务器发送以下消息:
import socket
import sys
def invia_comandi(s):
while True:
comando = input("-> ")
if comando == "ESC":
s.close()
sys.exit()
else:
s.send("DATA 457f598514e2adafdb\
d3234675343aee4c216987a2a1e69c4681d43c6c43978ca59d322105bf00543089\
75b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n".encode())
#s.send(comando.encode())
data = s.recv(4096)
print(str(data, "utf-8"))
def conn_sub_server(indirizzo_server):
try:
s = socket.socket()
s.connect(indirizzo_server)
except socket.error as errore:
sys.exit()
invia_comandi(s)
if __name__ == '__main__':
conn_sub_server((".....", ....))
我需要发送消息DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf0054308975b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n
到服务器。当我尝试执行此操作时,我得到 ERROR Invalid hexadecimal character ' '
。
我能怎么做?谢谢
如果你将文本放在多行中,那么你必须在第一列开始下一行,因为 Python 不知道你想跳过 spaces 并且它添加 spaces 到 DATA
.
def invia_comandi(s):
while True:
comando = input("-> ")
if comando == "ESC":
s.close()
sys.exit()
else:
s.send("DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb\
d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98\
be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf00543089\
75b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n".encode())
或者你应该在每一行中使用引号(但不使用 \
)
def invia_comandi(s):
while True:
comando = input("-> ")
if comando == "ESC":
s.close()
sys.exit()
else:
s.send("DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb"
"d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98"
"be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf00543089"
"75b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n".encode())
所以你的 DATA
中有 spaces 并且它无法将 space 从十六进制转换为数字 - 这给出了 ERROR Invalid hexadecimal character ' '
我有这段代码,我需要使用 TCP 向服务器发送以下消息:
import socket
import sys
def invia_comandi(s):
while True:
comando = input("-> ")
if comando == "ESC":
s.close()
sys.exit()
else:
s.send("DATA 457f598514e2adafdb\
d3234675343aee4c216987a2a1e69c4681d43c6c43978ca59d322105bf00543089\
75b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n".encode())
#s.send(comando.encode())
data = s.recv(4096)
print(str(data, "utf-8"))
def conn_sub_server(indirizzo_server):
try:
s = socket.socket()
s.connect(indirizzo_server)
except socket.error as errore:
sys.exit()
invia_comandi(s)
if __name__ == '__main__':
conn_sub_server((".....", ....))
我需要发送消息DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf0054308975b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n
到服务器。当我尝试执行此操作时,我得到 ERROR Invalid hexadecimal character ' '
。
我能怎么做?谢谢
如果你将文本放在多行中,那么你必须在第一列开始下一行,因为 Python 不知道你想跳过 spaces 并且它添加 spaces 到 DATA
.
def invia_comandi(s):
while True:
comando = input("-> ")
if comando == "ESC":
s.close()
sys.exit()
else:
s.send("DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb\
d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98\
be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf00543089\
75b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n".encode())
或者你应该在每一行中使用引号(但不使用 \
)
def invia_comandi(s):
while True:
comando = input("-> ")
if comando == "ESC":
s.close()
sys.exit()
else:
s.send("DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb"
"d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98"
"be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf00543089"
"75b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n".encode())
所以你的 DATA
中有 spaces 并且它无法将 space 从十六进制转换为数字 - 这给出了 ERROR Invalid hexadecimal character ' '