Simple_Python_TCP_exemple/server.py

24 lines
677 B
Python

#!/usr/bin/env python3
import socket # Import the lib
TCP_IP = '127.0.0.1' # Set the listen address
TCP_PORT = 5005 # set the port
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create the socket
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept() # Accept every connection
print ('Connection address:', addr) # display connection detail.
while 1:
data = conn.recv(BUFFER_SIZE)
print ("received data:", data) # Print what you recive.
conn.send(data) # echo
if (data == b"exit"): # Exit the program once you recieve "exit".
break
conn.close() #Close the connection.