/
connections.py
75 lines (60 loc) · 2.28 KB
/
connections.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
import socket
def my_ips():
from subprocess import check_output
raw = check_output(['hostname', '--all-ip-addresses'])
ips = raw.decode("utf-8")
ips = ips.strip().split()
return ips
def pingable(ip):
import os
response = os.system("ping -c 1 " + ip + " >/dev/null")
if response == 0:
return True
else:
return False
class TCP(object):
def __init__(self, ip='localhost', port=8089, type=None, backlog=0):
self.ip = ip
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if type is None:
if ip in my_ips():
print("TCP object being set as server!")
self.sock.bind((self.ip, self.port))
self.sock.listen(backlog)
self.connection, address = self.sock.accept()
elif pingable(ip):
print("TCP object being set as client!")
self.sock.connect((self.ip, self.port))
self.connection = self.sock
else:
raise ConnectionError(
"Couldn't locate ip %s" % ip)
if type is not None:
if type.lower() == "server":
self.sock.bind((self.ip, self.port))
self.sock.listen(backlog)
self.connection, address = self.sock.accept()
elif type.lower() == "client":
self.sock.connect((self.ip, self.port))
self.connection = self.sock
else:
raise TypeError(
"Possible input values for argument `type` are either 'server' or 'client'.")
def server(self, backlog=0):
if not self.server_set:
self.sock.bind((self.ip, self.port))
self.sock.listen(backlog)
self.connection, address = self.sock.accept()
return self.connection
def client(self):
self.sock.connect((self.ip, self.port))
self.connection = self.sock
return self.connection
def recv(self, length=4096):
return self.connection.recv(length)
def send(self, data):
self.connection.sendall(data)
def timeout(self, time):
self.connection.settimeout(time)