-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConnect.py
More file actions
executable file
·50 lines (44 loc) · 1.39 KB
/
Connect.py
File metadata and controls
executable file
·50 lines (44 loc) · 1.39 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
import socket, time
EOM = "\n\n###"
def Send(message,server,port):
if waitTillAlive(server, port):
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((server, port))
output = str(message) + str(EOM)
client.sendall(output.encode('utf-8'))
chunks = []
while True:
buf = (client.recv(10000)).decode("utf-8")
if len(buf) < 5:
if len(chunks) == 0:
chunks.append(buf)
else:
chunks[-1] += buf
else:
chunks.append(str(buf))
if EOM in chunks[-1]:
res = "".join(chunks)[:-5]
break
return res
def connect(domain,port,timeout=10):
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.settimeout(timeout)
client.connect((domain, port))
output = 'TEST: HELLO\n\n###'
client.sendall(output.encode('utf-8'))
client.close()
return True
except IOError:
return False
def waitTillAlive(domain, port):
secondTime = False
while (1):
if connect(domain,port):
if secondTime: print("Connected To:",domain)
break
else:
time.sleep(5)
secondTime = True
print("Trying again....")
return True