Python socket stop receiving data

Is there any way we can stop receiving data when we use python socket?

if I have

while True:
    Socket.recv(1024)

What way we can use to manually stop receiving data?

Use something like

X=True
while X:
  Socket.recv(1024)
  if something:
    X=False

You can of course also catch exceptions, keyboard interrupts etc.