SHARE
TWEET

Chat App

kalpin Jun 1st, 2020 (edited) 1,694 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Chat Application
  2. import socket
  3.  
  4.  
  5. def send_text(sending_socket, text):
  6.     data = text.encode()
  7.     sending_socket.send(data)
  8.  
  9. def get_text(receiving_socket):
  10.     data = receiving_socket.recv(1024)
  11.     return data.decode()
  12.  
  13. def server_setup():
  14.     print("\nPlease enter an IP address in the format x.x.x.x or press ENTER to listen choose your localhost address.")
  15.     ip_address = input("Enter your IP address: ")
  16.     if ip_address == "":
  17.         ip_address = '127.0.0.1'
  18.     print("\nPlease enter the port number you wish to use or press ENTER to use 8081.")
  19.     port_number = input("Enter a port number: ")
  20.     if port_number == '':
  21.         port_number = 8081
  22.     else:
  23.         port_number = int(port_number)
  24.    
  25.     server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  26.     server_socket.bind((ip_address, port_number))
  27.  
  28.     #wait for connection
  29.     server_socket.listen()
  30.     print("Waiting for connection")
  31.     my_socket, address = server_socket.accept()
  32.     return my_socket, server_socket
  33.  
  34. def client_setup():
  35.     print("\nPlease enter the IP address of the server in the format x.x.x.x or press ENTER to listen choose your localhost address.")
  36.     ip_address = input("Enter server IP address: ")        
  37.     if ip_address == "":
  38.         ip_address = '127.0.0.1'
  39.     print("\nPlease enter the port number you wish to use or press ENTER to use 8081.")
  40.     port_number = input("Enter a port number: ")
  41.     if port_number == '':
  42.         port_number = 8081
  43.     else:
  44.         port_number = int(port_number)
  45.     my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  46.     my_socket.connect((ip_address, port_number))
  47.     server_socket = None
  48.     return my_socket, server_socket
  49.  
  50. def main():
  51.     print("Chat Program")
  52.  
  53.     #get choice for client or server
  54.     print("\nAre you going to be the server or the client?")
  55.     choice = ""
  56.     while choice not in ("S","s","C","c"):
  57.         choice = input("Enter S or C: ")
  58.     choice = choice.lower()
  59.  
  60.     #setup cliet or server
  61.     if choice == "s":
  62.         my_socket,server_socket = server_setup()
  63.         message = "Welcome to the chat room!"
  64.         send_text(my_socket, message)
  65.     else:
  66.         #setup client
  67.         my_socket,server_socket = client_setup()
  68.  
  69.     #wait for messages and process
  70.     running = True
  71.     while running:
  72.        
  73.         #wait for message and display
  74.         print("...")
  75.         message = get_text(my_socket)
  76.         print(message)
  77.        
  78.         #get response and send
  79.         print('\nEnter your message to send. A blank messages quits the program.')
  80.         message = input("Enter message: ")
  81.         if message != '':
  82.             send_text(my_socket, message)
  83.         else:
  84.             running = False;
  85.             #exit program
  86.  
  87.     my_socket.close()
  88.     if server_socket !=None:
  89.         server_socket.close()
  90.  
  91.  
  92. if __name__ == '__main__':
  93.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top