Creating simple tcp server


if you want to test a simple TCP server either on local server or global server you can just copy the below code and run it on your machine.If you want to test with global server then change the ip address instead of local host.

import socket
import threading

bind_ip = ‘’
bind_port = 65432

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

this is a python 2.7 code you can run it by sudo python
You can put your desired port number instead of the above.
for allowing the ports and killing the process refer the link below.

if you want to ensure your tcp server is working,then from another terminal in your local system you have to run another code that is tcp_client.below is the code run it then you would be seeing the messages from tcp server and tcp client.

import socket
#insert the details below
hostname, sld, tld, port =-------------------
target = ‘{}.{}.{}’.format(hostname, sld, tld)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client.connect((‘’, 65432))


response = client.recv(4096)

print response

make sure ports are same and enabled.

1 Like