what is going on everybody and welcome
to another sockets with Python tutorial
video in this video we're gonna be
talking about pickles not the food but
instead the serialization in Python so
real quick if you don't know what
pickling is in Python it is the
serialization or if I think we can call
that flattening as well and there's
probably other terms for it what does
that mean we're converting it to bytes
what are we doing with bytes with
sockets we're sending them so putting
that all together it means we can send
and receive things we've pickled in
Python via sockets now what can we
pickle in Python objects and what's an
object in Python everything so we can
pickle pretty much everything in Python
and basically what this means for us
with sockets is that we can send and
receive these Python objects so anything
that we're doing in a Python script we
can send and receive that you know
whatever it might be maybe you're
training a machine learning model and
you want to share that model amongst
many machines or you've got some sort of
shared database or you know who knows
what what exactly you're doing just you
know that you can send and receive that
information with Python so any object
you have in Python any data structure
whatever it you can serialize it with
pickle and then send it so that's what
we're going to be doing here so if you
don't know any if you already know
things about pickle don't worry we're
gonna send and receive exactly what I'm
gonna build here and if you don't know
anything about pickle it's pretty simple
so we're gonna go import pickle and just
for the record you can also save pickles
as files so you don't only transmit
pickle data like between two programs
many times you're actually just going to
save that Python object using pickle
anyway so the first thing we want to do
is just come up with some sort of
example of a thing that we're gonna save
so I'm gonna say it's just we're just
gonna use a dictionary for now just so
anything or some arbitrary Python object
will do so we'll just say hey there okay
that's the object we want to send but
again it doesn't have to be a dictionary
and also if you were doing a dictionary
you also could just use like JSON or
something
to you convert it to a string send it as
bytes convert it back out use JSON you'd
be good to go
so you don't actually you wouldn't use
pickle for probably a dictionary but
again you can use pickle for anything
whereas like JSON is one structure you
wouldn't be able to do everything with
JSON so okay so that's the thing that we
want a pickle so how do we do that well
we're gonna just say message the message
we want to receive later is gonna be
pickle dot dump s D and we can just
print message real quickly to see okay
here's what we've got basically we do
have the string data that we can see
here we also I don't see the number but
it's probably there somewhere anyway and
then we've got some gobbledygook that is
the information that is contained within
the actual structuring of what a
dictionary is and how we can interact
with it and all that cool so now we want
to send this information so let's go
ahead and do that so I'm gonna cut that
come over here come down here or fix
that okay get rid of this loop we don't
we're not gonna loop anymore and the
message now let me just get rid of the
first one paste this one in so now our
message is that pickle dump but we also
we still want to use that header again
because the thing we pickle might very
easily exceed our buffer size so we need
to be able to handle for that so the way
that we're gonna do that is actually
going to be will convert this cuz this
is already bytes right so we just need
to convert this to bytes so by it's that
and then what kind of bytes you
utf-8 bytes okay so that's the message
that we want to send we no longer have
to convert it here to bytes because it's
already bytes and we're good to go on
the server now we just need to fix a few
things on the client side the first
thing would be the full message here
should be bytes then the information
that we want to receive should be pretty
much all the same the only thing I
wouldn't do like no longer are we going
to decode utf-8 it's just going to be
message when the full message is
received we're going to unpick alit so
let's go ahead and import
Pikul come down here
so full message received here it is but
first we need to load it in so we're
just gonna say copy that we're gonna say
D equals pickle dot load s that thing
and then let's print D save that come
over here and now let's run these two
things Python server dot pi good to go
and finally Python client PI so full
message receive this is what it looks
like to us because that also contains
actually I can't remember if we trimmed
off the header anyway phone message
received
looks something like that once we've
loaded it with pickle there's our actual
Python object that we sent using pickles
so super cool if that doesn't excite you
you are either not nerdy enough or you
just don't understand but because now
you can send literally anything in
Python that you're working with so if
you got some script it's doing something
you've got two scripts that can share
this data between each other either
locally or remotely so that's freaking
awesome speaking of awesome quick shout
out to my most recent channel members is
a bay a shower you are amazing your real
grow shonkoff just just incredible
henry damina awesome and sundar balaji
spectacular thank you guys very much for
your support you guys are all awesome so
that's it for now
in the next tutorial I think we're gonna
wrap everything up with a an example
with a chatbot or not Shep hoes want to
say a chatbot any time I say chat I
wonder if I've made that mistake so far
in this series anyways probably anyway I
want to make a chat server so that'll
kind of bring everything together and
allow us to show one more concept that
we haven't yet shown select and yeah
that'll be pretty cool so anyway
questions comments concerns whatever
feel free to them below you can also
join the discord discord GG / centex if
you want to support the channel you can
click that beautiful blue
join a button that just beckons for you
to click on it otherwise I'll see you
guys another video