How to Train Your Python: Part 22, Beginning the Socket Module

Part 22, Beginning the Socket Module

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!

Step 1: Establishing a Client Socket

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.

Step 2: Connecting to a Server

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.

Step 3: Sending and Receiving Data

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?

Wrapping It Up

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!

-Defalt

5 Comments

It would be helpful if you also talk a little about the TCP and UDP options when creating the socket object.- Just a suggestion

Scriptkiddie55

Excellent suggestion, will do.

-Defalt

A really interesting application for this lesson would be to receive data from the server and interpret it. Maybe run commands on the computer when certain data is received. You know where I am going with this. I am working on a portable password cracking script which would run during startup from a live-usb, listen for connections and run hashcat with the parameters provided. Copy this is some usb(s) and you just have to plug them in computers on lan. Voila, your password cracking cluster is ready!

Is there a web site or pdf that you recommend us to learn these modules? Since you are writing briefly about them, I didn't understand how to use socket module very well. I want to learn them very well but I couldn't find a nice source.

I dind't understand this tutorial.
When i print response i get a "bad request error".
What's AFInET and SOCKSTREAM?
What does that request do, exactly?
Why does s.send(request) show 67?
What does recv do and why did you put a 1024 in it?

Actually, maybe i don't understand any of that because i know nothing about connections and stuff (hell, i'm not even sure what a port is). So could you point me to a place where i could learn about these things?

Sorry for these questions, but i'm a complete begginer, and at this point the tutorials have become one of those that assume that people already know about other things.

Share Your Thoughts

  • Hot
  • Latest