You are on page 1of 4

instructables

Netcat in Python

by swarajdh

What is netcat? The manual page for netcat says the In essence, netcat allows you to connect to other
following: "the nc (or netcat) utility is used for just servers using the TCP or UDP protocol. TCP stands
about anything under the sun involving TCP, UDP, or for Transmission Control Protocol, and is connection
UNIX-domain sockets. It can open TCP connections, oriented. UDP stands for Universal Datagram
send UDP packets, listen on arbitrary TCP and UDP Protocol, and is connectionless. TCP is commonly
ports, do port scanning, and deal with both IPv4 and used for internet applications, while UDP is used for
IPv6. Unlike telnet(1), nc scripts nicely, and separates media streaming or VPNs.
error messages onto standard error instead of
sending them to standard output, as telnet(1) does
with some"

Step 1: How Do We Begin?

Above is how netcat is called. You can see that there are two arguments at the end called "destination" and "port."
The destination refers to a hostname or ip address of the server we are trying to connect to, while the port refers to
the port of the server we are trying to connect to.

Netcat in Python: Page 1


Step 2: Lets Begin

Above is some beginning python code. As you can see, we want to process the arguments to the program
similarly to how the actual utility does. The hostname will be the first argument after the executable's name, while
the port will be the second argument after the executable's name in the command line.

Step 3: Creating a Connection

Let's create a netcat function we can use. What we are basically doing here is creating a socket and connecting to
the server using the parameters given. For the netcat command, the current parameters are the hostname and port
of the server that we are attempting to connect to. The socket contains the parameters "socket.AF_INET" and
"socket.SOCK_STREAM" because we are defaulting to a TCP connection for this tutorial.

Netcat in Python: Page 2


Step 4: Lets Send Some Content

We extended our netcat function to take a third as there is data to read.


parameter, "content." There's a lot of content here so
let's break it down by line number. Line 28-29: we want this netcat connection to be a
one-time connection, so we declare the connection
Line 14-16: we send all of the content over the closed and then close the connection.
socket, we wait for a bit, and then we close the
socket to any outgoing data so the socket knows Line 31: This is a standard HTTP request. If you run
there is no more data coming. the code with the command line arguments
"google.com" and "80," then you will see a proper
Line 18-26: we create a buffer to store the server HTTP response
response, and while the socket is receiving data, we
append up to 1024 bytes of data to the result as long

Netcat in Python: Page 3


Step 5: Lets Have an Open Connection

The above code (which is located below the code our request
from the previous section) simply allows us to run
multple netcat commands over a pseudo-open Line 36-45: We will read into the buffer until we read
connection. (In reality, each time you run a command, an empty line
it opens and then closes a new TCP connection, so it
doesn't truly emulate the behavior of netcat, we are Line 48: we simply call our netcat function with the
simply doing this for learning purposes). Lets break hostname, port, and newly created content (which is
this down line by line as well: properly encoded)

Line 31: We want to read commands indefinitely in Line 50: if the content of our buffer ever contains
order to maintain "interactiveness" "Connection: Close" (indicating we want to close the
connection), we simply break out of the loop
Line 32: This is our buffer that will store the content of

Step 6: Conclusion

At the end of this tutorial you should have a minimal working netcat implementation. I shall leave it as an exercise
to the user to implement features such as:

1. supporting other protocols

2. fixing the code to not close the connection each time

3. adding flags that netcat already has to modify the behavior

Thanks for sharing :)

Netcat in Python: Page 4

You might also like