Python Forum
Python 2.7 vs Python 3.8 (Socket Module)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 2.7 vs Python 3.8 (Socket Module)
#1
Hello. I have developed a program in Python 3.8 that uses socket to communicate. It works great with 3.8. It will return data back in hex like it should. See below.

I have found python 3.8 socket module returns in bytes where python 2.7 return a string.

I am stuck using python 2.7 because the software I am using on the project requires it. I have tried to convert it many ways and I have had no luck. Any help will be appreciated.


result from Python 3.8 (Correct)
############################################################################
sending "43 41 4E 6F 65 46 44 58 01 01 01 00 00 80 00 00 06 00 06 00 10 A0"
waiting to receive
received "b'CANoeFDX\x01\x02\x02\x00\x00\x80\x00\x00\x10\x00\x04\x00\x03\x00\x00\x00\xe3\x1e\xc4\xdb\xd9\xc1\x03\x00\t\x00\x05\x00\x10\xa0\x01\x00\x03'"
closing socket
################################################################################
result from Python 2.7 (Not Correct Response)
#############################################################################
sending "43 41 4E 6F 65 46 44 58 01 01 01 00 00 80 00 00 06 00 06 00 10 A0"
waiting to receive
received "CANoeFDX Ç â£ÿI┬ á "
closing socket
##############################################################################

 #Python 3.8
def Program1():#A010 Shifter Position
    def readResponse(): 
        try:
            # Send data
            print ('sending "%s"' % readVector)
            sent = sock.sendto(y, ServerAddress)
            # Receive response
            print ('waiting to receive')
            # data, server = sock.recvfrom(1024)
            data, server = sock.recvfrom(1024)
            print ('received "%s"' % data)

        finally:
            print ('closing socket')
            # vectorResponse = binascii.hexlify(bytearray(data)) # spits data same as 2.7 but not in byte form
            vectorResponse = data
            # vectorResponse = data # Returns the correct bytes string in hex form in 3.8
            sock.close()
        # responsToPLC = vectorResponse[40:41]
        responsToPLC = vectorResponse
        print (responsToPLC)
        return responsToPLC

    ####---------Read from Vector Box----------#### 
    
    readVector = '43 41 4E 6F 65 46 44 58 01 01 01 00 00 80 00 00 06 00 06 00 10 A0' 
    y = bytearray().fromhex(readVector)

    ####----Below is logic for OUTPUT from Vector Box formatted----####
    readResponse()
Reply


Messages In This Thread
Python 2.7 vs Python 3.8 (Socket Module) - by MattB - Mar-16-2020, 07:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python socket connect only sometimes espDino 0 1,583 Jul-15-2020, 12:13 PM
Last Post: espDino
  how to get your own ip using python (NO SOCKET:) ) julio2000 3 2,402 Mar-02-2020, 09:35 PM
Last Post: buran
  Python module wifi not showing all wireless networks marozsas 3 4,116 Jan-24-2020, 02:59 PM
Last Post: buran
  For Xilinx EthernetLite LWIP:Need help in Python SOCKET Coding Saras 1 3,054 Oct-01-2018, 05:16 AM
Last Post: Saras
  Python Socket programming with packets sourabhjaiswal92 1 4,269 Sep-18-2018, 06:24 AM
Last Post: martingever
  Send data BMP180 between client and server trought module socket smalhao 0 2,936 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Python socket : Error receive data quanglnh1993 1 13,158 Mar-14-2018, 11:59 AM
Last Post: avorane
  Python module for Router configuration parsing anna 0 3,707 Mar-08-2018, 06:02 PM
Last Post: anna
  Python // C # - SOCKET connection is constantly interrupted raspberryPiBRA 0 2,470 Feb-01-2018, 09:53 AM
Last Post: raspberryPiBRA

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020