forked from hellman/sock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sock.py
187 lines (152 loc) · 5.05 KB
/
sock.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
import socket
from time import time
import telnetlib
from socket import timeout as Timeout, error as SocketError
__all__ = "Sock Sock6 toSock Timeout SocketError".split()
DEFAULT_TIMEOUT = 5
class Sock:
SOCKET_FAMILY = socket.AF_INET
def __init__(self, ip, port, timeout=None):
self.addr = (ip, port)
self.timeout = timeout
self.buf = ""
self.eof = False
self.sock = socket.socket(self.SOCKET_FAMILY, socket.SOCK_STREAM)
if timeout is None:
self.timeout = DEFAULT_TIMEOUT
self.sock.settimeout(timeout)
self.sock.connect((ip, port))
return
def read_one(self, timeout=None):
self.fill_one(timeout)
if not self.buf and timeout != 0:
raise EOFError("Connection closed")
buf = self.buf
self.buf = ""
return buf
def read_final(self):
try:
res = self.read_all()
except Timeout:
return self.buf
except EOFError:
return self.buf
return res
def read_all(self, timeout=None):
res = self.read_cond(lambda x: x.eof, timeout)
self.buf = ""
return res
def wait_for(self, s, timeout=None):
"""Wait for string in dataflow, DO NOT return data (to avoid splitting data)"""
self.read_cond(lambda x: s in x.buf, timeout)
pos = self.buf.find(s)
self.buf = self.buf[pos:]
return
def telnet(self):
t = telnetlib.Telnet()
t.sock = self.sock
t.interact()
def wait_for_re(self, r, timeout=None):
"""Wait for RE in dataflow, DO NOT return data (to avoid splitting data)"""
self.read_cond(lambda x: re.search(r, x.buf), timeout)
s = re.search(r, self.buf).group(0)
pos = self.buf.find(s)
self.buf = self.buf[pos:]
return
def read_until(self, s, timeout=None):
res = self.read_cond(lambda x: s in x.buf, timeout)
pos = res.find(s) + len(s)
self.buf = self.buf[pos:]
return res[:pos]
def read_until_re(self, r, timeout=None):
res = self.read_cond(lambda x: re.search(r, x.buf), timeout)
s = re.search(r, res).group(0)
pos = res.find(s) + len(s)
self.buf = res[pos:]
return res[:pos]
def read_nbytes(self, n, timeout=None):
res = self.read_cond(lambda x: len(x.buf) >= n, timeout)
self.buf = res[n:]
return res[:n]
def read_cond(self, cond, timeout=None):
time_start = time()
remaining = timeout
if timeout is None:
timeout = self.timeout
if self.eof and not self.buf:
raise EOFError("Connection closed")
while not cond(self):
self.fill_one(remaining)
if cond(self):
return self.buf
if self.eof:
raise EOFError("Connection closed")
if timeout == -1:
remaining = -1
elif timeout == 0:
raise Timeout("read_cond timeout")
else:
remaining = time_start + timeout - time()
if remaining <= 0:
raise Timeout("read_cond timeout")
return self.buf
def fill_one(self, timeout=None):
"""Read something from socket.
timeout = -1 - blocking until read
timeout = 0 - non-blocking
timeout = N - blocking until read or timeout
"""
if timeout == None:
timeout = self.timeout
if timeout == 0:
self.sock.setblocking(False)
try:
self.buf += self.sock.recv(4096)
except SocketError:
pass
return
if timeout == -1:
self.sock.settimeout(None) # blocking, infinity timeout
else:
self.sock.setblocking(True) # it's overriden by settimeout, but for clarity
self.sock.settimeout(timeout)
buf = self.sock.recv(4096)
self.eof = (not buf)
self.buf += buf
return
def set_socket(self, sock):
self.sock = sock
return
def get_socket(self):
return self.sock
def get_fileno(self):
return self.sock.fileno()
def write(self, s):
return self.sock.sendall(s)
def send(self, s):
return self.sock.sendall(s)
def shut_wr(self):
self.sock.shutdown(socket.SHUT_WR)
def shut_rd(self):
self.sock.shutdown(socket.SHUT_RD)
def close(self):
return self.sock.close()
def __del__(self):
self.sock.close()
class Sock6(Sock):
SOCKET_FAMILY = socket.AF_INET6
# Class to convert a socket into Sock class
# client sockets (returned by 'accept') can be coverted too
class toSock(Sock):
def __init__(self, sock, timeout=None):
self.timeout = timeout
self.buf = ""
self.eof = False
self.sock = sock
if timeout is None:
self.timeout = DEFAULT_TIMEOUT
self.sock.settimeout(timeout)
return