Before you try to use frame
you should check what you get in ret, frame
and evetually exit loop when there is no more frames.
if not ret: # eventually `if ret is False:`
print("No more frames")
break
or
if frame is None: # will not work `if not frame:` because `frame` can be numpy array which may need `any()` or `all()` to check if it is True or False.
print("No more frames")
break
while True:
ret, frame = cap.read()
if not ret:
print("no more frames")
break
result, frame = cv2.imencode('.jpg', frame)
data = pickle.dumps(frame)
size = len(data)
print("{}:{}".format(img_counter, size))
conn.sendall(struct.pack(">L", size) + data)
img_counter += 1
I added spaces in some places to make it more readable.
See PEP 8 – Style Guide for Python Code