This repository has been archived by the owner on Jan 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket_client.py
127 lines (106 loc) · 3.81 KB
/
socket_client.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
import socket
import sys
import time
import re
class Client(object):
"""Client class
Attributes:
sock: The socket
"""
def __init__(self, sock=None):
# Create a TCP/IP socket
try:
if sock is None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
except socket.error:
print 'Failed to create socket'
sys.exit()
print 'Socket Created'
def recv_timeout(self, timeout, test):
"""Receives reply from server, with a timeout and a list of strings
to test. When all test strings are received, the listening loop ends."""
# make socket non blocking
self.sock.setblocking(False)
# total data in an array
total_data=[]
data=''
joined_data = ''
# start time
begin=time.time()
while not (all(t in data for t in test) or
('scanfinished' in data)):
# if data exist, then break after timeout
if total_data and time.time()-begin > timeout:
break
# if no data exist, then break after longer timeout
elif time.time()-begin > timeout*2:
break
# receive data
try:
data = self.sock.recv(8192)
if data:
print 'received "%s"' % data
total_data.append(data)
# reset start time
begin=time.time()
# join all data to final data
joined_data = ''.join(total_data)
else:
# sleep to add time difference
time.sleep(0.1)
except:
pass
return joined_data
def connect(self, host, port):
"""Connects to the socket object, on host and port.
host: A string representing the ip address of the server to connect.
port: An int representing the port address of the server to connect.
"""
try:
# Connect to the server at the port
server_address = (host, port)
print 'connecting to %s port %s' % server_address
self.sock.connect(server_address)
# Receive welcome reply from server
self.recv_timeout(3, ['Welcome'])
except socket.error:
print 'Failed to connect to socket.'
sys.exit()
return
def send(self, message):
"""Function to send data from client to server.
message: A string representing the message to send from the client
to the server.
"""
try:
# Send data
# Make compatible with Windows line breaks
for line in message.splitlines():
if line.endswith('\r\n'):
line = line
elif line.endswith('\n'):
line = line[:-1] + '\r\n'
else:
line = line + '\r\n'
print 'sending "%s"' % line
self.sock.send(line)
self.recv_timeout(20, [line[:-2]])
if 'stopscan' in line:
self.recv_timeout(20, ['scanfinished'])
time.sleep(0.3)
except socket.error:
#Send failed
print 'Sending to server failed.'
sys.exit()
print 'Message sent successfully.'
return
def close(self):
self.sock.shutdown(socket.SHUT_RDWR)
time.sleep(0.5)
self.sock.close()
print('Socket closed.')
return
if __name__ =='__main__':
main(sys.argv[1:])