Python Sockets Bind vs Connect

R. Eric Kiser
2 min readJan 11, 2023

--

R. Eric Kiser

A fellow nerd buddy of mine once told me how much he wanted to “know” his new lady friend. I laughed and responded with some simple advice. Teddy, before you can make a connection you have to offer her a binding agreement. (Clap if you get this)

I have created several successful python connections for penetration testing in my career. However, I still get asked to create a “trojan” or “backdoor” by would be scrip kiddies. I shrug my shoulders as each person has their method for obtaining information and this is a rather simple task if by reading some Python documentation. Still building out tool in different ways is a fun task. The key to a good “backdoor” is knowing how to use the socket module in Python. The interesting thing is this usually is allowed on most networks as it is a legitimate way of connecting two systems. However, it gets tricky if you are connecting outside the network. I find a HTTP request module to a form is best for collecting data.

In this article I want to quickly break down (mostly for a linked reference for those who ask me) the difference between the server socket bind and the client socket connect.

In Python, the socket module provides low-level core networking services. It allows you to create network sockets and work with them in a manner similar to the Berkeley sockets API.

socket.bind() is used to bind a socket to a specific address and port. This is typically used for servers, where the socket needs to be bound to a specific address and port so that clients can connect to it.

bind() is used to bind the socket to a specific address and port on the host machine. This is done so that the socket can listen for incoming connection requests on that specific address and port. For example, in a server application, the socket can be bound to the localhost address (127.0.0.1) and port 8000. This means that the server is listening for incoming connection requests on the localhost address and port 8000.

On the other hand socket.connect() is used by a client to initiate a connection to a server. After a connection has been established, the client can send data to the server using send() method and the server can receive data from the client using recv() method.

So in summary, bind() is used to bind a socket to a specific address and port, making the socket listen for incoming connection requests on that address and port, while connect() is used by a client to initiate a connection to a server.

--

--

R. Eric Kiser

R. Eric Kiser is highly skilled certified information security manager with 10+ years of experience in the field.