forked from NULLify-UNO/Scripting-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadedChat.py
More file actions
37 lines (29 loc) · 751 Bytes
/
threadedChat.py
File metadata and controls
37 lines (29 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import threading
from socket import *
class ThreadClass(threading.Thread):
def run(self):
while True:
print cname+': '+conn.recv(1024),
HOST = ''
PORT = input('Enter port to use: ')
ADDR = (HOST,PORT)
sname = raw_input('Enter Your Name: ')
serv = socket( AF_INET,SOCK_STREAM)
serv.bind((ADDR))
serv.listen(5)
print 'Listening...'
conn,addr = serv.accept()
print 'Connection from '+str(addr[0])
conn.send('Chat!!!\nEnter Your Name: ')
cname = conn.recv(1024)
cname = cname.rstrip('\n')
conn.send('You are now chatting with '+sname+'\n')
print cname+' has connected!'
t = ThreadClass()
t.start()
while True:
message = raw_input()
conn.send(sname+': '+message+'\n')
if 'exit' in message:
conn.close()
break