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 https://docs.python.org/3/library/socket.html . 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 ch15_01.py.
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 ch15_01.py
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 ch15_02.py. 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
server.listen(10)
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"
client.send(message.encode('ascii'))
client.close()
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 ch15_03.py. 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 = '192.168.0.10'
port = 8091
print('connecting to server...')
client.connect((host, port))
print('connected')
recv = client.recv(1024)
print('received:', recv.decode('ascii'))
client.close()
print('closed')
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 ch15_02.py
Then, run client app.
$ python3 ch15_03.py
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.