Skip to content

Instantly share code, notes, and snippets.

@TheProjectsGuy
Created July 1, 2019 12:11
Show Gist options
  • Select an option

  • Save TheProjectsGuy/5444c3bf3bd9918baae3cba662434a73 to your computer and use it in GitHub Desktop.

Select an option

Save TheProjectsGuy/5444c3bf3bd9918baae3cba662434a73 to your computer and use it in GitHub Desktop.
A listener node which is a server. It simply connects, receives a message, prints it out, ends.
import socket
# An IP address for communication
IP_ADDR = "127.0.0.1" # Internal loop back
# Port to connect to
PORT_NO = 50001
with socket.socket(
socket.AF_INET, socket.SOCK_STREAM
) as skt:
# Connect and initialize the socket
skt.bind((IP_ADDR, PORT_NO))
# Listen for connections
skt.listen(1) # Queue size 1
# Accept a connection
conn_skt, addr = skt.accept()
# Communication started
with conn_skt:
print("Connection from {}".format(addr))
while True:
data = conn_skt.recv(1024)
if not data:
break
print("Received", data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment