- This topic has 2 replies, 2 voices, and was last updated 4 years, 8 months ago by Vashisht Boodhun.
- AuthorPosts
- April 19, 2020 at 7:50 pm #34450BartoszParticipant
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!April 19, 2020 at 10:44 pm #34454BartoszParticipantOk, 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!
April 20, 2020 at 12:57 pm #34507Vashisht BoodhunParticipantGreat stuff! Gald you figured it out;)
- AuthorPosts
- You must be logged in to reply to this topic.