In this video, we're going to be
looking at how to create a TCP server. Now, of course, I
mentioned that we would be starting off with creating
sockets. But it came to my understanding that if you really
want to understand how sockets work with Python, then you need
to implement them into a simple example. And of course, this
might not be very simple, but creating a TCP server and
client, which is what we're going to be doing, but in this
video, we'll be focusing on creating the server. In the next
video, we'll be looking at creating the client, and then
we'll we'll I'll show you that they actually do communicate
with each other. Alright, so we'll be we'll be completing the
the connection or the communication. Now, before we
get started, I just want to say that I am using Windows, but
nonetheless, I'm still using Visual Studio code. I've installed the Python
extension by Microsoft, and we're ready to go. Now, as I
said, I wanted to start off with sockets. Now, the problem, as I said,
with sockets is they're a very important thing when it comes
down to penetration testing with Python. And that's because they
initialize connections, and they allow the sending and receiving
of data. So if you might be a little bit confused, or you have
no idea of what a socket is, don't worry, I'm going to
explain them to you in both a technical and a simple
definition. So in terms of technicality, a socket is an
internal endpoint for sending and receiving data. Alright, now
if I was to explain it, simply, we can use the analogy of the
widget was named a socket is like outlet, alright, that
essentially gives you connection of power that gives you a
connection have power from your transformer from the grid, you
know, to the transformer to your house, and then you can finally
use the power. The only thing about that analogy is that you
cannot send power through the socket, you only receive it, the
thing about a network socket it allows is it allows you to send
and receive data. Alright, so it's an internal endpoint, which
means it works locally. So you know, we we've talked about
ports and how they work. So that's how it works. All right.
Now, the the socket module or the socket library in the Python
standard library is a fantastic module and allows you to
implement this connection or allows you to implement sockets
using different protocols. So for example, we are going to be
looking at TCP but we also have UDP, which is awesome. Okay, so
when we talk about when we talk about sockets, in Python, we
have the socket module. Now the socket module for you to utilize
it is you need to import it. And then finally, to use it in your
code, you need to call the socket function, which is
denoted by socket dot socket, as you probably know, but if you
don't know, we'll be looking at it as we create the TCP server.
Okay, so we are going to be we're going to be calling we're
going to be importing the socket module, and then we need to use
the socket function that is available in the socket module.
And that also has a very specific syntax or syntax that
not many people understand. And that's what I want to clear up
here. The thing about Python is we need to create an object, the
object will then call the socket function. Now in that socket
function, you have parameters that you need to specify, for
example, we have the socket family, the socket family is
usually specified in the form of asi net, which have heard of,
and I'll explain what that is. And then you have your socket
type, which is specified by sock stream or sock diagram to
specify the two of the most important ones. And those are
the two parameters that you need to specify. Okay, especially
with Python three. So let me explain what a fi net is used
for in terms of socket family. So in specifying socket family
usually see that we use socket dot sock, sock stream or sock,
or the asi net now we start off with a fi net, a fi net is is
used to specify the protocol that will be used for
communication. You might be wondering, Well, what are you
talking about? We're using TCP or UDP. What other protocols are
you talking about? The protocol I'm talking about is either ipv4
or ipv6. As you know, the Transmission Control Protocol
has different ways of routing data through network. So we'll
be using ipv4 which is denoted by a fit net, we then have the
socket type which is denoted by the sock stream or sock de gram.
Now, these depend on the type of connections that you will be
using in terms of protocols. So for example, sock stream will
mean you're specifying a TCP as your will mean you're specifying
connection oriented protocols like TCP. What do I mean by this
TCP is a connection oriented protocol that essentially
involves you performing or performing a three way handshake
to initialize connection with a server and a client. Okay, so the other you know, connection,
the connection less protocols like UDP, you would use the
socket type of sock diagram. Okay, so hope I'm not confusing
here. We'll be looking at how this applies practically in a
few seconds. All right, so let's move on. Now to the the
functions or the methods that you will be using. So the some
of the most common ones we'll be using are the the bind method,
which will allow us to bind the host and the port to a socket,
we then have the listen function or method that will allow us to
start the TCP listener, we have the Accept method, which will
allow us to accept the incoming TCP client information, we then
have the most common ones, which you probably know. But if you
don't, don't worry, we then have the Send function, which allows
us to send a TCP message or a message over TCP, we then have
the Send, we then have the receive method that allows us to
receive TCP messages or a message over TCP. And then we
have the S mean sorry, the close method, which allows us to close
the socket, and therefore ending the connection, as you probably
guessed. Alright, so we'll be using all of those methods, and
I'll be explaining them as we move on. So we will be creating
a simple TCP server. So let's get started with that. So I'm
using Visual Studio code, I'm going to create a new file and
I'm going to save this on my desktop right here. And I'm
going to call it server dot p y. So we are using Python three. So
if you're running this on Linux eight, we specify the the
working directory, which if you're on Linux is user bin. And
then if you're using Python two, you'd specify the environment
which is Python two, and then but in this case, we're using
Python three. Alright, so that's what you do. Now this doesn't
matter on Windows, it will automatically use Python three,
because that's what have installed. So now we need to
import the socket module. Alright, so socket. And as you
can see, we've imported it really simple. I love Python.
Now we need to create the object to the socket object. Now many
people like using the s, the thing about using the S is it,
it kind of confuses you as to what's going on. So I'm gonna
call this server socket. Alright, you can call it server
socket object if you want to, but I'm going to keep it simple.
That's the object. So now we are calling the socket function. So
we'll say socket, dot socket, like so socket dot socket, and
now in here we would specify like so you can open the
brackets if you want to others like doing it in the same line.
Now we need to specify the we need to specify the socket, the
socket function, and the socket type, the socket, the socket
family and the socket types are not the function we've already
specified the socket function so now we would say socket.af I net
that means we're using ipv4 and then we need to specify the type
Alright, so socket Sorry about that. Got a little bit confused
there. My keyboard is not even in front of me. So socket dot
sock stream. Alright, that essentially tells us what what type of communication we're
going to be doing it what through what protocol TCP or UDP
in this case, we are using a connection connection based
protocol. Again, in this case, TCP. So now that we have
specified that, we now need to create another object that will
store the we need to essentially store the hostname, which will
use the get hostname. We'll be using the get hostname function
from that's another function that I didn't specify earlier,
but I'll explain what it does. So we will say as host, we're
getting the host information. So we're getting the host name. So
socket, dot host name. And again, actually, it's not
hostname it get host name. The thing about this is this is a
function that will get the host name so it will get the address
and, and any other information that we specify, we'll get to
this in a second. We then need to specify the port that we'll
be listening on now this you can specify to whatever you want,
I'm going to set mine on. If you're on Windows, it's going to
ask you for your firewall permissions to make sure to to
allow that to to to allow you to listen on that port. So make
sure you are accepted with administrator privileges. So I'm
going to set the port to something like 444 usually like
that, I don't really know and a lot of you guys have pointed
that out. Alright, so now we need to use the get, we need to
we have we have got the the hostname we now need to bind, we
need to bind the object, we need to bind the values that we got
back which are the host and port but we need to bind them to the
object that we created, which is the server socket. So right so
we're going to say server socket. And now we need to we
need to bind it so we're gonna say dot bind. And this
essentially means we're going to be binding the the addresses
that we got which is the host and the port to a socket and the
socket we have already specified as the socket object which you
have created. Alright, so bind and what are we binding in here
we are binding the host and the port. Alright, so we have
specified the host, as you can see here, and we've got the port
right there. So that's working out quite well. Everything makes
sense. Now we need to listen for TCP. We need to set up a TCP
listener listen for TCP connections from the client. So
we need to set up a listener. So server socket dot listen.
Alright. And of course, you can add comments to the code, if you
want to, once I'll upload this to GitHub, by the way, the link
will be in the description, it should have documentation as to
what's going on. Alright, so we need to listen and you know, in
in the parameters here, we can specify how many connections we
can listen to at a time. So a lot of people actually forget
this. And they try and connect from various computers, whilst
one has already initialized the connection. So if you want to
connect to this server from many computers, you can specify the
amount so I can specify three, for example, and you can specify
as many as you want, there. Alright, so now that we have
specified, we will specify the listener and how many
connections or how many requests we should have, or how many
requests to allow at a given time, when we can now use a
while loop here. So we're going to say while and we could have
said we could have performed, you know, added this into a loop
at the beginning, but I was going through this and I decided
that we can specify an option your while all of this is true.
All right, when we get all the connection information, we can
then say, we can then say establish the connection.
Alright, so we create another object, yes, we say client
socket, which we'll be using in the next video client socket.
And the address, or we can specify also another one
address, this is going to be equal to the server socket,
which is the server socket object. And then remember, I
said we're going to be using the, the server socket methods
or functions like the Accept function, which will allow the
the TCP connection so we'll say server socket, dot accept,
alright, and this will accept the TCP information coming from
the client. All right, now we can, we can print in some data
here. So we'll say print. And we can say for example, we can
notify the server that we have gotten a connection. So I've
said, receive connection from and then we can use
concatenation. So receive connection from, we can say,
let's see received connection from the we got the we got the
address, which is we need to convert it into a string. So we
can say we can concatenate this and say sorry about that, we got
the string and we're going to convert that that is the
address. All right, we're going to convert it into a string,
because the client socket information will still be in
terms of the host, the address needs to be saved as well.
Alright, so we've converted that into a string. Awesome. Now we
need to create a, we need to create a message and we can say
message is equal to, let's see, we can say thank you for
connecting you for connecting to the server. Or we can say we can
also add something a little welcoming, so hello, or
something really stupid like that. I'm not really thinking
this through in terms of the amount of information I'm trying
to add here. We can then also start concatenation here. And we
can we can then specify. Let's see, do we need to move to the
next line? Yes. So we would specify now that we want to move
back, we want to go to the next line. So Rn, alright, and Oops,
sorry about that. My keyboard is so far away from me. It's
unbelievable at this point. Alright, so we specify that now,
we need, we cannot decide whether we want to encode the
message in terms of types of encoding, but I'm not going to
do that because I don't want to overcomplicate you guys. So now
we can close, we can close the the socket that we created,
which was with the client socket, which again, is not
related to the server socket, so we can say, client socket, and
we can say dot close. Alright, so this is going to close the
connection while any of this information is true. All right,
so that is how to create a TCP server in Python three. Now, of
course, I would like to think that I've explained everything
correctly. But if you have queries, as to any, any, you
know, for any part of this or you have any questions or
suggestions, just let me know. But if you have any problems,
always just let me know in the comment section or if you want
to contact me privately, contact me through the website. Now this
will be on GitHub. And don't worry, I'll add the
documentation so you can understand what's going on. So
let's just go over what we've done. So was important the
socket module from the from the Python standard library, we then
created a server socket object which then called the socket
function, and we we specified the socket type and the socket
family sorry, the socket family and the socket type within you
know, we then give the socket load get by hostname we but we
give that value to host and we also specify the value for port
which is a Fourth of all you can specify whatever you want. The
great thing about Python is you don't have to specify data
types, which is awesome. Right, we then had to use this, we use
the server socket object and we bind it that are we essentially
binded, the address, which is host and port to the socket, we
then set up a TCP listener here, which would listen for
connections up to a maximum of three. And then while all of
this is true, while all of this is true, you could see we have
we created two more, we said, the client socket and the
address are going to be equal to server socket dot accept, that's
going to be the information that we accepted from, from the
client. And then we're going to print received connection from,
and we specify that we want to convert it into a string, we
want to convert the address into a string. And then the message
we specified was going to be Hello, thank you for connecting
to the server. And we closed, we closed the socket. Again, I know
a lot of you might have noticed a mistake I made here. And that
is I did not send any data back. And you might be wondering to
yourself, Well, what exactly happened here? What do you mean,
you haven't sent anything or you haven't sent anything? Well, you
can see that we gave the message a value, but we didn't actually
send it. And you remember I talked about the Send function.
Alright, so we need to specify that we are sending this
information. So we'll say before we close it, we want to let the
client know that they've successfully connected to the
server. Alright, so we'll say client socket, and that's going
to be we'll use the Send function. And in here, we
specify the value, which is again, we give the value of
message there. And we're going to be using message. So this is
what will be sent to the client once they connect to the server.
Awesome. So that was a little bit of an example as to what I
was saying it gets really complicated when you're
initializing the connection. And we'll also be looking at
receiving data back which is also very important. So
hopefully, this was a good introduction into using sockets.
Again, as I said, I don't expect you to, to understand everything
if you're a beginner. And I would I would expect you to have
understood Python three, at least the basics till now. But
we'll be covering the advanced stuff. As you can see, it is not
really beautiful code. I mean, it's quite ugly. So you know,
and I really believe in the Zen of Python. So I'll improve this
up and I'll upload it to GitHub.