Network programming
with Sockets
Learning Objectives
Use sockets to create some networking applications
Sockets
Sockets allow us to communicate between computers over a network.
We are going to demonstrate this by creating:
An echo server and client
A chat application
A chatroom that allows multiple users to communicate over the
network
Client and Echo Server
We will develop a simple client and echo server.
We need to write two programs one for the client and the other for
the server
The client will be able to send messages to the echo server
the server will return the same message.
Client
# client.py
# Import socket module
from socket import *
We will develop the client first.
Create a python file called
client.py and add the following
# hostname and port of server
code
# 127.0.0.1 is the loopback IP address
# Computer can access the
# server running on the same computer
host = '127.0.0.1'
port = 5000
# set up socket
# A socket is the combination of
# the IP address and port number
s = socket()
# Connect to server
s.connect((host,port))
Client Continued
# Ask user for message
message = input(" -> ")
while message != 'q':
# send and encode message
s.send(message.encode())
'
# received data from server
data = s.recv(1024).decode()
print ('Received from server: ' + data)
# User message prompt
message = input(" -> ")
s.close()
Client - Complete Code Listing
1 from socket import *
2 host = '127.0.0.1'
3 port = 5000
4 s = socket()
5 s.connect((host,port))
'
6 message = input(" -> ")
7 while message != 'q':
8 s.send(message.encode())
9 data = s.recv(1024).decode()
10 print ('Received from server: ' + data)
11 message = input(" -> ")
12 s.close()
Server
# server.py
We will now develop the server.
# Import socket module
Create a python file called server.py and
from socket import *
add the following code
# hostname and port of server
# Need to be the same as the
# client host and server
host = '127.0.0.1'
port = 5000
# set up socket. A socket is the
# endpoint of a link between
# programs running over a network
s = socket()
Server Continued
# Bind host and port to socket. For a
# socket to accept client connections,
# it must be bound to a network address
# and a port number so the application
# knows which port to open and listen and
# receive and send data on.
s.bind((host,port))
# listen and accept socket
s.listen(1)
conn, addr = s.accept()
print ("Connection from: " + str(addr))
Server Continued
# Send and receive data. Server will
# always have a while loop this is
# continuously listening
while True:
# Receive data
data = conn.recv(1024).decode()
if not data:
break
print ("from connected user: " + str(data))
data = str(data).upper()
print ("sending: " + str(data))
# Send data
conn.send(data.encode())
conn.close()
Server - Complete Code Listing
1 from socket import *
2 host = '127.0.0.1'
3 port = 5000
4 s = socket()
5 s.bind((host,port))
6 s.listen(1)
7 conn, addr = s.accept()
8 print ("Connection
' from: " + str(addr))
9 while True:
10 data = conn.recv(1024).decode()
11 if not data:
12 break
13 print ("from connected user: " + str(data))
14 data = str(data).upper()
15 print ("sending: " + str(data))
16 conn.send(data.encode())
17 conn.close()
Client and echo server
Start the server by double clicking on the file server.py.
Start the client by double clicking on the file client.py.
Write a series message from the client.
Write q at the prompt to quit
Communicating over the network
As it stands it is only possible to access the server on the same
computer.
To access from another computer on the network we need to change
the IP address from 127.0.0.1 to the IP address of the server.
The IP address needs to be changed in both the client and server
code.
To get computer hostname and IP address
To get the computer hostname and IP address run the following code
from socket import *
print("Hostname:", gethostname())
print("IP Address:", gethostbyname(gethostname()))
In server.py and client.py change to this IP address.
host = 'IP address goes here’
Now run server.py on one computer and if you have access run client.py on
another computer on the same LAN.
Chat application
We have created an echo server that is useful for testing to see if a
network works, but has little practical utility beyond that.
Now we are going to create a simple two way half duplex chat
application
Half duplex means that we cannot send and receive messages at the
same time
The client code remains unchanged, but we do need to make changes
to the echo server.
1 from socket import *
2 host = "127.0.0.1"
3 port = 5000
4 s = socket()
5 s.bind((host,port))
6 s.listen(1)
7 conn, addr = s.accept()
8 print ("Connection from: " + str(addr))
9 while True:
10 data = conn.recv(1024).decode()
11 if not data:
12 break
13 print (str(data))
14 message = input(" -> ")
15 conn.send(message.encode())
16 conn.close()
17
Chat server: Server code
1 from socket import *
2 host = "127.0.0.1" Run server.py on one computer and
3 port = 5000
if you have access run client.py on
4 s = socket() another computer on the same LAN.
5 s.bind((host,port)) You will need to modify the IP address
6 s.listen(1)
7 conn, addr = s.accept()
8 print ("Connection from: " + str(addr))
9 while True:
10 data = conn.recv(1024).decode()
11 if not data:
12 break
13 print (str(data))
14 message = input(" -> ")
15 conn.send(message.encode())
16 conn.close()
Chatroom
We are now show a chatroom that allows multiple users to
communicate over a network
To do this we need to send and receive messages at the same time.
We need to introduce threading, which allows to run concurrent
operations.
We can run multiple clients now and the server acts to receive and
broadcast the messages to all the other clients.
Chatroom Client
The code can be opened from chatroom/chat_client.py and
chatroom/chat_server.py
Run the code on the same computer. Use a server and two clients.
Investigate the code by adding in comments on each of the line.
There are already some comments to help.
Now modify the code to allow you to run the code across multiple
machines. Work with you neighbours to achieve this.
Improvements
There are lots of directions you can take this
You could encrypt the messages
Create a simple game over the network, like noughts and crosses or a simple
car game.
Add a GUI for the client and the server.