Socket Programming with Python and PubNub

1,109
PubNub
Build real-time apps quickly and scale them globally.

Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial walks through how you can send data from device-to-device, client-to-server, and vice versa using socket programming in Python.

Why use sockets to send data?

Internet-connected applications that need to operate in realtime greatly benefit from the implementation of sockets in their networking code. Some examples of apps that use socket programming are:

Python, unlike JavaScript, is a language that executes synchronously. This is why asyncio was developed – to make Python more robust, particularly for the nature of socket programming.

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 threads can handle the new socket data. Libraries like asyncio implement multiple threads, so your Python program can work in an asynchronous fashion.

Python Socket Programming Tutorial

Natively, Python provides a socket class so developers can easily implement socket objects 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.

import socket

Now we can create socket objects in our code.

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

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 Socket Stream, also the default which enables “sequenced, reliable, two-way, connection-based byte streams” over TCP1.

Once we have an initialized socket object, we can use some methods to open a connection, send data, receive data, and finally close the connection.

## Connect to an IP with Port, could be a URL
sock.connect(('0.0.0.0', 8080))

## Send some data, this method can be called multiple times
sock.send("Twenty-five bytes to send")

## Receive up to 4096 bytes from a peer
sock.recv(4096)

## Close the socket connection, no more data transmission
sock.close()

Python Socket Server

Now that we know a few methods for transmitting bytes, let’s create a client and server program with Python.

import socket

serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serv.bind(('0.0.0.0', 8080))
serv.listen(5)

while True:
    conn, addr = serv.accept()
    from_client = ''

    while True:
        data = conn.recv(4096)
        if not data: break
        from_client += data
        print from_client

        conn.send("I am SERVER\n")

    conn.close()
    print 'client disconnected'

This code makes a socket object, and binds it to localhost’s port 8080 as a socket server. When clients connect to this address with a socket connection, the server listens for data, and stores it in the “data” variable.

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 client socket demo code.

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('0.0.0.0', 8080))

client.send("I am CLIENT\n")

from_server = client.recv(4096)

client.close()

print from_server

This client opens up a socket connection with the server, but only if the server program is currently running. To test this out yourself, you will need to use 2 terminal windows at the same time.

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 streaming data between clients and servers using some basic Python network programming.

How Do You Send Data Between Clients?

Sending data between 2 or more client devices 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.

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 reliability and speed when transmitting peer-to-peer data?

This can be accomplished using a server in the middle. 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.

PubNub does this best with the Pub/Sub API. It is fast, reliable, secure, and easy to implement on any client device. Whether you have a Python server, a JavaScript website, or anything in between, you can use PubNub to send data to anyone in under 250ms.

With One-to-Many, One-to-One, or Many-to-Many, PubNub scales automatically 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.

PubNub and Python with an SSL Connection

Here is an example of peer-to-peer data that is sent with PubNub, on a single channel, with SSL. 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 infinite number of channels to send messages in realtime. Before you try the code, be sure to make a free PubNub account.

Client 1

from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
import time
import os

pnconfig = PNConfiguration()

pnconfig.publish_key = 'your pubnub publish key here'
pnconfig.subscribe_key = 'your pubnub subscribe key here'
pnconfig.ssl = True

pubnub = PubNub(pnconfig)

def my_publish_callback(envelope, status):
    # Check whether request successfully completed or not
    if not status.is_error():
        pass

class MySubscribeCallback(SubscribeCallback):
    def presence(self, pubnub, presence):
        pass
    def status(self, pubnub, status):
        pass
    def message(self, pubnub, message):
        print "from device 2: " + message.message

pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels("chan-1").execute()

## publish a message
while True:
    msg = raw_input("Input a message to publish: ")
    if msg == 'exit': os._exit(1)
    pubnub.publish().channel("chan-1").message(str(msg)).pn_async(my_publish_callback)

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!


from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
import time
import os

pnconfig = PNConfiguration()

pnconfig.publish_key = 'your pubnub publish key here'
pnconfig.subscribe_key = 'your pubnub subscribe key here'
pnconfig.ssl = True

pubnub = PubNub(pnconfig)

def my_publish_callback(envelope, status):
    # Check whether request successfully completed or not
    if not status.is_error():
        pass

class MySubscribeCallback(SubscribeCallback):
    def presence(self, pubnub, presence):
        pass
    def status(self, pubnub, status):
        pass
    def message(self, pubnub, message):
        print "from device 1: " + message.message

pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels("chan-1").execute()

