After changing from Python 2.7 to Python 3.7 data getting an additional letter?

Viewed 64

I'm working on a program that receives a string from an Android app sent through WiFi, the program was originally written for Python 2.7, but after adding some additional functionalities I changed it to Python 3.7. However, after making that change, my data had an extra letter at the front and for the life of me I can't figure out why that is.

Here's a snippet of my code, it's a really simple if statement to see which command was sent from the Android app and controls Raspberry Pi (4) cam (v.2) with the command.

This part sets up the connections and wait to see which command I send.

isoCmd = ['auto','100','200','300','400','500','640','800']

HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)
brightness = 50
timelapse = 0


tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
        print ('Waiting for connection')
        tcpCliSock,addr = tcpSerSock.accept()
        try:
                while True:
                        data = ''
                        brightness = ' '
                        data = tcpCliSock.recv(BUFSIZE)
                        dataStr = str(data[1:])
                        print ("Here's data ",dataStr)
                        if not data:
                                break
                        if data in isoCmd:
                                if data == "auto":
                                        camera.iso = 0
                                        print ('ISO: Auto')
                                else:
                                        camera.iso = int(data)
                                        print ('ISO: '), data

When I start the program this is what I see:

Waiting for connection
#If I send command '300'
Here's data b'300'
Here's data b''
Waiting for connection

I'm not sure why there's the extra b'' is coming from. I have tested the code by just adding the "b" at the beginning of each items in the array which worked for any commands that I defined, not for any commands to control the Pi camera since well, there's no extra b at the beginning. (Did that make sense?) My point is, I know I'm able to send commands no problem, just not sure how to get rid of the extra letter. If anyone could give me some advice that would be great. Thanks for helping.

2 Answers

Byte strings are represented by the b-prefix. Although you can see the string in output on printing, inherently they are bytes.

To get a normal string out of it, decode function can help.

dataStr.decode("utf-8")

b'data' simply means the data inside quotes has been received in bytes form, as mentioned in other answers also, you have to decode that with decode('utf-8') to get it in string form.

I have updated your program below, to be compatible for v3.7+

from socket import *
isoCmd = ['auto','100','200','300','400','500','640','800']

HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)
brightness = 50
timelapse = 0

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
    print ('Waiting for connection')
    tcpCliSock,addr = tcpSerSock.accept()
    try:
        while True:
            data = ''
            brightness = ' '
            data = tcpCliSock.recv(BUFSIZE).decode('utf-8')
            print ("Here's data "+data)
            if not data:
                break
            if data in isoCmd:
                if data == "auto":
                    camera.iso = 0
                    print ('ISO: Auto')
                else:
                    camera.iso = int(data)
                    print ('ISO: '+ data)
    except Exception as e:
        print(e)
                    
Related