File tree 3 files changed +97
-0
lines changed
Python_chatting_application
3 files changed +97
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Python_chat_App
2
+ Simple chat application using python
Original file line number Diff line number Diff line change
1
+ import socket
2
+ import threading
3
+
4
+ flag = 0
5
+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
6
+ hostname = input ("Enter your host :: " )
7
+ s .connect ((hostname , 1023 ))
8
+ nickname = input ("Enter your Name :: " )
9
+
10
+ def recieve ():
11
+ while True :
12
+ try :
13
+ msg = s .recv (1024 ).decode ("utf-8" )
14
+ if msg == 'NICK' :
15
+ print ("Welcome to Chat room :: " , nickname )
16
+ s .send (bytes (nickname , "utf-8" ))
17
+ else :
18
+ print (msg )
19
+ except :
20
+ print ("An Erro occured " )
21
+ s .close ()
22
+ flag = 1
23
+ break
24
+
25
+ def Write ():
26
+ while True :
27
+ try :
28
+ reply_msg = f"{ nickname } :: { input ()} "
29
+ s .send (bytes (reply_msg , "utf-8" ))
30
+ except :
31
+ print ("An Error Occured while sending message !!!" )
32
+ s .close ()
33
+ flag = 1
34
+ break
35
+ if flag == 1 :
36
+ exit ()
37
+ recieve_thrd = threading .Thread (target = recieve )
38
+ recieve_thrd .start ()
39
+
40
+ write_thrd = threading .Thread (target = Write )
41
+ write_thrd .start ()
Original file line number Diff line number Diff line change
1
+ import socket
2
+ import threading
3
+
4
+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
5
+ s .bind ((socket .gethostname (), 1023 ))
6
+ print (socket .gethostname ())
7
+ s .listen (5 )
8
+
9
+ clients = []
10
+ nickename = []
11
+
12
+
13
+ def Client_Handler (cli ):
14
+ while True :
15
+ try :
16
+ reply = cli .recv (1024 ).decode ("utf-8" )
17
+ if reply == "QUIT" :
18
+ index_of_cli = clients .index (cli )
19
+ nick = nickename [index_of_cli ]
20
+ nickename .remove (nick )
21
+ clients .remove (cli )
22
+ BroadCasating (f"{ nick } has left the chat room" )
23
+ print (f"Disconnected with f{ nick } " )
24
+ break
25
+ BroadCasating (reply )
26
+ except :
27
+ index_of_cli = clients .index (cli )
28
+ print (index_of_cli )
29
+ nick = nickename [index_of_cli ]
30
+ nickename .remove (nick )
31
+ clients .remove (cli )
32
+ BroadCasating (f"{ nick } has left the chat room" )
33
+ print (f"Disconnected with { nick } " )
34
+ break
35
+
36
+
37
+ def BroadCasating (msg ):
38
+ for client in clients :
39
+ client .send (bytes (msg , "utf-8" ))
40
+
41
+ def recieve ():
42
+ while True :
43
+ client_sckt , addr = s .accept ()
44
+ print (f"Connection has been established { addr } " )
45
+ client_sckt .send (bytes ("NICK" , "utf-8" ))
46
+ nick = client_sckt .recv (1024 ).decode ("utf-8" )
47
+ nickename .append (nick )
48
+ clients .append (client_sckt )
49
+ print (f"{ nick } has joined the chat room " )
50
+ BroadCasating (f"{ nick } has joined the chat room say hi !!!" )
51
+ threading ._start_new_thread (Client_Handler , (client_sckt , ))
52
+
53
+ recieve ()
54
+ s .close ()
You can’t perform that action at this time.
0 commit comments