简体   繁体   English

在Python中为TCP通信编码字符串时出现问题

[英]Problems encoding string for TCP Communication in Python

collective internet, 集体互联网

I am a very new programmer that has given myself a specific project to teach myself coding. 我是一个非常新的程序员,给了我一个专门的项目来教自己编程。 I work with a lot of equipment that can take TCP commands so I set out to build a system of buttons that will send different commands per each button. 我使用许多可以接收TCP命令的设备,因此我着手构建一个按钮系统,该系统将为每个按钮发送不同的命令。 I got myself a Raspberry Pi 3b and took online classes on Python. 我给自己买了Raspberry Pi 3b,并上了Python在线课程。 I've got reasonably far on my own (I've got the buttons working how I want!) but where I've been stuck is sending TCP commands. 我已经靠自己了很多(按我想要的按钮工作了!),但是我一直被困的是发送TCP命令。

To be more specific: I am sending data and it is being received but the string command is not being encoded properly. 更具体地说:我正在发送数据并且正在接收数据,但是string命令未正确编码。 The commands are functional when I execute them in a telnet session, but obviously I want them executed as part of my script. 当我在telnet会话中执行命令时,这些命令是有效的,但是显然我希望它们作为脚本的一部分执行。 The commands don't specify that they need to be received over a telnet session and, by other means, I've had these commands work as TCP commands exterior to a telnet session. 这些命令没有指定需要通过telnet会话接收,并且通过其他方式,我已经将这些命令用作telnet会话外部的TCP命令。 I read about a telnet module for Python but I don't think I should need it. 我读到有关Python的telnet模块的信息,但我认为我不需要它。

I verified packet delivery with wireshark. 我用wireshark验证了数据包的传递。 I captured the packets sent by my script and the packets sent by the telnet session and they're similar but not the same. 我捕获了我的脚本发送的数据包和telnet会话发送的数据包,它们相似但不相同。 Horseshoes and hand grenades, right? 马蹄铁和手榴弹,对吗? My current method has been to just preface the string (within ') with a lower case b. 我当前的方法是在字符串(在'内)前面加上小写b。 I also tried putting .encode() after the string (omitting the b in that situation). 我还尝试将.encode()放在字符串后(在这种情况下省略b)。

The string command has the format: 字符串命令的格式为:

setInput "InputName" Value

So for my use case, I'm setting the input named "One" to a value of 1: 因此,对于我的用例,我将名为“ One”的输入设置为值1:

setInput "One" 1

So as you can see in my script (inserted below) I ended up using: 因此,如您所见(在下面插入脚本),我最终使用了:

s.sendall(b'setInput "One" 1')

But it's not quite sending the right information because it is not working and it doesn't look the same in wireshark. 但是它不能发送正确的信息,因为它不起作用,并且在wireshark中看起来也不一样。

TL;DR: I'm trying to send packets via TCP but they're not being encoded properly. TL; DR:我正在尝试通过TCP发送数据包,但未正确对其进行编码。

Ultimately, my question is if I am even headed in the right direction using these commands and just need a different means to encode the string or if I need to explore another direction entirely (perhaps the telnet module?) 最终,我的问题是使用这些命令是否可以使我朝正确的方向前进,而只是需要一种不同的方式来对字符串进行编码,或者是否需要完全探索另一个方向(也许是telnet模块?)

Here is the script I've been using to test and the wireshark output of my script: 这是我一直在测试的脚本以及脚本的wireshark输出:

import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.100.40', 3040))
print('connected')
time.sleep(2)
s.sendall(b'setInput "One" 1')
print('sent increase')
time.sleep(2)
s.sendall(b'setInput "One" 0')
print('sent decrease')

Wireshark log of my script 我的脚本的Wireshark日志

Here is the wireshark output of the telnet session that was successful: 这是成功的telnet会话的wireshk输出:

Wireshark log of the telnet session telnet会话的Wireshark日志

Any and all help is appreciated. 任何和所有帮助表示赞赏。 I looked far and wide and can't seem to find any cases similar to mine. 我四处张望,似乎找不到与我相似的案例。

EDITS: Sorry for the poor formatting. 编辑:对不起,格式不正确。 I appreciate the advice on how better to present posts. 我感谢有关如何更好地展示帖子的建议。 This is my first post here and I'm just getting the hang of it. 这是我在这里的第一篇文章,而我对此很了解。 My photos are still links due to my lack of privileges here. 由于我在这里的特权不足,我的照片仍然是链接。 Sorry if I was too wordy, I just wanted to supply as much information as possible so as to help people understand my problem and, if a solution is found, to help people with a similar issue find this. 抱歉,如果我太罗word,我只是想提供尽可能多的信息,以帮助人们理解我的问题,如果找到了解决方案,则可以帮助有类似问题的人们找到此问题。

The telnet tcp data includes a carriage return and a linefeed and the end of the data. telnet tcp数据包括回车符和换行符以及数据的结尾。 Apparently the receiving part needs this to be included to make things work. 显然,接收部分需要包含此内容以使工作正常。 So change your Python string to b'setInput "One" 1\\r\\n' 因此,将您的Python字符串更改为b'setInput "One" 1\\r\\n'

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

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