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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With