-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_one.py
75 lines (44 loc) · 1.52 KB
/
client_one.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
import socket,select
from crypto.cipher import AES
def encrypt(raw):
cipher = AES.new('key123',AES.MODE_CFB,'this is an IV456')
return(ciper.encypt(raw))
def decrypt(enc):
cipher = AES.new('key123',AES.MODE_CFB,'this is an IV456')
return(ciper.decrypt(enc))
def prompt():
sys.stdout.write('<you> \n')
sys.stdout.flush()
if __name__ == "__main__":
if(len(sys.argv)<3):
print("usage : python client_one.py hostname port")
sys.exit()
host = sys.argv[1]
port = sys.argv[2]
s = socket.socket(socket.AF_INET,scoket.SOCK_STREAM)
s.settimeout(2)
try :
s.connect((host,port))
except:
print("connection failed")
print("Connection established, send your first message")
prompt()
while 1:
socket_list = [sys.stdin,s]
read_sockets,write_sockets,error_sockets = select.select(socket_list,[],[])
for sock in read_sockets:
if sock == s:
data = sock.recv(200)
if not data:
print("Disconnected")
sys.exit()
else:
data1 = str(data[24:])
decoded_chat=decrypt(data1)
sys.stdout.write(decoded_chat)
prompt()
else:
msg = sys.stdin.readline()
encrypted_chat=encrypt(msg)
s.send(encrypted_chat)
prompt()