Video Streaming using Python

Python OpenCV

Socket Programming in Python

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

What is a Network Socket?

Types of Internet Sockets:

  • Stream sockets: Stream sockets allow processes to communicate using TCP. A stream socket provides a bidirectional, reliable, sequenced, and unduplicated flow of data with no record boundaries.
  • Datagram sockets: Datagram sockets allow processes to use UDP to communicate. A datagram socket supports a bidirectional flow of messages. A process on a datagram socket can receive messages in a different order from the sending sequence and can receive duplicate messages.
  • Raw sockets: Raw sockets provide access to ICMP. These sockets are normally datagram-oriented, although their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not for most applications.

Client-Server Model in Network Programming

Video Chat App using Python

Server-side Python Code:

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


client_socket,addr = server_socket.accept()




# This code is for the server 
# Lets import the libraries
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)

# Socket Bind

# Socket Listen
print("LISTENING AT:",socket_address)

# Socket Accept
while True:
client_socket,addr = server_socket.accept()
print('GOT CONNECTION FROM:',addr)
if client_socket:
vid = cv2.VideoCapture(0)

img,frame =
frame = imutils.resize(frame,width=320)
a = pickle.dumps(frame)
message = struct.pack("Q",len(a))+a

cv2.imshow('TRANSMITTING VIDEO',frame)
if cv2.waitKey(1) == '13':

Client-side Python Code:

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


packet = client_socket.recv(1024)



import socket,cv2, pickle,struct

# create socket
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = '' # 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
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)
if cv2.waitKey(1) == '13':

Thank You for reading!! 😇😇



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditya Raj

Aditya Raj

I'm passionate learner diving into the concepts of computing 💻