Code Simple TCP Client In Python

Hello guy's when we talk about networking there is two main things first is Client and second is Server. As we all know client send some data to server and get response this is the phenomena of network.

But the question is what is the TCP Client it. The client is provide the simplest method to connect, send and receive stream data over a network in synchronous mode is called TCP client.

Let's get coding.

import socket
target_host = "www.example.com"
target_port = 80
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.send( "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
response = client.recv(4444)

print response

  • We first create a socket object with the AF_INET and SOCK_STREAM parameters
  • The AF_INET parameter saying we are using a standard IPv4 address or hostname and SOCK_STREAM indicates that this will be a TCP client.
  • Then we connect the client to the server.
  • Client send some data to server.
  • Client receive some data back and print the response.
That's all. This is the simplest form of TCP Client. IF you have any question or query then feel free to comment below and if you found something interesting on our blog then follow us for future updates.

Mohit Saran(Hacker's King)