-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
128 lines (117 loc) · 4.34 KB
/
test.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'''
Created on Jan 30, 2015
@author: Shreyas
'''
#!usr/bin/env python
import socket
import threading
import select
import time
import datetime
def main():
class Chat_Server(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = 1
self.conn = None
self.addr = None
def run(self):
HOST = ''
PORT = 23647
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST,PORT))
s.listen(2)
self.conn, self.addr = s.accept()
# Select loop for listen
while self.running == True:
inputready,outputready,exceptready = select.select ([self.conn],[self.conn],[])
for input_item in inputready:
# Handle sockets
try:
message = self.conn.recv(1024)
print "Daniel: " + message + ' (' + datetime.datetime.now().strftime('%H:%M:%S') + ')'
except(KeyboardInterrupt,SystemExit):
print Exception
self.running=False
break
time.sleep(0)
self.sock.close()
def kill(self):
self.running = 0
class Chat_Client(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.host = None
self.sock = None
self.running = 1
def run(self):
PORT = 23647
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host, PORT))
# Select loop for listen
while self.running == True:
inputready,outputready,exceptready \
= select.select ([self.sock],[self.sock],[])
for input_item in inputready:
# Handle sockets
try:
message = self.sock.recv(1024)
print "Daniel: " + message + ' (' + datetime.datetime.now().strftime('%H:%M:%S') + ')'
except(KeyboardInterrupt,SystemExit):
print Exception
self.running=False
break
time.sleep(0)
self.sock.close()
def kill(self):
self.running = 0
class Text_Input(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = 1
def run(self):
while self.running == True:
text = raw_input('')
try:
chat_client.sock.sendall(text)
except(KeyboardInterrupt,SystemExit):
Exception
try:
chat_server.conn.sendall(text)
except(KeyboardInterrupt,SystemExit):
Exception
time.sleep(0)
def kill(self):
self.running = 0
# Prompt, object instantiation, and threads start here.
ip_addr = raw_input('Type IP address or press enter: ')
if ip_addr == '':
try:
chat_server = Chat_Server()
chat_client = Chat_Client()
chat_server.daemon=True
chat_server.start()
text_input = Text_Input()
text_input.daemon=True
text_input.start()
chat_server.join()
text_input.join()
except(KeyboardInterrupt,SystemExit):
Exception
else:
try:
chat_server = Chat_Server()
chat_client = Chat_Client()
chat_client.host = ip_addr
chat_client.daemon=True
text_input = Text_Input()
text_input.daemon=True
chat_client.start()
text_input.start()
chat_client.join()
text_input.join()
except(KeyboardInterrupt,SystemExit):
Exception
if __name__ == "__main__":
main()