Python Programming by Example (2015)

15. Socket Programming

This chapter explains how to create socket application using Python.

15.1 Socket Module

We can create application based on socket stack using net package. You can find it for further information on . I recommend you to read some books or websites about the concept of socket.

15.2 Hello World

To get started, we create a simple application to get a list of IP Address in local computer. We can use gethostname() and gethostbyname() from socket object.

In this section, we try to get local IP Address. Firstly, we can create a new file, called

import socket

hostname = socket.gethostname()

ip = socket.gethostbyname(hostname)

print('hostname:', hostname)

print('ip address:', ip)

Save this code. Try to build and run it.

$ python3

You should see your local IP address from your computer.


15.3 Client/Server Socket

Now we create a client/server socket using Python. We will use socket package to build client/server application. For illustration, we create server and client.

15.3.1 Server Socket

How to create server socket? It is easy. The following is a simple algorithm how to build server socket

·        create server socket

·        listen incoming client on the specific port

·        if client connected, server sends data and then disconnect from client

In this section, we build server app. Firstly, we can create a new file, called Then, write these scripts.

import socket

# create tcp/ip socket

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

host = socket.gethostname()

port = 8091

# bind to the port

server.bind((host, port))

# queue up to 10 clients


counter = 0

print('waiting connection from clients...')

while True:

    # establish a connection

    client, address = server.accept()

    counter += 1

    print('a new connection from',str(address))

    message = "welcome, your id=" + str(counter) + "\r\n"



It uses port 8091. You can change it.

15.3.2 Client Socket

Client socket is client application that connects to server and then sends/receives data from/to server. We should know about IP address and port from target server. We can call connect() to connect to server and call recv() to receive incoming data.

In this section, we build client app. Firstly, we can create a new file, called Then, write these scripts.

import socket

# create a socket object

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

# ip/hostname of server

# change this ip address

host = ''

port = 8091

print('connecting to server...')

client.connect((host, port))


recv = client.recv(1024)

print('received:', recv.decode('ascii'))



You should change host for IP address of server.

15.3.3 Testing

Now we can test our client/server application. Firstly, we run server application and then execute client application.

$ python3

Then, run client app.

$ python3

Here is sample of program output for server application:


Here is sample of program output for client application:


This is program output for the second client.