Kyle Burns
Kyle Burns

Reputation: 167

cross compatibility of python 2.x and python 3.x for networking

I've been thinking of developing a robot using the Raspberry pi using an old RC tank I have. I know the raspberry Pi come with Python 2 but I'm using Python 3 on my PC.

The end goal for the robot is to be able to pass it coordinate through python to the pi but I'm not sure if the 2 version of python have compatible networking scripts.

I want to connect Python 2 and 3 using their networking but I'm not sure if they are compatible or if I'll have to download python 2 on my PC as well?

Here is the code I'll more than likely end up using with some changes, would it I be able to send information from this python 3 script the a python 2 script:

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]

the code is from a P2P client I made a while back that also uses a base GUI engine I made with Pygame.

Upvotes: 0

Views: 215

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1125128

For network communication it won't matter what language either side is written in.

All your code does is send bytes over the network. As long as the other side can process bytes and send back other bytes, you are fine. Python 2 is perfectly capable of doing that.

Upvotes: 2

Related Questions