You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-console-gui/gui/conf_connection.py

159 lines
5.8 KiB

#!/usr/bin/python
# -*- coding: utf-8 -*-
from PySide import QtGui, QtCore
from calculate.api.client.client_class import Client_suds
from calculate.api.client.client_class import HTTPSClientCertTransport
from suds import WebFault
from suds.transport import TransportError, Request
from calculate.api.cl_api import DataVarsApi
from calculate.api.client.function import *
from calculate.api.client.pid_information import *
from calculate.api.client.cert_func import *
from calculate.api.client.cert_verify import get_CRL, VerifyError
from calculate.api.client.sid_func import *
import soap
from soap.more import https_server, client_signal, show_msg, test
class Conn(QtGui.QWidget):
def __init__(self, ClientObj):
super(Conn, self).__init__()
self.ClientObj = ClientObj
self.initUI()
def initUI(self):
grid = QtGui.QGridLayout()
grid.setSpacing(10)
self.lbl_host = QtGui.QLabel("Host: ")
self.lbl_port = QtGui.QLabel("Port: ")
self.text_host = QtGui.QLineEdit(self.ClientObj.default_host)
self.text_port = QtGui.QLineEdit(self.ClientObj.default_port)
grid.setRowMinimumHeight(0,20)
grid.setColumnMinimumWidth(0,30)
# grid.addWidget(QtGui.QSpacerItem(10,10),0,0)
grid.addWidget(self.lbl_host, 1, 1)
grid.addWidget(self.lbl_port, 2, 1)
grid.addWidget(self.text_host, 1, 2)
grid.addWidget(self.text_port, 2, 2)
self.cmd_connect = QtGui.QPushButton('Connect')
grid.addWidget(self.cmd_connect, 3, 2)
self.cmd_connect.connect \
(self.cmd_connect, QtCore.SIGNAL('clicked()'), self.onClick)
self.setLayout(grid)
self.setGeometry(0, 0, 150, 180)
self.show()
# establish a connection
def onClick(self):
# write parameters
self.str_host = self.text_host.text()
self.str_port = self.text_port.text()
self.lbl_host.setText(self.str_host)
self.lbl_port.setText(self.str_port)
try:
int_port = int(self.str_port)
except:
show_msg("Enter correctly port!")
return 1
# get server hostname
url = "https://%s:%d/?wsdl" %(self.str_host, int_port)
path_to_cert = self.ClientObj.path_to_cert
try:
client = Client_suds(url, transport = \
HTTPSClientCertTransport(None,None, path_to_cert))
except:
show_msg("Not connected!")
return 1
server_host_name = client.service.get_server_host_name()
del (client)
self.lbl_port.setText(server_host_name)
try:
import glob
all_cert_list = glob.glob(path_to_cert + '*.crt')
fit_cert_list = []
for client_cert_path in all_cert_list:
client_cert = client_cert_path.replace(path_to_cert, '')
client_cert_name = client_cert.replace('.crt', '')
if server_host_name.endswith(client_cert_name):
fit_cert_list.append(client_cert_name)
fit_cert_list.sort(key = len)
Connect_Error = 1
for i in range (0, len(fit_cert_list)):
#print 'fit_cert_list = ',fit_cert_list
cert_name = fit_cert_list.pop()
CERT_FILE = path_to_cert + cert_name + '.crt'
CERT_KEY = path_to_cert + cert_name + '.key'
# print "cert_name = ",cert_name
try:
self.ClientObj.client = Client_suds(url,\
transport = HTTPSClientCertTransport(CERT_KEY, \
CERT_FILE, path_to_cert))
self.ClientObj.client.set_parameters \
(path_to_cert, CERT_FILE, CERT_KEY)
client_post_cert(self.ClientObj.client)
Connect_Error = 0
except VerifyError, e:
show_msg (e.value)
Connect_Error = 1
except Exception, e:
print 1111111
print dir(e)
show_msg (e.message)
Connect_Error = 1
#sys.exit()
if Connect_Error == 0:
break
#If the certificate file misses
if Connect_Error:
CERT_FILE = None
CERT_KEY = None
self.ClientObj.client = Client_suds(url,\
transport = HTTPSClientCertTransport(CERT_KEY, CERT_FILE,\
path_to_cert))
self.ClientObj.client.set_parameters (path_to_cert, CERT_FILE, CERT_KEY)
Vars = DataVarsApi()
Vars.importApi()
Vars.flIniFile()
try:
self.ClientObj.client.frame_period = Vars.Get('cl_api_get_frame_period')
except:
self.ClientObj.client.frame_period = 2
signaling = threading.Thread(target=client_signal, args = (self.ClientObj.client, ))
signaling.setDaemon(True)
https_server(self.ClientObj.client, signaling)
#----------------------------------------------------
except WebFault, f:
show_msg ("Exception: %s" %f)
print f.fault
except TransportError, te:
show_msg ("Exception: %s" %te)
except Exception, e:
show_msg ("Exception: %s" %e)
# tb.print_exc()
class FrameConnection (QtGui.QScrollArea):
def __init__(self, parent, ClientObj):
self.ClientObj = ClientObj
QtGui.QScrollArea.__init__(self)
# верхнее правое
connection = Conn(self.ClientObj)
self.setWidget(connection)
# self.resize(700,900)