Python Socket programming and pythonsocket Programming

Source: Internet
Author: User

Python Socket programming and pythonsocket Programming

Socket is the foundation of network applications. Python makes network socket programming very simple. In this introduction, we will create a simple server for receiving requests from the corresponding client program.
I am a little obsessed with Linux Containers recently, so we will also implement two Containers on the server. At the same time, we can create other hosts in a few seconds in the container, which can simulate a network very easily.

Create a container

I use Ubuntu14.04. then, run the following command as the root user to create two containers.

Copy codeThe Code is 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 we have created a container, first enters the server container and starts 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 process. Let's reconnect to the container first. Here I like to use screen, so that I can easily access the container. Create a screen session: screen-dRR pyServer. To re-connect to the container, run the following command: lxc-attach-n pyServer.
After entering the container, We need to install python and start the server.

Copy codeThe Code is as follows:
Apt-get install python
Vim pyServer. py

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

Copy codeThe Code is 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 are really good at socket programming"
ServerSocket. sendto (response, clientAddress)

This code is intuitive. We have created a serverSocket listener for port 12000. When a request is received (including the user name), a message is returned. The command to start the server is python pyServer. py. If everything is normal, you should see This server is ready to rock and roll! Use Ctrl + a and Ctrl + d to exit the container (and screen sessions)

Start the client

Now the server is ready for running. Check the IP address of the server container before starting, and we will use it immediately. You can use this command to obtain the IP Address: lxc-ls -- fancy. Use a screen session to enter the container on the client. Install python as in the previous step.

Copy codeThe Code is as follows:
Lxc-start-n pyClient-d
Screen-dRR pyClient
Lxc-attach-n pyClient
Apt-get install python
Vim pyClient. py

Run the following code in vim to create a pyClient. py file.

Copy codeThe Code is as follows:
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. The user is required to enter the user name, send it to the server, and then print the server response information.
Now you can come by yourself! Save the file and run the python pyClient. py program. After you enter your name and press enter, you will receive a response from the server.
This is a very simple example, but we can easily find that you can do some extensions on these basic code to implement many more interesting and complex applications. We can also use the powerful functions of LXC to simulate a larger network for a distributed application.

The above is all the content of this article. I hope it will be helpful for you to learn python.

Please take a moment to share your article with your friends or leave a comment. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.