-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscottSock.py
More file actions
77 lines (61 loc) · 1.46 KB
/
scottSock.py
File metadata and controls
77 lines (61 loc) · 1.46 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
72
73
74
75
76
77
#!/usr/bin/env python3
# +
# import(s)
# -
import socket
import sys
import select
# +
# class: scottSock()
# -
# noinspection PyPep8Naming
class scottSock(socket.socket):
# +
# method; __init__()
# -
def __init__(self, host, port, timeout=None):
self.host = host
self.port = int(port)
self.timeout = int(timeout)
socket.socket.__init__(self, socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(host)
self.connect((host, int(port)))
# +
# method: talk()
# -
def talk(self, message):
self.send(message)
# +
# method: listen()
# -
def listen(self, endchar='\n'):
test = True
resp = b""
timeout = 0.1
while test:
try:
ready = select.select([self], [], [], timeout)
if ready[0]:
new_stuff = self.recv(128)
else:
new_stuff = ""
except socket.timeout:
return resp
if new_stuff:
resp += new_stuff
else:
return resp
timeout = 0.01
# +
# method: converse()
# -
def converse(self, message, endchar='\n'):
self.talk(message)
return self.listen(endchar=endchar)
# +
# main()
# -
if __name__ == '__main__':
if len(sys.argv) > 3:
soc = scottSock(sys.argv[1], sys.argv[2])
print(soc.talk(sys.argv[3]))