Repository navigation
Expand file tree
/
Copy pathClient.py
More file actions
81 lines (73 loc) · 2.79 KB
/
Copy pathClient.py
File metadata and controls
81 lines (73 loc) · 2.79 KB
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
from socket import *
import msvcrt as m
###Amin Ansarian 9223803
######################
def runClient(serverName,serverPort):
clientSocket = socket(AF_INET , SOCK_STREAM)
clientSocket.settimeout(0.5)
print(clientSocket)
check_flag = True
quit_flag = True
status_send_flag = True
try:
clientSocket.connect((serverName , serverPort))
print("You are now connected to the server")
cnflag=1
except:
print("Connection Failed, Try Again")
cnflag=0
if (cnflag==1):
user_status = input('1- type in your own message, 2- View other user messages\n')
while quit_flag:
if(user_status == '1'):
if status_send_flag:
clientSocket.send(bytes(user_status, 'UTF-8'))
status_send_flag = False
while True:
user_message = input('type in your text...\n')
if(user_message=="#view"):
clientSocket.send(bytes(user_message, 'UTF-8'))
print("You are now in view mode:")
user_status='2'
status_send_flag = True
break
elif(user_message=="#quit"):
clientSocket.send(bytes(user_message, 'UTF-8'))
clientSocket.close()
quit_flag = False
break
else:
clientSocket.send(bytes(user_message, 'UTF-8'))
elif(user_status == '2'):
if (check_flag):
if status_send_flag:
clientSocket.send(bytes(user_status, 'UTF-8'))
status_send_flag = False
try:
modifiedSentence = clientSocket.recv(4096)
print(modifiedSentence.decode('UTF-8'))
except:
if(m.kbhit()):
key = m.getch().decode('UTF-8')
if (key == "e"):
status_send_flag = True
check_flag = True
user_status = '1'
elif (key == "q"):
clientSocket.send(bytes(key, 'UTF-8'))
check_flag = True
quit_flag = False
clientSocket.close()
break
else:
check_flag = False
print("wrong key,please enter again")
else:
pass
else:
user_status = input('invalid status number, Please try again\n'
'1- type in your own message, 2- View other user messages\n')
if __name__ == "__main__":
serverName = 'localhost'
serverPort = 5000
runClient(serverName,serverPort)