Create a live streaming video chat app in Python

Ashika Madhav
4 min readJun 12, 2021

Python can create a variety of apps. So here we create a live streaming video chat app without a voice in python

What is Socket Program

Socket programming is a programming schema in which sockets are used to create a connection between software. They are used to connect software either between different computers or within the same computer. One socket listens on a particular port at an IP, while other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server. They are the real backbones behind web browsing. In simpler terms, there is a server and a client.

A sample of live streaming video chat app in Python language based on server-client

We create a sample app. We use socket programming for the network connection. We use OpenCV to capture the image processing and with videoCapture() we will continuous images. After this, we will use pickle and struct() so as to convert our video into binary data. At last, we will use imutils for image manipulation.

Server Script:

A server has a bind() method which binds it to a specific IP and port so that it can listen to incoming requests on that IP and port. A server has a listen() method which puts the server into listening mode. This allows the server to listen to incoming connections. And last a server has an accept() and close() method.

We will write the code in the file name server.py and copy it.

First, we install the libraries in the command prompt of python like socket, opencv, pickle, structure, imutils for running the code

import socket, cv2, pickle,struct,imutils # Socket Create
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
print('HOST IP:',host_ip)
port = 9999
socket_address = (host_ip,port)
server_socket.bind(socket_address)server_socket.listen(5)
print("LISTENING AT:",socket_address)
while True:
client_socket,addr = server_socket.accept()
print('GOT CONNECTION FROM:',addr)
if client_socket:
vid = cv2.VideoCapture(0)
while(vid.isOpened()):
img,frame = vid.read()
frame = imutils.resize(frame,width=320)
a = pickle.dumps(frame)
message = struct.pack("Q",len(a))+a
client_socket.sendall(message)
cv2.imshow('TRANSMITTING VIDEO',frame)
key = cv2.waitkey(1) & 0xFF
if key == ord('q'):
client_socket.close()
  • First of all, we import socket which is necessary.
  • Then we made a socket object and reserved a port on our pc.
  • After that, we bound our server to the specified port. Passing an empty string means that the server can listen to incoming connections from other computers as well. If we would have passed 127.0.0.1 then it would have listened to only those calls made within the local computer.
  • After that we put the server into listening mode.5 here means that 5 connections are kept waiting if the server is busy and if a 6th socket tries to connect then the connection is refused.
  • At last, we make a while loop and start to accept all incoming connections and close those connections after a thank you message to all connected sockets

Client Script:

Now we need something with which a server can interact. We could tenet to the server like this just to know that our server is working.
import socket,cv2, pickle,struct

import socket, cv2, pickle,struct,imutils# create socket
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = '169.254.162.241' # paste your server ip address here
port = 9999
client_socket.connect((host_ip,port)) # a tuple
data = b""
payload_size = struct.calcsize("Q")
while True:
while len(data) < payload_size:
packet = client_socket.recv(4*1024) # 4K
if not packet: break
data+=packet
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("Q",packed_msg_size)[0]
while len(data) < msg_size:
data += client_socket.recv(4*1024)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data)
cv2.imshow("RECEIVING VIDEO",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
client_socket.close()

How to run the script

  1. we run server.py script
python server.py

2. Now we run client.py script

python client.py

3. Final output

So in this way we have created a server-client connection.

--

--