Python Socket programming

  • 2020-04-02 14:42:55
  • OfStack

Socket is the foundation of network application. Python makes it super easy to get started on socket programming. In this introduction we will create a simple server to accept and respond to requests from client programs.
Since I've recently become a bit of a fan of Linux Containers, we'll implement two Containers in the server as well. Also in the container we can create other hosts in a few seconds, which makes it very easy to simulate a network.

Create a container

I used Ubuntu14.04. Then I ran the following command from root to create the two containers.


lxc-create -t download -n pyServer
# Choose ubuntu, trusty, amd64 when prompted
# Then clone the first container
lxc-clone -o pyServer -n pyClient

Start the server

Now that we have created the container, we go into the server container and start our server program. To start the container, run the following command as root: lxc-start-n pyserver-d, which starts the container as our daemon. Let's reconnect to the container first. I like to use screen here so that I can easily get in and out of the container. First create a screen session:   Screen-drr pyServer, need to reconnect to the container, you can use the command :lxc-attach -n pyServer
Once we're inside the container, we need to install python and start the server.


apt-get install python
vim pyServer.py

Open vim(or your preferred text editor) and type in the following python code.


from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print "The server is ready to rock and roll!"
while 1:
    name, clientAddress = serverSocket.recvfrom(2048)
    response = "Hello " + str(name) + "! You are really good at socket programming"
    serverSocket.sendto(response, clientAddress)

This code is straightforward. We created a serverSocket to listen on port 12000. When the request is received (including the user name), a message is returned. The command to start the server is python pyserver.py. If all goes well, you should see This message: This server is ready to rock and roll! Ctrl+a and Ctrl+d to exit the container (and screen session)

Start client

Now that the server is ready, let the client run. Before we start, check the IP address of the server container, which we will use in a moment. You can use this command to get IP: lxc-ls --fancy. Enter the client container with a screen session and install python as in the previous steps.


lxc-start -n pyClient -d
screen -dRR pyClient
lxc-attach -n pyClient
apt-get install python
vim pyClient.py

Type the following code into vim to create a pyclient.py file.


from socket import *
# Replace the IP address in serverName with the IP of your container that you      grabbed previously.
serverName = '10.0.3.211'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
name = raw_input('Please enter your name:')
clientSocket.sendto(name, (serverName, serverPort))
response, serverAddress = clientSocket.recvfrom(2048)
print response
clientSocket.close()

This code is also intuitive. Ask the user to enter a username, send it to the server, and print out the server response.
Now you can do it yourself! Save the file, then execute the python program python pyclient.py. After you enter your name and press enter, you should receive a response from the server.
This is a very simple example, but it's easy to see that you can do a lot more interesting and complex applications with a few extensions to the basic code. We can also use LXC's powerful but easy operation to simulate a larger network to achieve a distributed application.

That's all for this article, and I hope it will help you learn python.

Please take a moment to share the article with your friends or leave a comment. We sincerely appreciate your support!


Related articles: