Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #34450
    Bartosz
    Participant

    Hi there πŸ™‚

    Sorry for making the message long, I thought a little background could help pinpointing where I lack the knowledge. I placed numbers before each question, I hope it will make it easier to answer…

    I decided to make another backdoor, after the first one was successfully working.
    Mainly as a challenge, as I wanted to restrain myself from watching the lectures this time. Only using documentation and google for guidance.
    But also I wanted to make the code work in Python3.
    I learned that socket.send() in Python3 sends bytes, and not a string (like Python 2).
    I experimented with encoding, learned a little about utf-8 and so, must say I’m a little overwhelmed, but I don’t expect you to explain all that here πŸ˜€

    A solution I used was to ‘.encode()’ the data sent and ‘.decode()’ the data received.
    1. I don’t know if that’s an elegant solution (would love your thoughts on that as well), but it worked.

    But than comes the next step: using JSON to make the data well defined.
    I just can’t wrap my head around the concept of encoding while also converting the data to JSON…
    2. Is the encoding still necessary? if so, when, during the process?
    3. In which parts of the code do I need the data to be a string, in which bytes-like object?
    4. It would be just perfect, if you could provide a short explanation to how the string-bytes mechanism works. Maybe from the “packet POV”: when is it a string, when is it bytes again? And when in all that is JSON?

    I’m really sorry for asking, I know this technically isn’t a “lecture-content” problem.
    Also, I’m aware that with enough focus and reading documentation I could figure it out…
    I’m just stuck for so long I’m starting to lose my enthusiasm, and I really don’t wanna lose it πŸ˜‰

    thank you very much for great courses and awesome support!
    If you decide to ignore this question, I will understand πŸ™‚
    cheers!

    #34454
    Bartosz
    Participant

    Ok, I figured it out πŸ™‚
    For anyone interested, here goes the solution that worked for me:

    #####
    # json.dumps() takes a string, returns a string
    # json.loads() takes a string, bytes or bytearray, returns string
    # .encode() takes a string, return bytes
    # .decode() takes bytes, returns a string
    #####

    so the simplified working version (just one message from host to client); This is the code after establishing connection:
    <<HOST>>
    msg = input(“>> “) #(msg is a STRING)
    msg_json = json.dumps(msg) #msg_json is a (serialized) STRING
    connection.send(msg_json.encode()) #since the socket.send() in Python3 requires bytes, we use the .encode() function –> sending BYTES
    <<CLIENT>>
    msg_json_encoded = connection.recv(1024) #we receive the json-formatted BYTES
    ***msg_json_encoded.decode()*** – this step is not necessary, explained below*
    msg = json.loads(msg_json_encoded) #jason.loads() does all the magic
    print(msg)

    * This is what json.loads() does: “Deserialize s (a str, bytes or bytearray instance containing a JSON document) to a Python object” Therefore it can handle bytes, we don’t need to use .decode() first. Learned that by accident πŸ˜€

    Ok, so not only does it work, it’s actually not that complicated πŸ˜€
    Still, if I may, I would like to know if it’s “right”?
    Meaning, is there a more elegant/efficient way of dealing with json and sockets in Python3?

    Thanks, sorry again for all the trouble!

    #34507
    Vashisht Boodhun
    Participant

    Great stuff! Gald you figured it out;)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.