'TCP Server/Client code that reverses a string in python

I need to adapt the following simple code so that to achieve the following:

  1. the client will send requests to the server to reverse strings (taken as a command line input) over the network using sockets.
  2. the client and the server negotiate through a fixed negotiation port (<n_port>) of the server, a random port (<r_port>) for later use. Then, later in the transaction stage, the client connects to the server through the negotiated random port (<r_port>) for actual data transfer. Here is the code:

#TCP Server:

from socket import *
serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('''',12020))
serverSocket.listen(1)
print 'The server is ready to receive'
while True:
  connectionSocket, addr = serverSocket.accept()
  sentence = connectionSocket.recv(1024).decode()
  capitalizedSentence = sentence.upper()
  connectionSocket.send(capitalizedSentence.
  encode()) 
  connectionSocket.close()

TCP Client

 from socket import *
 serverName = ’servername’
 serverPort = 12000
 clientSocket = socket(AF_INET, SOCK_STREAM)
 clientSocket.connect((serverName,serverPort))
 sentence = raw_input(‘Input lowercase sentence:’)
 clientSocket.send(sentence.encode())
 modifiedSentence = clientSocket.recv(1024)
 print modifiedMessage.decode()
 clientSocket.close()

Will appreciate your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source