Sockets are the foundation of network applications. Python makes it super easy to get started with network socket programming. In this introduction we will create a simple server to accept and request the corresponding client program.
Since I recently had a bit of an obsession with Linux Containers, we will also implement 2 containers in the server. At the same time in the container we can create some other hosts in a few seconds, this can be very simple to simulate a network.
Create a container
I'm using Ubuntu14.04. Then run the following command with the root user to create 2 containers.
Copy the Code code as follows:
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've created the container, we'll go into the server container and start our server program. Run the following command with root permission to start the container: Lxc-start-n pyserver-d, which will start the container as our daemon. Let's reconnect to this container first. I like to use screen here, so I can easily access the container. Create a screen session first: SCREEN-DRR Pyserver, you need to reconnect to the container, you can use the command: Lxc-attach-n pyserver
When we get into the container, we need to install Python and start the server.
Copy the Code code as follows:
Apt-get Install Python
Vim pyserver.py
Open Vim (or your personal preference Text editor) and start typing in the Python code below.
Copy the Code code as follows:
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 is really good at socket programming "
Serversocket.sendto (response, clientaddress)
This piece of code is intuitive. We have created a serversocket listening 12000 port. A message is returned when the request is received (including the user name). The command to start the server is Python pyserver.py if all is well, you should see a message that this server is the ready to rock and roll! Exit the container with CTRL + A and ctrl+d (there is also a screen session)
Start the client
Now that the server is ready, let the client run. Check the IP address of the server container before you start, and we'll use it right away. You can get ip:lxc-ls--fancy with this command. Use a screen session to enter the container for the client, and install Python as in the previous steps.
Copy the Code code as follows:
Lxc-start-n pyclient-d
SCREEN-DRR pyclient
Lxc-attach-n pyclient
Apt-get Install Python
Vim pyclient.py
In Vim, create a pyclient.py file by typing the following code.
Copy the Code code as follows:
From socket Import *
# Replace The IP address in serverName with the IP of your container so 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 very intuitive. Require the user to enter a user name, then send to the server, and finally print out the server response information.
Now you can come on your own! Save the file, and 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 is easy to see that some of these basic code can be expanded to achieve many more interesting and complex applications. We can also use LXC's powerful features but simple operations to simulate a larger network for a distributed application.
The above is the whole content of this article, I hope to learn Python can help you.
Please take a moment to share the article with your friends or leave a comment. We would be grateful for your support!