## publish a message
while True:
    msg = raw_input("Input a message to publish: ")
    if msg == 'exit': os._exit(1)
    pubnub.publish().channel("chan-1").message(str(msg)).pn_async(my_publish_callback)

All of the code in this post is hosted on GitHub in the Python Socket Demo repository.

PubNub is entirely free up to 1 million messages per month. For more capability of the API, check out the PubNub Python v4 SDK documentation, or any of the other 75+ PubNub client SDKs.

PubNub
Build real-time apps quickly and scale them globally.
Tools mentioned in article
Open jobs at PubNub
Developer Advocate
London, England (UK)

PubNub powers apps that bring people together in real-time for remote work, play, learning, and health. Thousands of companies use PubNub’s developer platform and APIs as the foundation for their online chat, virtual events, geolocation, remote control, and real-time updates, at a massive global scale. Since 2010, PubNub has invested in the tools and global infrastructure required to serve customers like Adobe, DocuSign, Peloton, and RingCentral, delivering SOC 2 Type 2 security and reliability while meeting regulatory needs like HIPAA and GDPR. PubNub has raised over $130M from notable investors like Raine Group, Sapphire, Scale, Relay, Cisco, Bosch, Ericsson, and HPE.

We are an all-star technical team comprising of folks who have been part of successful acquisitions in enterprise and consumer software companies. If you like hyper scale systems and engineering projects that redefine limits, PubNub is for you. PubNub is proud to be an EEO employer.

Job Summary

As Developer Relations Engineer, you will use modern APIs to build open source software examples and demos for PubNub for Mobile and other platforms combining PubNub APIs. You will use the latest technology and champion developer interests in your pick of niche communities. This role is ideal for those that have had a past job title of Developer Educator, Developer Advocate, Developer Evangelist, Developer Relations, Software Engineer and the like.

You work with developers and with other departments to understand developer needs and translate those needs into solutions, such as code examples, demos, talks, videos, documentation, workshops, meetups, events, and more. You represent the developer and own developer problems externally and internally for the company and are confident in making a case presenting developer problems and solutions to other teams.

You enjoy public speaking, sharing your work, and do it with a positive and outgoing attitude. Ongoing flexibility in how you do your work to accomplish your goals is totally ok with you. You like exploring programming languages and technologies you may not be familiar with and can rapidly assess how to produce and promote new content and solutions for your discoveries.

As a Developer Advocate, you will build relationships with the developer community by generating content that will:

  • Grow developer interest in PubNub.
  • Provoke more signups and trials for PubNub's service.
  • Educate developers about PubNub capability and implementation best practices.
  • Help customers integrate PubNub rapidly into their projects.

Job Responsibilities

  • Create technical, developer-centric content for the PubNub blog, tutorials pages, and external media.
  • Create lectures and present at developer meetups before a technical audience.
  • Travel to developer conferences, user group meetups, and hackathons and participate on behalf of PubNub.
  • Do keyword research and SEO for all content that you create.
  • Explore and use PubNub features and provide feedback to product teams.
  • Collaborate with the engineering team to understand the functionality of products and represent the voice of the developer community.
  • Work with the marketing team to create strategy for the developer funnel. Report metrics of events and content.

Required Skills & Experience

  • Seasoned Software Engineering and/or Developer Relations experience. We are specifically looking for several years of experience in mobile development in multiple languages.
    • Examples include Kotlin, Java, Objective-C, and Swift.
  • Inherent sense of empathy for Developers in the community as well as your teammates and co-workers.
  • Demonstrate creativity, empathy, and outstanding communication ability - excellent spoken and written English.
  • Happy to travel occasionally.
  • Adept at communicating technically complicated topics simply and elegantly.
  • An innate, heightened urgency to complete projects on time and with high quality.
  • General Search Engine Optimization (SEO) skill.
  • Fluent in English, with Japanese or Korean as a bonus

