我一直在考虑开发一个机器人使用树莓皮使用一个旧的钢筋混凝土坦克。我知道python2附带了raspberry Pi,但我在我的电脑上使用的是python3
机器人的最终目标是能够将坐标通过python传递给pi,但我不确定python的2版本是否有兼容的网络脚本。在
我想用它们的网络连接Python2和3,但我不确定它们是否兼容,或者我是否也必须在我的PC上下载Python2?在
下面是一些更改后我很可能会使用的代码,如果我能够从这个python 3脚本发送信息,a python 2脚本:import socket, Encryption, threading
class Networking():
StrName = ""
BlnCon = False
StrMsgs = []
Server = socket.socket()
StrMsgR = ""
C = Encryption.Cryption()
def ConnectTrd(self,E):
TrdNetworking = threading.Thread(target = self.Connect, args = (E.TextBoxs.TxtboxSet[2].StrMessage,int(E.TextBoxs.TxtboxSet[3].StrMessage),E.TextBoxs.TxtboxSet[4].StrMessage,E))
TrdNetworking.start()
def Connect(self, StrHost, IntPort, StrName,E):
self.StrName = StrName
self.Server.connect((StrHost, IntPort))
self.BlnCon = True
while self.BlnCon:
try:
self.RecvMsg(E)
except Exception as Error:
print(Error)
self.Server.close()
def SendMsg(self,Message,E):
if self.BlnCon:
StrMsg = Message
if StrMsg == "EXIT":
self.BlnCon = False
StrMsg = self.StrName + ": " + "EXIT"
else:
StrMsg = self.StrName + ": " + Message
try:
self.StrMsgs.append(str(self.StrName + ": " + Message))
self.Server.send(self.C.EncryptMsg(StrMsg).encode())
except Exception as Error:
print(Error)
else:
print("can't send message.")
self.WriteToTxBx(E)
def RecvMsg(self,E):
if self.BlnCon:
try:
self.StrMsgR = self.C.DecryptMsg(self.Server.recv(10240).decode())
except Exception as Error:
print(Error)
self.BlnCon = False
StrConCh = self.StrMsgR.split(":")
if StrConCh[1] == " EXIT":
StrMsg = self.StrName + ": " + "EXIT"
self.Server.send(self.C.EncryptMsg(StrMsg).encode())
self.BlnCon = False
self.StrMsgs.append(str(StrConCh[0]) + " has disconnected.")
else:
self.StrMsgs.append(self.StrMsgR)
self.WriteToTxBx(E)
def WriteToTxBx(self,E):
E.TextBoxs.TxtboxSet[0].StrMessage = ""
for IntZ in range(0,len(self.StrMsgs)):
while len(self.StrMsgs[IntZ])%55 != 0:
self.StrMsgs[IntZ] += " "
E.TextBoxs.TxtboxSet[0].StrMessage += self.StrMsgs[IntZ]
代码来自我不久前制作的一个P2P客户端,它也使用了我用Pygame制作的一个基本GUI引擎。在