servent.py
3.23 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
# from PortGlosa import traduzir
import os
import select
import socket
import subprocess
import sys
import threading
import time
import vlibrashook
from PortGlosa import traduzir
class Servent():
def __init__(self):
self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# self.__sock.setblocking(1)
def receive(self, connection, address_nvda):
try:
while True:
data = connection.recv(1024)
if len(data) > 0:
print("Received: %s %s" % (address_nvda, data))
if (self.client.connected()):
#translated = data
print "Traduzindo."
translated = traduzir(data + "\n")
print("Translated: %s" % (translated))
if (self.client.send(translated)):
print("Sent: %s" % (translated))
else:
break
except Exception as e:
try:
connection.shutdown(1)
connection.close(1)
except Exception as e:
pass
def accept(self, host = "127.0.0.1", port = 10001):
self.client = vlibrashook.client(host, 10000)
self.client.start()
self.__sock.bind((host, port))
self.__sock.listen(1)
time.sleep(1)
print("Listen: %s:%s" % (host, port))
while True:
print("Waiting for NVDA client: %s:%s" % (host, port))
(connection, address_nvda) = self.__sock.accept()
print("Client NVDA connected!")
# ---------------------------------------------------------------------------------
# single thread
# ---------------------------------------------------------------------------------
self.receive(connection, address_nvda)
# ---------------------------------------------------------------------------------
# multithread
# ---------------------------------------------------------------------------------
# t = threading.Thread(target = self.receive, args = ((connection, address_nvda)))
# self.__threadlist.append(t)
# t.start()
def file_exists(file_path):
if ((os.path.isfile(file_path) == 1) and (os.path.exists(file_path) == 1)):
return True
else:
return False
def main():
nvda_slave_x86 = "C:\\Program Files (x86)\\NVDA\\nvda_slave.exe"
nvda_slave_x64 = "C:\\Program Files\\NVDA\\nvda_slave.exe"
if (file_exists(nvda_slave_x86)):
nvda_slave = nvda_slave_x86
elif(file_exists(nvda_slave_x64)):
nvda_slave = nvda_slave_x64
else:
print("NVDA slave not found: %s" % (nvda_slave_x86))
print("NVDA slave not found: %s" % (nvda_slave_x64))
sys.exit(1)
# try:
# # subprocess.call(["python", ".\\nvda\\source\\nvda.pyw"])
# nvdaproc = subprocess.call([nvda_slave, "launchNVDA", "-r"])
# except:
# print("Erro no subprocesso NVDA")
# sys.exit(1)
servent = Servent()
servent.accept()
if __name__ == "__main__":
main()