Currently, for these few days I have created a working code that works as I would expect, but there is one significant problem that I do not know how to solve.
But let's start from the beginning. I found something like pexpect and I wanted to use it on linux, instead of the usual "os.system". I'm going to use windscribe here...
def connect():
username = "username1"
while True:
child = pexpect.spawn('windscribe account')
match = child.expect("ee")
rem = re.compile(r'(\x9B|x1B\[)[0-?]*[ -/]*[@-~]')
v = rem.sub('', str(child.before.decode('UTF-8')))
sta = v.split("\n")
for line in sta:
if line.startswith("Username:"):
user = line.replace('Username: ', '')
if line.startswith("Data Usage:"):
dataus = line.replace('Data Usage: ', '').replace('GB / 10 GB', '')
print("The username is: ", username)
print("Data used: ", dataus)
if username == user:
print("the same")
if float(dataus) > 8:
print("You used too much data!")
else:
print("not the same")
Unfortunately, when I try to display the currently logged in user, or check how much the limit has been used, theoretically everything works fine. Displays:
The username is: username1
Data used: 7.79
**not the same**
But when I want to apply it in the program, i.e. compare:
if float(dataus) > 8:
I get an error that the string cannot be formatted to float and its preview is: "7.79 \x1b[0m\r"
Same with the username you want to compare in:
if username == user:
Theoretically they are the same, but not.
The username is: username1 \x1b[0m\r
Data used: 7.79 \x1b[0m\r
I temporarily got around this with this trick, but I don't quite understand why that code doesn't work if I decode it to utf-8 and I think I created a good regex
for line in sta:
if line.startswith("Username:"):
user = line.replace('Username: ', '').replace("\x1b[0m\r", '')
if line.startswith("Data Usage:"):
dataus = line.replace('Data Usage: ', '').replace('GB / 10 GB', '').replace("\x1b[0m\r", '')
Question 2 How i can print output from sent command? because now i must cut last two "ee" words, to print whole output ^^. Mean:
child = pexpect.spawn('windscribe account')
match = child.expect("ee")
rem = re.compile(r'(\x9B|x1B\[)[0-?]*[ -/]*[@-~]')
v = rem.sub('', str(child.before.decode('UTF-8')))
Question 3 Is pexpect is good for that usage? Or better way is use ex:
stdoutdata = subprocess.getoutput("windscribe status")
sta = stdoutdata.strip()
if "CONNECTED -- " in sta:
...