ssh tunnel console vs GUI - co jest grane?

0

Witam,

Wersja konsolowa działa dobrze w ten sposób:

from sshtunnel import SSHTunnelForwarder
import requests
 
ssh_host = 'example.com'
ssh_user = 'user1'
local_port = 3081
remote_host = '172.16.0.4'
remote_port = 81
ssh_private_key_path = "/path/to/private/key"
 
with SSHTunnelForwarder(
    (ssh_host, 22),
    ssh_username=ssh_user,
    ssh_private_key=ssh_private_key_path,
    remote_bind_address=(remote_host, remote_port),
    local_bind_address=('127.0.0.1', local_port)
) as tunnel:
    response = requests.get(f'http://127.0.0.1:{local_port}/login')
    print(response.text)
> <!DOCTYPE html>
> <html lang="en">
> <head>
>     <meta charset="UTF-8">
>     <meta name="viewport" content="width=device-width, initial-scale=1.0">
>     <title>Login</title>
> </head>
> <body>
>     <h1>Login</h1>
> ...
> </body>
> </html>

ale wersja GUI w jakiś sposób zwraca błąd:

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
from sshtunnel import SSHTunnelForwarder
import sys
 
ssh_host = 'example.com'
ssh_user = 'user1'
local_port = 3081
remote_host = '172.16.0.4'
remote_port = 81
ssh_private_key_path = "/path/to/private/key"
 
class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.browser = QWebEngineView()
        self.setCentralWidget(self.browser)
 
    def set_tunnel(self, tunnel):
        self.tunnel = tunnel
 
    def load_url(self):
        self.browser.setUrl(QUrl(f'http://127.0.0.1:{local_port}/login'))
 
app = QApplication(sys.argv)
 
with SSHTunnelForwarder(
    (ssh_host, 22),
    ssh_username=ssh_user,
    ssh_private_key=ssh_private_key_path,
    remote_bind_address=(remote_host, remote_port),
    local_bind_address=('127.0.0.1', local_port)
) as tunnel:
    window = MainWindow()
    window.set_tunnel(tunnel)
    window.load_url()
    window.show()
 
sys.exit(app.exec_())

ć
Czy ktoś ma pomysł dlaczego???...

P.S.: Próbowałem timeout i takie tam, bez skutku

QTimer.singleShot(6000, lambda: self.browser.setUrl(QUrl(f'http://127.0.0.1:{local_port}/')))

time.sleep(5)
0
sys.exit(app.exec_())

To było poza with, więc tunel został zamknięty przed uruchomieniem GUI.
Wreszcie działa OK...

1 użytkowników online, w tym zalogowanych: 0, gości: 1