Programming Sockets in Python
EDIT: I am sorry for derping out of control. When I initially published this post it was called “Programming Web Sockets in Python”, this is just flat out wrong. What we are making here is just a regular socket. Web Sockets and regular sockets are similar but are certainly not the same thing. I hope you will still find this useful! Sockets are pretty much the basis of how applications work on the Internet. Python makes it super easy to get started programming sockets. In this brief introduction we will create a simple server that greets the user when it receives incoming requests from the client application. Due to my recent obsession with Linux Containers we will also be implementing this inside of two containers. Containers make it really simple to simulate a network because you can create additional hosts in seconds.
Creating your Containers
I am running Ubuntu 14.04. So creating two additional containers can be achieved by running the following as the root user.lxc-create -t download -n pyServerChoose ubuntu, trusty, amd64 when prompted
Then clone the first container
lxc-clone -o pyServer -n pyClient
Running the Server
Now that we have created our containers lets jump into our server container and fire up our simple server application. We can start up the container by issuing the following command as root:lxc-start -n pyServer -d, this will start the container as a daemon. Let’s go ahead and get into by attaching the container. I like to do this inside of screen so that we can easily get in and out of the container. Create a screen session screen -dRR pyServer and once inside the screen attach the container lxc-attach -n pyServer Once we are inside the container we need to install python and launch our simple server.apt-get install python vim pyServer.py
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)
python pyServer.py if all goes well you should see a message that states This server is ready to rock and roll! Exit the container (and the screen session) by pressing Ctrl+a and Ctrl+dRunning the Client
Now that we have our server up and running, lets get our client working as well. Before we move forward, lets grab the IP address of our server container because we will need it soon. You can get the IP by runninglxc-ls --fancy. Launch the client container, attach it in screen, and install python in the same way that we did previously.lxc-start -n pyClient -d screen -dRR pyClient lxc-attach -n pyClient apt-get install python vim pyClient.py
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()
python pyClient.py. After entering your name and pressing enter you should see a response from your server with the encouraging message. This was a pretty trivial exercise, but we can quickly see that we can expand upon this basic code to create much more interesting and complex applications. We can also leverage the power and simplicity of LXC to create a simulated large network for distributed applications.Thank you for reading! Share your thoughts with me on bluesky, mastodon, or via email.
Check out some more stuff to read down below.
Most popular posts this month
- Lev Lazinskiy
- SQLite DB Migrations with PRAGMA user_version
- My Custom Miniflux CSS Theme
- Convert Markdown to PDF in Sublime Text
- Terminal RSS Reader With Nom
Recent Favorite Blog Posts
This is a collection of the last 8 posts that I bookmarked.
- The circus freaks of open source from Drew DeVault's blog
- Clanker: A Word For The Machine from Armin Ronacher's Thoughts and Writings
- I ran a half-marathon! from gluecko.se
- My Running Tips from Kevin Bell's Blog
- tweet from Derek Sivers blog
- My life was changed by four sentences in four books from Derek Sivers blog
- The world reveals itself to those who travel by foot from Escaping Flatland
- 45 things from Sean Voisen
Articles from blogs I follow around the net
“They had the simplest task in the world.”
This is a really nice set of transitions when pinching in and out in Photos in iOS 26. This is trickier than it seems, because it’s not just a linear zoom (like it would be in Maps or Sketch, for example). It’s a zoom and reflow – from 3 items to 1 item pe...
via Unsung June 13, 2026Pluralistic: Shareholder supremacy and the precog CEO (13 Jun 2026)
Today's links Shareholder supremacy and the precog CEO: A bright line test that's totally unfalsifiable. Hey look at this: Delights to delectate. Object permanence: Msft v Linux geeks; James Joyce scholars v Joyce estate; iPod sweatshops; Pratchett initiat...
via Pluralistic: Daily links from Cory Doctorow June 13, 2026Dangerous Technology For Americans Only
There is a bit of schadenfreude on Twitter right now about Anthropic being hit by the US government’s export control directive to suspend access to Fable and Mythos. Anthropic and their leadership have spent a lot of time and effort describing its own tech...
via Armin Ronacher's Thoughts and Writings June 13, 2026Generated by openring