我有这个代码,我需要使用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
75b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5rn".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 d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf0054308975b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5rn
到服务器。当我尝试执行此操作时,我会得到ERROR Invalid hexadecimal character ' '
。我该怎么办?感谢
如果您将文本放在多行中,那么您必须从第一列的下一行开始,因为Python不知道您想要跳过空格,它会向DATA
添加空格。
def invia_comandi(s):
while True:
comando = input("-> ")
if comando == "ESC":
s.close()
sys.exit()
else:
s.send("DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb
d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98
be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf00543089
75b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5rn".encode())
或者你应该在每一行都使用引号(但没有(
def invia_comandi(s):
while True:
comando = input("-> ")
if comando == "ESC":
s.close()
sys.exit()
else:
s.send("DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb"
"d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98"
"be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf00543089"
"75b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5rn".encode())
所以你的DATA
中有空格,把空格从十六进制转换成数字有问题——这给了ERROR Invalid hexadecimal character ' '