title: Encoded Communication
body: <p>I'm trying to learn Network programming with Python language. In order that, I created a simple chat program with python. Now I want to encrypt communication between Server and Clients. How can I do that? The following code is my server code: </p>
  
  <pre><code>        TcpSocket.bind(("0.0.0.0",8000))
          TcpSocket.listen(2)
          print("I'm waiting for a connection...!")
          (client, (ip, port)) = TcpSocket.accept()
          print("Connection recived from the {}".format(ip))
          messageToClient = "You connected to the server sucessfuly.\n"
          client.send(messageToClient.encode('ascii'))
  
          dataRecived = "Message!"
  
          while True:
                  dataRecived = client.recv(1024)
                  print("Client :", dataRecived)
                  print("Server :")
                  dataSend = raw_input()
                  client.send(str(dataSend) + "\n")
  
  
          print("Connection has been closed.")
          client.close()
          print("Server has been shutdowned.")
          TcpSocket.close()
  
  
  
  def main():
  
          try:
                  print("Server has started.")
                  connectionOrianted()
  
          except :
                  print("Maybe connection terminated.")
          finally:
                  print("Session has closed.")
  
  
  
  if __name__ == "__main__": main()
  </code></pre>
  
  <p>And the following code is my client code.</p>
  
  <pre><code>#!/usr/bin/python3
  
  import socket
  import sys
  from builtins import input
  
  def main():
  
      try:
          serverHostNumber = input("Please enter the ip address of the server: \n")
          serverPortNumber = input("Please enter the port of the server: \n")
  
          # create a socket object
          TcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
          # connection to hostname on the port.
          TcpSocket.connect((serverHostNumber, int(serverPortNumber)))                                                                    
  
          while True:
              data = TcpSocket.recv(1024)
              print("Server : ", data)
              sendData = input("Client : ")
  
              if sendData == "exit":
                      TcpSocket.close()
                      sys.exit()
  
              TcpSocket.send(sendData.encode(encoding='ascii', errors='strict'))
  
      except Exception as e:
          print("The error: ", e) 
          TcpSocket.close()
          sys.exit()      
  
  if __name__ == "__main__" : main()
  </code></pre>