Python's Socket programming _python

Source: Internet
Author: User
Tags get ip

Socket is the foundation of Network application. Python makes it super easy to get started with web socket programming. In this brief introduction we will create a simple server for accepting and requesting the corresponding client program.
Since I have recently been a bit obsessed with Linux containers, we will also implement 2 containers in the server. Also in the container we can create some other hosts in a matter of seconds, which can be very simple to simulate a network.

Creating a container

I'm using Ubuntu14.04. Then you can create 2 containers by running the following command with the root user.

Copy Code code as follows:

Lxc-create-t Download-n Pyserver
# Choose Ubuntu, trusty, AMD64 when prompted
# Then Clone the ' 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 launches the container as our daemon. Let's reconnect into this container first. I like to use screen here so that I can easily go in and out of the container. To 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 Code code as follows:

Apt-get Install Python
Vim pyserver.py

Open Vim (or your personal preference Text editor) and tap the following Python code.

Copy 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) + "! are really good at socket programming "
Serversocket.sendto (response, clientaddress)

This piece of code is intuitive. We created a serversocket to listen for Port 12000. A message is returned when the request is received (including the username). The command to start the server is Python pyserver.py if everything is OK, you should be able to see such a message This server is ready to rock and roll! Exit the container with CTRL + A and Ctrl+d (and 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 that right away. You can use this command to get Ip:lxc-ls--fancy. Use a screen session to enter the container to the client, and install Python like the previous steps.

Copy 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, typing the following code to create a pyclient.py file.

Copy Code code as follows:

From socket Import *
# Replace The IP address in serverName with the IP of your container to 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 piece of code is also very intuitive. Require the user to enter a user name, and 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 type your name and press ENTER, you should be able to receive a response message from the server.
This is a very simple example, but it is easy to see that there are many more interesting and complex applications that can be done on the basis of the code. We can also use LXC powerful features but simple operations to simulate a larger network to achieve a distributed application.

The above is the entire content of this article, I hope to learn Python can help.

Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

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.