server.py:
import socket
def main():
host = '127.0.0.1'
port = 5000
server_socket = socket.socket()
server_socket.bind((host, port))
server_socket.listen(1)
print('***** Server Started *****')
conn, addr = server_socket.accept()
print('Connection from: ' + str(addr))
while True:
data = conn.recv(1024).decode()
print('from connected user: ' + str(data))
if not data:
data = 'NO DATA'
else:
data = str(data).upper()
print('sending: ' + str(data))
conn.send(data.encode())
conn.close()
client.py:
import socket
def main():
host = '127.0.0.1'
port = 5000
client_socket = socket.socket()
client_socket.connect((host, port))
message = input(' -> ')
while message != 'q':
client_socket.send(message.encode())
data = client_socket.recv(1024).decode()
print('Receive from server: ' + data)
message = input(' -> ')
client_socket.close()
if __name__ == '__main__':
main()