-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwUtils.py
146 lines (116 loc) · 3.15 KB
/
dwUtils.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
'''
darkWater Utils
just some junk to make processing data easier
'''
import os
import random
import string
import socket
class masterSocket:
def new(self, ip, port, buff):
self.ip = ip
self.port = port
self.sock = None
self.buff = buff
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((self.ip, self.port))
s.listen(1)
except Exception as e:
print(str(e))
else:
self.sock = s
return self.sock
def write(self, data):
self.sock.send(bytes(data), 'utf-8')
def read(self):
return self.sock.recv(self.buff).decode('utf-8')
def close(self):
self.sock.close()
def accept(self):
return self.sock.accept()
class syncSock:
def __init__(self, ip, port, buff):
self.ip = ip
self.port = port
self.sock = None
self.buff = buff
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.ip, self.port))
except:
return False
else:
self.sock = s
def write(self, data):
self.sock.send(bytes(data), 'utf-8')
def read(self):
return self.sock.recv(self.buff).decode('utf-8')
def close(self):
self.sock.close
def cleanPath(thsPath):
if not thsPath[len(thsPath) - 1] == '/':
thsPath = thsPath + '/'
if thsPath[0] == '~':
thsPath = os.path.expanduser('~') + thsPath[1:]
return thsPath
def fileSearch(thsFile, thsLocation):
if thsFile is None:
fList = ''
for roots, dirs, files in os.walk(thsLocation):
for f in files:
fList = fList + f + ':'
return fList
elif not thsFile is None:
for roots, dirs, files in os.walk(thsLocation):
for f in files:
if f == thsFile:
return os.path.join(roots, f)
return None
else:
print('Err in fileSeach - dwUtils')
return None
def byteStr2Int(byteStr):
alpha = []
kb = 1024
mb = kb * kb
gb = mb * kb
for i in string.ascii_lower():
alpha.extend(i)
for i in byteStr:
if byteStr[i] in alpha:
num = int(byteStr[:i - 1])
if byteStr[i] == 'k':
return(int(num * kb))
elif byteStr[i] == 'm':
return(int(num * mb))
elif byteStr[i] == 'g':
return(int(num * gb))
return None
def swirl():
return chr(random. randrange(5163, 5189, 1))
def strTrim(thsString):
l = len(thsString)
thsString = thsString.replace('\n', '')
while thsString[l - 1] == ' ':
l -= 1
return thsString[0:l]
def getCMD(thsString, cmdPos):
x = []
x.extend(thsString.split(':'))
if cmdPos == 0:
xList = ''
for i in x:
i = strTrim(i)
if not i == '':
xList = xList + i
return xList
else:
try:
x = strTrim(x[cmdPos - 1])
except:
return None
if x == '':
return None
else:
return x