-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
71 lines (65 loc) · 1.96 KB
/
client.py
File metadata and controls
71 lines (65 loc) · 1.96 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
# -*- coding: utf-8 -*-
# @Author: FSOL
# @File : client.py
"""
client.py
==================
Used as a default console to connect and control server.
Will only connect to local's server.
"""
import socket
import readline
import sys
import select
def communicate(server):
epoll = select.epoll()
epoll.register(server.fileno(), select.EPOLLHUP)
user_input = raw_input('-->')
while user_input != 'exit':
events = epoll.poll(1)
if events:
break
server.send(user_input)
message = server.recv(4096)
print message
user_input = raw_input('-->')
while user_input == '':
user_input = raw_input('-->')
server.close()
if user_input == 'exit':
return 1
return 0
def main():
flag = False
print "---------------------------\n Initializing....\n"
user_input = ''
while user_input != 'n':
server = socket.socket()
try:
if len(sys.argv) != 1:
server.connect(('127.0.0.1', int(sys.argv[1])))
else:
server.connect(('127.0.0.1', 9813))
while True:
user_input = raw_input("Please input the password to server(exit to close):")
if user_input == 'exit':
flag = True
break
server.send(user_input)
message = server.recv(8192)
if message == 'Connection established':
print "Connection established."
if communicate(server):
flag = True
break
print "Disconnected."
else:
print "Connection refused."
if flag:
break
except Exception as e:
print e
user_input = raw_input("Connection broke, wanna retry? n to exit")
print 'Console terminated.'
if __name__ == '__main__':
main()