3

My Python script is as below (sensitive data removed where necessary)

import socket

class TCPConnection:
    def __init__(self, sock=None):
        if sock is None:
            self.sock = socket.socket(
                            socket.AF_INET, socket.SOCK_STREAM)
        else:
            self.sock = sock

    def connect(self, host, port):
        try:
            self.sock.connect((host, port))
            print('Successful Connection')
        except:
            print('Connection Failed')

    def readlines(self):
        data = self.sock.recv(1024)
        print(data)


if __name__ == '__main__' : 
    listen = TCPConnection()
    listen.connect('**.**.**.**',##)
    listen.readlines()

I'm getting the following output.

Successful Connection
b'\x01\x00\xc7\x00\x08\x01\x07\t\x00\x00\x00\x03='

What do I now need to do to make the printed output from the readlines() function more friendly? From the look of it, I know it's in bytes, but beyond that, I'm lost.

All help is gracefully received.

oliversarfas
  • 86
  • 1
  • 9
  • 1
    did you tried using .decode() ? data = self.sock.recv(1024).decode() – Gabriel Ciubotaru May 12 '15 at 13:32
  • hi Gabriel, already tried that, I'm getting `'utf-8' codec can't decode byte 0xcd in position 2: invalid continuation byte`as my error message – oliversarfas May 12 '15 at 13:33
  • 1
    indeeed is not utf-8 string. What this string represent and what exactly do you mean by "frendly" ? – Gabriel Ciubotaru May 12 '15 at 13:36
  • I just want the data in a readable manner, not byte code. ASCII would be ideal – oliversarfas May 12 '15 at 13:37
  • Are you being sent ASCII data? – Eric Renouf May 12 '15 at 13:38
  • That's a little impossible. You have non ASCII characters. Only values between 0x20 and 0x7f are printable (http://www.asciitable.com/). You have a lot of non printalbe characters. I'm pretty sure that bytes are part of a protocol or smth – Gabriel Ciubotaru May 12 '15 at 13:40
  • I'll be honest, I have no idea what the data coming out of the socket is. I'm just guessing for the most part – oliversarfas May 12 '15 at 13:53
  • Unless you give me some "hints" about the protocol or what server provide, i cannot guess it. It could be anything: a structure, an encrypted message, a hand-shake .... The message do not contain any common header – Gabriel Ciubotaru May 12 '15 at 14:09
  • I've asked the development team of the server I'm hitting, and have now resolved the issue. Our local network was not whitelisted with their services and was thus returning encrypted messages and not fully data. – oliversarfas May 12 '15 at 14:11
  • If you would like to post your decode() as an answer, I'll happily accept that for your profile as I did use it in my final implimentation – oliversarfas May 12 '15 at 14:13

1 Answers1

2

You can decode the message before printing it:

def readlines(self):
    data = self.sock.recv(1024).decode()
    print(data)

but still please consider that your data is not "ASCII" or "UTF-8".

Gabriel Ciubotaru
  • 1,042
  • 9
  • 22