Bonus Skills

  • Other API Development Experience (frameworks in languages like Python, Node.js, C#, PHP, Java, C++ or other).
  • IoT (C or Python) Development Experience.
  • Full Stack Web Application Development Experience
  • Serverless Microservice Development Experience
  • Higher education degree/certification in Computer Science or related technical field.
  • Image and Video editing skills for instructional videos and diagrams in blog/tutorial content

What we offer

  • The opportunity to be part of a well funded, innovative global company on high growth trajectory expanding into the APAC region
  • Partner with top customers in our vision to make virtual experiences and spaces as engaging as in-person ones
  • Work in a friendly, collaborative and passionate environment that takes pride in exploring new ideas and solving real problems
  • Company sponsored health plan which includes eligible dependents for medical, dental, vision, life and disability coverage in addition to a competitive compensation package
  • Open vacation policy which allows you to take time off when you need it
  • Other perks such as Fitness benefits, home office supply reimbursement, Mobile and Internet stipend
  • Flexibility to work remote or from our centrally located offices (co-sharing)
  • Last but most importantly, a fun team that loves to celebrate achievements of all sizes

Note: I acknowledge that this position is not eligible for Visa sponsorship.

IND

Developer Relations Engineer
Singapore

PubNub powers apps that bring people together in real-time for remote work, play, learning, and health. Thousands of companies use PubNub’s developer platform and APIs as the foundation for their online chat, virtual events, geolocation, remote control, and real-time updates, at a massive global scale. Since 2010, PubNub has invested in the tools and global infrastructure required to serve customers like Adobe, DocuSign, Peloton, and RingCentral, delivering SOC 2 Type 2 security and reliability while meeting regulatory needs like HIPAA and GDPR. PubNub has raised over $130M from notable investors like Raine Group, Sapphire, Scale, Relay, Cisco, Bosch, Ericsson, and HPE.

We are an all-star technical team comprising of folks who have been part of successful acquisitions in enterprise and consumer software companies. If you like hyper scale systems and engineering projects that redefine limits, PubNub is for you. PubNub is proud to be an EEO employer.

Located in Singapore.

Job Summary

As Product Evangelist you will use modern APIs to build open source software examples and demos for PubNub for Mobile and other platforms combining PubNub APIs. You will use the latest technology and champion developer interests in your pick of niche communities. This role is ideal for those that have had a past job title of Developer Educator, Developer Advocate, Developer Evangelist, Developer Relations, Software Engineer and the like.

You work with developers and with other departments to understand developer needs and translate those needs into solutions, such as code examples, demos, talks, videos, documentation, workshops, meetups, events, and more. You represent the developer and own developer problems externally and internally for the company and are confident in making a case presenting developer problems and solutions to other teams.

You enjoy public speaking, sharing your work, and do it with a positive and outgoing attitude. Ongoing flexibility in how you do your work to accomplish your goals is totally ok with you. You like exploring programming languages and technologies you may not be familiar with and can rapidly assess how to produce and promote new content and solutions for your discoveries.

As a Developer Advocate, you will build relationships with the developer community by generating content that will:

  • Grow developer interest in PubNub.
  • Provoke more signups and trials for PubNub's service.
  • Educate developers about PubNub capability and implementation best practices.
  • Help customers integrate PubNub rapidly into their projects.

Job Responsibilities

  • Create technical, developer-centric content for the PubNub blog, tutorials pages, and external media.
  • Create lectures and present at developer meetups before a technical audience.
  • Travel to developer conferences, user group meetups, and hackathons and participate on behalf of PubNub.
  • Do keyword research and SEO for all content that you create.
  • Explore and use PubNub features and provide feedback to product teams.
  • Collaborate with the engineering team to understand the functionality of products and represent the voice of the developer community.
  • Work with the marketing team to create strategy for the developer funnel. Report metrics of events and content.

Required Skills & Experience

  • Seasoned Software Engineering and/or Developer Relations experience. We are specifically looking for several years of experience in mobile development in multiple languages. Examples include Kotlin, Java, Objective-C, and Swift.  
  • Inherent sense of empathy for Developers in the community as well as your teammates and co-workers.
  • Demonstrate creativity, empathy, and outstanding communication ability - excellent spoken and written English.
  • Happy to travel occasionally.
  • Adept at communicating technically complicated topics simply and elegantly.
  • An innate, heightened urgency to complete projects on time and with high quality.
  • Exposure to general Search Engine Optimization (SEO) 
  • Fluent in English, with Japanese or Korean as a bonus

Bonus Skills

  • Other API Development Experience (frameworks in languages like Python, Node.js, C#, PHP, Java, C++ or other).
  • IoT (C or Python) Development Experience.
  • Full Stack Web Application Development Experience
  • Serverless Microservice Development Experience
  • Higher education degree/certification in Computer Science or related technical field.
  • Image and Video editing skills for instructional videos and diagrams in blog/tutorial content

What we offer 

  • The opportunity to be part of a well funded, innovative global company on high growth trajectory expanding into the APAC region 
  • Partner with top customers in our vision to make virtual experiences and spaces as engaging as in-person ones 
  • Work in a friendly, collaborative and passionate environment that takes pride in exploring new ideas and solving real problems
  • Company sponsored health plan which includes eligible dependents for medical, dental, life and disability coverage in addition to a competitive compensation package
  • Open vacation policy which allows you to take time off when you need it 
  • Other perks such as Fitness benefits, home office supply reimbursement, Mobile and Internet stipend
  • Flexibility to work remote or from our centrally located offices (co-sharing)
  • Last but most important, a fun team that loves to celebrate achievements of all sizes

Note: This position is not eligible for Visa sponsorship.

IND

Engineering Manager - Server Engineering
US

PubNub powers apps that bring people together in real-time for remote work, play, learning, and health. Thousands of companies use PubNub’s developer platform and APIs as the foundation for their online chat, virtual events, geolocation, remote control, and real-time updates, at a massive global scale. Since 2010, PubNub has invested in the tools and global infrastructure required to serve customers like Adobe, DocuSign, Peloton, and RingCentral, delivering SOC 2 Type 2 security and reliability while meeting regulatory needs like HIPAA and GDPR. PubNub has raised over $130M from notable investors like Raine Group, Sapphire, Scale, Relay, Cisco, Bosch, Ericsson, and HPE.

PubNub is proud to be an EEO employer.

Job description

You’ll lead a dynamic team of talented engineers who are responsible for building and managing PubNub’s core services and APIs. You’ll report to the Director of Engineering and work closely with Architects, SREs, and Product Managers to deliver large-scale customer value. You’ll also need to collaborate with other engineering teams whose work will be dependent on the APIs you build.

The applications your team manages create billions of events and produce terabytes of data on a daily basis. You will have the unique opportunity to help build out PubNub's services and infrastructure to solve challenging problems related to distributed systems, real-time messaging, and large scale data management.

Responsibilities:

  • Build and mentor a team of talented Software Engineers with various levels of experience while positively influencing their careers.
  • Work with Product Management to develop product and technical specifications for bugs, enhancements, and new features.
  • Communicate both the business and technical rationale behind your decisions.
  • Own, operate and maintain your team’s services, ensuring they meet customer demand as well as defined performance and availability SLAs. 
  • Employ Agile methodologies to continuously deliver value to customers, helping to plan your team’s Sprints while collaborating with dependent teams to ensure that they are aware of your delivery dates.
  • Institute and maintain a rotating on-call incident escalation and response processes for the team.
  • Manage highly critical infrastructure and services in the cloud.
  • Understand complex architectures and be comfortable working with multiple teams.
  • Conduct performance analysis, capacity management, and troubleshoot large scale distributed systems.
  • Support the engineering team in achieving a high level of technical excellence and stability
  • Develop engineers on the team, helping them advance in their careers
  • Contribute to engineering-wide initiatives as a member of PubNub’s engineering management team

Experience & Skills Required:

  • A Masters or Bachelors degree, or equivalent experience.
  • 10+ years of software engineering experience developing highly-available full stack applications and distributed systems with 3+ years of that experience working as an Engineering Manager.
  • Stellar communication, organization and management skills with proven track record in an agile environment.
  • Experience developing RESTful services.
  • Hands-on experience with SQL/NoSQL databases and caches.
  • Experience developing products on AWS or Google Cloud Services.
  • Experience automating CI/CD pipelines, and configuration management for deploying, configuring, and managing servers and systems.
  • Strong expertise in troubleshooting complex production issues. 
  • Excellent problem solving, critical thinking, and communication skills.
  • You are a leader who can influence the technical direction of a team, but you understand that your primary mission is growing a rock solid engineering team.
  • You know your team's operational metrics and know how to keep them delivering high quality, resilient, services and products in a cloud environment.
  • You use agile methodologies and believe in constant incremental improvements. 
  • You are a leader that serves your team, empowers them, inspires them, and helps them grow, but knows when to hold them accountable as well.
  • You believe in building a culture of learning, knowledge sharing and collaboration.
  • You are deeply committed to helping your customers be successful.
  • Managed teams that shipped and operated critical infrastructure. 
  • The ability to thrive on a high level of autonomy and responsibility

Bonus Qualifications:

  • Containerization and Orchestration tools and platforms (Kubernetes, Mesos, CloudFoundry, etc.)
  • Previous experience with Test Driven Development with heavy use of unit testing frameworks
  • Previous start-up or small company experience preferred

 

Verified by
You may also like