Welcome back everyone! It's been awhile hasn't it? Sorry for being so quiet, but my CCNA courses have really picked up recently. In the last article we covered how to import modules and how we can use them. In this article, we'll actually be covering a module that is essential to the hacking aspect of Python, sockets.
Essentially, the socket module allows us to make, maintain, and use connections. We can attempt a connection to any port we want, to any address we want. We can also send information back and forth using these connections. In this article, we're going to establish a client connection and send/receive some data!
Now, when I say "We're going to be the client", I mean that we are going to request and initiate the connection. First things first, we need to import the socket module, then we're going to make a socket object. We're going to import the entire module, so whenever we call something out of it, we need to precede it with "socket". Let's perform these actions now:
There we go, we've made a socket object. It may look a bit strange, but if you think about it then it makes perfect sense. We called the socket class out of the socket module, hence the "socket.socket". We gave this class some parameters that we also pulled out of the socket module, there are a multitude of parameters for socket, but these are the ones you'll see most often.
Now that we have our socket, we can connect it to a server, we're going to make a connection to our home right here at Null Byte! In order to connect our socket, we need to use the connect method, when we use this method, we need to give the IP or domain name of the server, followed by the port number. Both of these need to reside in a single tuple. Let's connect our socket now:
Now that our socket is connected, we can send and receive data through it.
Now we've connected our socket on to Null Byte on port 80. When we make client connections, the source port number is randomly chosen by the socket, so we don't have to worry about that! Now that we've connected to Null Byte, let's go ahead and send an HTTP GET request. Let's request the Null Byte home page. We can send the request using the send method, and we can receive the response using the recv method. Let's send/receive this information now:
There we go! We're able to send and HTTP GET request and receive the response! Let's wrap things up shall we?
Today we learned the very basics of the socket module. These basics include how to make a client socket, how to make a connection with that socket, and how to send/receive data through that socket. There is still much more to cover, but we'll finish in the next article.
As a small announcement, I've decided that we're not going to cover every inch of every module. We're only going to cover their most used functions and uses.
Thank you for reading!
Want to start making money as a white hat hacker? Jump-start your hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from cybersecurity professionals.
Other worthwhile deals to check out: