Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python TCP Client Server Example
import socket
hostname, sld, tld, port = 'www', 'integralist', '', 80
target = '{}.{}.{}'.format(hostname, sld, tld)
# create an ipv4 (AF_INET) socket object using the tcp protocol (SOCK_STREAM)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect the client
# client.connect((target, port))
client.connect(('', 9999))
# send some data (in this case a HTTP GET request)
client.send('GET /index.html HTTP/1.1\r\nHost: {}.{}\r\n\r\n'.format(sld, tld))
# receive the response data (4096 is recommended buffer size)
response = client.recv(4096)
print response
import socket
import threading
bind_ip = ''
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5) # max backlog of connections
print 'Listening on {}:{}'.format(bind_ip, bind_port)
def handle_client_connection(client_socket):
request = client_socket.recv(1024)
print 'Received {}'.format(request)
while True:
client_sock, address = server.accept()
print 'Accepted connection from {}:{}'.format(address[0], address[1])
client_handler = threading.Thread(
args=(client_sock,) # without comma you'd get a... TypeError: handle_client_connection() argument after * must be a sequence, not _socketobject
Copy link

saif017 commented Aug 10, 2018

Please explain me the client.send('GET /index.html HTTP/1.1\r\nHost: {}.{}\r\n\r\n'.format(sld, tld)) line in first code.
Thanks bro for this code. It will help me.

Copy link

SamirOuahhabi commented Aug 23, 2018

Please explain me the client.send('GET /index.html HTTP/1.1\r\nHost: {}.{}\r\n\r\n'.format(sld, tld)) line in first code.

This is an example of HTTP requests. The string will be processed to become:
GET /index.html HTTP/1.1

This is a simplified way of requesting the page index.html from, using the http 1.1 protocol.
Check this out for more examples.

Copy link

dineshkumardk007 commented Aug 27, 2018

Bro what can we do after the connection was success?

Copy link

anojsampath commented Sep 25, 2018

def handle_client_connection(client_socket)
how to close this function
would you mind explain it

Copy link

vishnuhkz commented Sep 26, 2018

can anybody please explain what is going on this line of code? : " client_sock, address = server.accept() "
How the server accepting both the variables client and socket?

Copy link

nckalil commented Nov 12, 2018

The socket will start listening on the port for incoming tcp connection requests, server.accept() will finish executing once it establishes a new connection with a client

Copy link

rahul6612 commented Nov 29, 2018

how server computer get the message in cmd when client send the text to server computer

i have client computer and server computer which are connected with LAN cable. client computer has file and server computer has file when i am running the files in both computer then my client computer sending the text message to server computer that is good. But i want that when i run the client program the server computer get the text message in a pop box or in cmd (means i dont want to run separately file in server computer it will automaitcally run when i run the file) how i can do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment