简体   繁体   English

从客户端向服务器发送十六进制数据 - Python

[英]Send Hex data from Client to Server - Python

I have this code, I need to send the following message to the server using TCP:我有这段代码,我需要使用 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((".....", ....))

I need to send the message DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf0054308975b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n to the server. I need to send the message DATA 60b4151f093363cc4fc2b4575e531bac3e715ae59b33b5136f598514e2adafdb d3234675343aee4c216987a2a1e69c4696610b1eb8b9ce3c54325b65e8de0d98be8074927c0f037288e9eba3530f4a81d43c6c43978ca59d322105bf0054308975b04e3971bb40407e921f294af6bb91eb0cda8571886bca38301e06fc4fd9c5\r\n to the server. When I try executing this I get ERROR Invalid hexadecimal character ' ' .当我尝试执行此操作时,我得到ERROR Invalid hexadecimal character ' ' How can I do?我能怎么做? Thanks谢谢

If you put text in many lines then you have to start next line in first column because Python doesn't know that you want to skip spaces and it add spaces to DATA .如果您将文本放在多行中,那么您必须在第一列中开始下一行,因为 Python 不知道您要跳过空格并且它会在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())

Or you should use quote in every line (but without \ )或者你应该在每一行使用引号(但没有\

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())

So you had spaces in your DATA and it had problem to convert space from hex to number - and this gives ERROR Invalid hexadecimal character ' '因此,您的DATA中有空格,并且将空格从十六进制转换为数字时遇到了问题-这会给出ERROR Invalid hexadecimal character ' '

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

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