-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpync.py
99 lines (88 loc) · 2.66 KB
/
pync.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
from socket import socket
from time import time,sleep
class Pync:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.sock = socket()
self.sock.connect((self.ip, self.port))
def receive(self, length=1024):
"""
Receive data in 'length'-sized blocks
Default:
length = 1024 (bytes)
"""
data = self.sock.recv(length)
return data.decode()
def receive_until(self, keyword, length=1024):
"""
Receive data in 'length'sized iteration until it finds the keyword
Default:
length = 1024 (bytes)
"""
buffer = ""
while not keyword in buffer:
data = self.sock.recv(length)
buffer += data.decode()
return buffer
def receive_from(self, keyword, length=1024, junk=False):
"""
Receive data in 'length'-sized iteration from the keyword until end, useful for waiting signals.
If junk is set to True, you will also receive the previous data before it finds the keyword
Default:
length = 1024 (bytes)
keyword = None
junk = False
Returns:
buffer, junk
"""
buffer = ""
junky = ""
while not keyword in buffer:
junk += self.sock.recv(length)
buffer = self.receive_all()
if junk:
return buffer, junky
return buffer
def receive_all(self, timeout=2, length=1024):
"""
Receive all data that can be accepted with 'timeout' idle time
Default:
timeout = 2 (seconds)
length = 1024 (bytes)
"""
buffer = ""
self.sock.setblocking(0)
datacount = 0
begin=time()
while True:
if datacount and time() - begin > timeout:
break
elif time() - begin > timeout:
return "no data"
try:
data=self.sock.recv(length)
if data:
buffer += data
begin = time()
datacount += 1
else:
sleep(0.1)
except:
pass
return buffer
def send(self, data="\n"):
"""
Well, it sends... data
Default:
data = "\n"
"""
self.sock.send(data.encode())
def send_all(self, data):
"""
It sends data until it breaks or it's done.
Just using basic "sendall" from python3
"""
self.sock.sendall(data.encode())
def close(self):
self.sock.close()