[英]parse data out of a specific TCPIP response code
i am currently working on a TCPIP communication.我目前正在研究 TCPIP 通信。 Everything is working fine, i can send messages and get specific response and stuff.
一切正常,我可以发送消息并获得特定的响应和内容。 My problem is I want to get a feedback if the communication was good or not, for now i am getting a feedback with the command i have sendet and a response code.
我的问题是,如果沟通好坏,我想得到反馈,现在我得到的反馈是我有 sendet 的命令和响应代码。 I want to parse the response code out of the whole string that I'm geting back... how to do this?
我想从我要返回的整个字符串中解析响应代码...该怎么做?
Heres what my working code looks like:这是我的工作代码的样子:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(("192.168.2.25", 3000))
s.sendall(b'GetLayoutPosition:3\r\n')
data = s.recv(1024)
print(data.decode())
after this i am receiving ->在此之后我收到->
GetLayoutPosition:1;3;10.5;11.8;0
The return values are:返回值是:
meaning layout position 3 at position 10.5 mm in X and 11.8 mm in Y with an angle 0...意思是布局 position 3 at position X 方向 10.5 mm,Y 方向 11.8 mm,角度 0...
now i want to safe the status, X and Y values out of the response.现在我想从响应中保护状态、X 和 Y 值。
What would be a way to do this?这样做的方法是什么?
Thanks:)谢谢:)
i tried to search online but couldnt find a specific solution to this...我试图在线搜索但找不到具体的解决方案...
import re
response = "GetLayoutPosition:1;3;10.5;11.8;0"
code = {"1":"success", "2":"pnf", "4":"oor", "6":"if"}
regex = r"^GetLayoutPosition:(?P<code>\S+);(?P<index>\S+);(?P<x>\S+);(?P<y>\S+);(?P<degree>\d+)$"
match = re.match(regex, response)
result = match.groupdict() | {"code_desc": code.get(match["code"], "unknown")}
{'code': '1', 'index': '3', 'x': '10.5', 'y': '11.8', 'degree': '0', 'code_desc': 'success'} {'code': '1', 'index': '3', 'x': '10.5', 'y': '11.8', 'degree': '0', 'code_desc': 'success'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.