+ 1

Pack and Unpack Data - Python Networking

I wish to transfer data through sockets between client and a server. in order to transfer the data I need to encode it and then decode it when it reaches the server, and vice versa. I think i should use the 'struct' python module that packs the data together amd encodes it. im struggling to pack multiple string and then unpack them. (I think that i should also pack an integer the represents the length of all the data, im not sure...) I'd like to have an explaination of how to do this properly! How to transfer the data properly through the sockets and then unpack the data? Thanks.

27th Jul 2021, 1:17 PM
Yahel
Yahel - avatar
7 Antworten
+ 2
Haven't worked with strings too often in packing, but i know you can pack characters with 'c' and strings with 's'. You'd basically take the three strings and stick em each in a variable. Have a max and/or minumum number of chars, then make another variable to pack em all in. I'd try several ways cause this is where it's just trial and error. data = struct.pack("10c25c25c", comm, user, pass_) ^^^ something like that, using string could be easier. But then you'd take that "10c25c25c" string (or whatever it ends up being) and plug it into struct.calcsize() like total_bytes_sent = struct.calcsize("10c25c25c") and you can send that to the server or just have the server take the max calcsize you want, and plug it in
27th Jul 2021, 2:15 PM
Slick
Slick - avatar
+ 1
What are you trying to pack? The only reason i use struct.pack/unpack is when decoding incoming packets to strip the information from the headers. # You can encode any string by using: str.encode(<string>) # To decode strings: <string>.decode() Here is an example of unpacking in an extremly simple network sniffer https://code.sololearn.com/cfPnBLFZmYZm/?ref=app
27th Jul 2021, 1:36 PM
Slick
Slick - avatar
+ 1
Slick I get what you did, but that's not exactly my problem... My goal is to transfer multiple strings (command, username, password), and that the server will know how many bytes to recv(and not just hard-code 1024 bytes to recv..). The struct.pack packs all the Strings and sends them over together, and when unpacking them - they Separate..
27th Jul 2021, 1:57 PM
Yahel
Yahel - avatar
0
Slick assume the client needs to send the server his username and password. The message consists of the command: [LOGIN]. the username: Man1 and the password: man123 How can I send all of this data to the server properly? I thought using the struct module... Please show me the way to do this..
27th Jul 2021, 1:39 PM
Yahel
Yahel - avatar
0
Oh noooooo! I typed out this whole thing and lost connection now it's gone. I'll just post a couple simple codes and if you have any issues, ask away https://code.sololearn.com/cZaV0a70nS9d/?ref=app https://code.sololearn.com/c24JOGY28PP7/?ref=app
27th Jul 2021, 1:49 PM
Slick
Slick - avatar
0
Slick 😂😪
27th Jul 2021, 1:51 PM
Yahel
Yahel - avatar
0
I'm a little ticked rn, but no worries haha. Basically, what you're talking about is sending unencrypted strings of data. Here's the thing, once you encode, send, and decode the string, it's just a string again. You can just use regular python to keep hold of the usernames and passwords (in a list of lists mabey?) And you can just look through it w/ indexing and whatnot. Just make sure to save the strings you get from each in variables to make it easier to work with
27th Jul 2021, 1:56 PM
Slick
Slick - avatar