test-tcp / app.py
chris-rannou's picture
chris-rannou HF staff
Update app.py
2648b2e verified
raw
history blame contribute delete
No virus
745 Bytes
import socket
# Set up a TCP/IP server
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to server address and port 50052
server_address = ('localhost', 50052)
tcp_socket.bind(server_address)
# Listen on port 50052
tcp_socket.listen(1)
while True:
print("Waiting for connection")
connection, client = tcp_socket.accept()
try:
print("Connected to client IP: {}".format(client))
# Receive and print data 32 bytes at a time, as long as the client is sending something
while True:
data = connection.recv(32)
print("Received data: {}".format(data))
if not data:
break
finally:
connection.close()