Sockets (aka socket programming) enable programs to send and receive data, , at any given moment. This tutorial walks through how you can from , , and using socket in . bi-directionally send data device-to-device client-to-server vice versa programming Python Why use sockets to send data? Internet-connected applications that need to operate in realtime greatly benefit from the implementation of in their . Some examples of apps that use socket programming are: sockets networking code Web pages that show live notifications (Facebook, Twitch, eBay) Multiplayer online games (League of Legends, WoW, Counter Strike) Chat Apps (WhatsApp, WeChat, Slack) Realtime Data Dashboards (Robinhood, Coinbase) IoT devices (Nest, August Locks) , unlike JavaScript, is a language that executes . This is why was developed — to make Python more robust, particularly for the nature of socket programming. Python synchronously asyncio With streaming sockets, data can be sent or received at any time. In case your Python program is in the middle of executing some code, other can handle the new socket data. Libraries like implement multiple threads, so your Python program can work in an asynchronous fashion. threads asyncio Python Socket Programming Tutorial Natively, Python provides a so developers can easily implement in their source code. To use a socket object in your program, start off by importing the socket library. No need to install it with a package manager, it comes out of the box with Python. socket class socket objects Now we can create socket objects in our code. This code creates a socket object that we are storing in the “sock” variable. The constructor is provided a family and type parameter respectively. The family parameter is set to the default value, which is the . Address Format Internet The type parameter is set to , also the default which enables “sequenced, reliable, two-way, connection-based byte streams” over . Socket Stream TCP 1 Once we have an initialized socket object, we can use some methods to open a , data, data, and finally the connection. connection send receive close Python Socket Server Now that we know a few methods for transmitting bytes, let’s create a client and server program with Python. This code makes a socket object, and binds it to as a . When clients connect to this address with a socket connection, the server listens for data, and stores it in the “data” variable. localhost’s port 8080 socket server Next, the program logs the client data using “print,” and then sends a string to the client: . I am SERVER Let’s take a look at client code that would interact with this server program. Python Socket Client Here is the socket demo code. client This client opens up a socket connection with the server, but . To test this out yourself, you will need to use at the same time. only if the server program is currently running 2 terminal windows Next, the client sends some data to the server: I am CLIENT Then the client receives some data it anticipates from the server. Done! You can now get started using some basic Python network programming. streaming data between clients and servers How Do You Send Data Between Clients? Sending data over the internet is tricky. Due to protections implemented by network security, not all devices connected to the world wide web have a publicly accessible internet protocol (IP) address. between 2 or more client devices This means that the Python code that we implemented will not be 100% reliable for sending peer-to-peer data in our realtime app. How do we achieve and when transmitting ? reliability speed peer-to-peer data This can be accomplished using a . Client devices using the internet can connect to a server with a public IP address (or a website domain). Then, this broker in the middle can pass messages routed to 1 or many clients. server in the middle PubNub does this best with the . It is fast, reliable, secure, and easy to implement on . Whether you have a Python server, a JavaScript website, or anything in between, you can use PubNub to send data to anyone in . Pub/Sub API any client device under 250ms With , , or , PubNub to support any application load. Using the API opens up an instant, always-on connection between all clients that have the Pub/Sub API keys. This accomplishes the same objectives as a socket connection. One-to-Many One-to-One Many-to-Many scales automatically PubNub and Python with an SSL Connection Here is an example of that is sent with PubNub, on a single channel, with . You can think of this like sending data over a TCP socket. When you sign up for a free PubNub account, you can use a practically to send messages in realtime. Before you try the code, be sure to . peer-to-peer data SSL infinite number of channels make a free PubNub account Client 1 Client 2 Strings can be entered on the command line for these 2 client programs. Maximum message size for PubNub publishing is 32kb. Use 2 terminal windows to try out the code! All of the code in this post is hosted on . GitHub in the Python Socket Demo repository PubNub is entirely free up to . For more capability of the API, check out the , or any of the other . 1 million messages per month PubNub Python v4 SDK documentation 75+ PubNub client SDKs Originally published at www.pubnub.com .