├── README.md ├── client.py └── server.py /README.md: -------------------------------------------------------------------------------- 1 | # Commandline chat app in Python 2 | 3 | 4 | Intro 5 | ------------ 6 | 7 | 8 | Hi Guys this repo consist of a source code for a simple CLI chatting app made using sockets in Python 9 | 10 | 11 | The original article 12 | ---------------------- 13 | 14 | This repo is continuation of an [Article](https://kalebujordan.com/chat-application-python/) on [my blog](https://kalebujordan.com/) so you might wanna review it to see a more brief tutorial on how it all works out. 15 | 16 | 17 | Getting started 18 | -------------------- 19 | 20 | 21 | To get started just clone the repository using *git* command or pressing download button option at the right side of the repository 22 | 23 | **Cloning** 24 | 25 | ```bash 26 | $ git clone https://github.com/Kalebu/Commandline-chatting-system-python 27 | $ cd Commandline-chatting-system-python 28 | Commandline-chatting-system-python $ tree 29 | . 30 | ├── client.py 31 | ├── README.md 32 | └── server.py 33 | 34 | 0 directories, 3 files 35 | ``` 36 | 37 | This repo consist of two **Python scripts** named *client.py* and *server.py*as I have explained on the tutorial, whereby **server.py** will serve as our server node and **client.py** will serve as our client node. 38 | 39 | 40 | Running our scipt 41 | ------------------ 42 | 43 | **Note** 44 | 45 | You should start running the server script before running the client script because if you do otherwise, the client will exit immediately as result of not finding a server node to connect 46 | 47 | **running server.py** 48 | 49 | ```bash 50 | $ python server.py 51 | 52 | ``` 53 | 54 | **running client.py** 55 | 56 | ```bash 57 | $ python client.py 58 | Enter server_ip: 127.0.0.1 59 | Finding connection 60 | Connection succesful made to the server 61 | ``` 62 | 63 | **Note** 64 | 65 | If the server script is run on the different pc or laptop enter your server pubic IP on client **enter ip** prompt 66 | 67 | Explore it 68 | ----------------- 69 | 70 | Now youre script should be running and able to communicate with each other, you try writing message to any of those script and then press enter to send the message to the another node whether it's server or client 71 | 72 | Give it a star 73 | -------------- 74 | Did you find this information useful, then give it a star 75 | 76 | 77 | Credits 78 | ----------- 79 | All the credits to [kalebu](github.com/kalebu) 80 | -------------------------------------------------------------------------------- /client.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import time 3 | import os 4 | import platform 5 | import threading 6 | 7 | 8 | class client(): 9 | ''' node for sending request to server 10 | to iniate a communication ''' 11 | 12 | 13 | #__________initializing a node for TCP communication___________ 14 | 15 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 16 | 17 | #__________ Our node server information _______________ 18 | 19 | 20 | def __init__(self): 21 | while True: 22 | self.server_ip = input('Enter server_ip: ') 23 | self.server_port = 12345 24 | if len(self.server_ip.split('.'))<4: 25 | continue 26 | break 27 | print('Finding connection') 28 | time.sleep(1) 29 | 30 | 31 | @property 32 | def make_connection(self): 33 | ''' Sending connection request to the server node ''' 34 | while True: 35 | try: 36 | server = (self.server_ip, self.server_port) 37 | self.client_socket.connect(server) 38 | print('Connection succesful made to the server') 39 | return True 40 | except: 41 | print('..');time.sleep(0.1) 42 | print('..'*2);time.sleep(0.1) 43 | print('..'*6);time.sleep(0.1) 44 | if platform.system().lower().startswith('win'): 45 | os.system('cls') 46 | continue 47 | os.system('clear') 48 | 49 | def send_sms(self, msg): 50 | ''' Sending the message to the connected 51 | Sever ''' 52 | self.client_socket.send(msg.encode()) 53 | 54 | def receive_sms(self): 55 | ''' Receiving message from the node server''' 56 | while True: 57 | data = '' 58 | data = self.client_socket.recv(1024).decode() 59 | '''Printing sms received from the server ''' 60 | time.sleep(0.001) 61 | print(data) 62 | 63 | def chat_room(self): 64 | if self.make_connection: 65 | Receiving_cio = threading.Thread(target=self.receive_sms) 66 | Receiving_cio.daemon = True 67 | Receiving_cio.start() 68 | while True: 69 | message = input() 70 | message = "\nclient:{}\n".format(message) 71 | self.send_sms(message)#Client replying 72 | continue 73 | 74 | if __name__ == '__main__': 75 | Client = client() 76 | Client.chat_room() 77 | 78 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Server will be receiving connection from 3 | 4 | client 5 | 6 | 7 | ''' 8 | 9 | import socket 10 | import threading 11 | import time 12 | 13 | 14 | class Server: 15 | # ______Creating node for TCP communication protocol ___________ 16 | server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 17 | # ______Default Local host ___________________________ 18 | host = "127.0.0.1" 19 | # If you want to send over wifi replace host with your IP address 20 | port = 12345 21 | # Select random port except those one set for default functionalities 22 | 23 | def __init__(self): 24 | server_config = (self.host, self.port) 25 | self.server.bind(server_config) 26 | self.server.listen(5) 27 | 28 | # ___________Accepting connection from client __________ 29 | 30 | self.clientsocket, self.addr = self.server.accept() 31 | print("Get connecting from ", self.addr) 32 | 33 | # ________chatting function _______________ 34 | 35 | def receive_sms(self): 36 | ''' Receiving connection from the client''' 37 | try: 38 | while True: 39 | data = self.clientsocket.recv(1024).decode() 40 | time.sleep(0.001) 41 | print(data) 42 | except Exception as ex: 43 | print("The below error have occured please checkout") 44 | print(ex) 45 | 46 | def chat(self): 47 | self.Receiving_ciao = threading.Thread(target=self.receive_sms) 48 | self.Receiving_ciao.daemon = True 49 | self.Receiving_ciao.start() 50 | while True: 51 | server_message = input() 52 | server_message = "\nserver:{}\n".format(server_message) 53 | self.clientsocket.send(server_message.encode()) 54 | 55 | 56 | if __name__ == '__main__': 57 | Server_m = Server() 58 | Server_m.chat() 59 | Server_m.Receiving_ciao.join() 60 | Server_m.server.close() 61 | --------------------------------------------------------------------------------