简体   繁体   English

如何在Python中以这种通信协议格式通过TCP发送无符号字符?

[英]How to send unsigned characters over TCP in this communication protocol format in Python?

So, I'm working on a project with a proprietary communication protocol. 因此,我正在使用专有通信协议进行项目。 I need to send data in given format and Up, Down, Left & Right co-ordinates to particular IP address. 我需要以给定的格式发送数据,并且将上,下,左和右坐标发送到特定的IP地址。 Communication happens over TCP and I need to program TCP client in python to send data in following format: 通信是通过TCP进行的,我需要使用python对TCP客户端进行编程,以便以以下格式发送数据:

IP address/Handshake address: 192.166.166.166 IP地址/握手地址:192.166.166.166

This is the Data format . 这是数据格式

Data type is unsigned char 0-255 (8-bit binary) and data length is 6-64 bit. 数据类型为无符号char 0-255(8位二进制),数据长度为6-64位。

Data length of each frame is 10-64 bit. 每帧的数据长度为10-64位。

So, If I want to move object at 1.0.0.1 to (23, 45, 67, 89), this is the given instruction: 因此,如果我要将对象从1.0.0.1移动到(23,45,67,89),这是给定的指令:

Send 255 255 10 3 1 0 0 1 23 45 67 89 to specified IP address. 将255 255 10 3 1 0 0 1 23 45 67 89发送到指定的IP地址。 I imagine specified IP address is 192.166.166.166. 我想象指定的IP地址是192.166.166.166。 You can refer to data format to understand this data that I'm supposed to send. 您可以参考数据格式来理解我应该发送的数据。 It's quite simple. 这很简单。

The question is, how am I supposed to send this series of unsigned chars over TCP in python? 问题是,我应该如何在python中通过TCP发送这一系列未签名的字符?

I've tried following: 我试过以下:

import socket
host = '192.166.166.166'
port = 80                   # 80 Because TCP

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

data = '255 255 10 3 1 0 0 1 23 45 67 89'
s.sendall(data)
result = s.recv(1024)
s.close()
print('Received', repr(result))

Obviously this is not working. 显然这是行不通的。 I've not specified unsigned char and I'm just sending raw data and spaces. 我没有指定unsigned char,而是发送原始数据和空格。

This is what I get in return from server: 这是我从服务器获得的回报:

('Received', '\'HTTP/1.0 400 Bad Request\\r\\nServer: Mini-IoT-314\\r\\nDate: , 31  1969 23:59:59 GMT\\r\\nPragma: no-
cache\\r\\nCache-Control: no-cache\\r\\nContent-Type: 
text/html\\r\\nConnection: close\\r\\n\\r\\n<HTML><HEAD><TITLE>400 Bad 
Request</TITLE></HEAD>\\n<BODY BGCOLOR="#cc9999"><H4>400 Bad 
Request</H4>\\nCan\\\'t parse request.\\n</BODY></HTML>\\n\'')

Now, I'm not sure what to do and how to send this data so server can process this data appropriately. 现在,我不确定该怎么做以及如何发送该数据,以便服务器可以适当地处理这些数据。 I would really appreciate a help here. 我非常感谢您的帮助。

Not really my area of expertise, just an idea: You might need to send the data as a Structure. 并不是我真正的专业领域,只是一个想法:您可能需要将数据作为结构发送。

Have a look at python's ctypes: https://docs.python.org/3/library/ctypes.html#structured-data-types 看看python的ctypes: https : //docs.python.org/3/library/ctypes.html#structured-data-types

from ctypes import *

class payload(Structure):
    _fields_ = [("data_sign1", c_ubyte),
                  ...]

1) I tested your code in python 3.5 with slight modifications rather than sending string,i sent bytes by converting string into bytes 1)我在python 3.5中测试了您的代码,进行了一些修改而不是发送字符串,我通过将字符串转换为字节来发送字节

bytes(data,'utf-8')

2) Do not use Port 80,might be your http server is running on that port, Try to use some other port. 2)不要使用端口80,因为您的http服务器正在该端口上运行,请尝试使用其他端口。 it will work 它会工作

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

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