Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'if not data: break' mean?

Tags:

python

I found this Python code here.

I don't understand what if not data: break in line 18 means.

#!/usr/bin/env python

import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 20  

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

conn, addr = s.accept()
print 'Connection address:', addr
while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    print "received data:", data
    conn.send(data)  # echo
    conn.close()
like image 463
Pano Avatar asked Oct 03 '22 10:10

Pano


1 Answers

It just checks if the data received is empty, if yes, then it breaks out of the loop. Much like checking for an empty string.

>>> not ""
True
>>> bool("")
False

If data = conn.recv(BUFFER_SIZE) gives an empty string, the while loop is terminated.

like image 129
Sukrit Kalra Avatar answered Oct 07 '22 18:10

Sukrit Kalra