簡體   English   中英

如何將pyngrok與多線程一起使用?

[英]How to use pyngrok with multi-threading?

我正在嘗試運行具有 3 個線程的 python 腳本,其中一個是使用線程模塊使用 pyngrok 將即將到來的連接端口轉發到本地主機,但問題是每當 ngrok 服務器啟動時,它都會掛起所有其他線程......

    def CreateTunnel(self):
        try:
            ngrok.set_auth_token(self.AuthToken)

            print(f"{Fore.GREEN}[+] Opening TCP tunnel 👌{Style.RESET_ALL}")
            tunnel = ngrok.connect(addr=self.port, proto="tcp")

            print(f"{Fore.GREEN}[+] Public URL:{Style.RESET_ALL} {Fore.CYAN}{ngrok.get_tunnels()[0]}{Style.RESET_ALL}")
            ngrok_proc = ngrok.get_ngrok_process()
            self.pub_url = tunnel.public_url.split("tcp://")[1]
            self.ngrokPort = tunnel.public_url.split("tcp://")[1].split(":")[1]

            ngrok_proc.proc.wait()  # Hangs the process and other threads stops execution

如何同步運行 3 個線程? 或者我如何在不需要使用 proc.wait() 的情況下設置 ngrok 服務器?

這是線程的調用方式:

def Threads(self):
        # Adding them to a list
        if self.ngrok:
            self.jobs.append(th(target=self.CreateTunnel()))

        self.jobs.append(th(target=self.listen()))
        self.jobs.append(th(target=self.brute()))

        # Starting them
        for job in self.jobs:
            th.setDaemon(True)
            th.start()

看一下這個

def Threads(self):
    # Adding them to a list
    if self.ngrok:
        self.jobs.append(th(target=self.CreateTunnel()))

    self.jobs.append(th(target=self.listen()))
    #self.jobs.append(th(target=self.brute()))

    # Starting them
    for job in self.jobs:
        th.setDaemon(True)
        th.start()


def CreateTunnel(self):
    try:
        ngrok.set_auth_token(self.AuthToken)

        print(f"{Fore.GREEN}[+] Opening TCP tunnel 👌{Style.RESET_ALL}")
        tunnel = ngrok.connect(addr=self.port, proto="tcp")

        print(f"{Fore.GREEN}[+] Public URL:{Style.RESET_ALL} {Fore.CYAN}{ngrok.get_tunnels()[0]}{Style.RESET_ALL}")
        ngrok_proc = ngrok.get_ngrok_process()
        self.pub_url = tunnel.public_url.split("tcp://")[1]
        self.ngrokPort = tunnel.public_url.split("tcp://")[1].split(":")[1]

        ngrok_proc.proc.wait()  # Hangs the process and other threads stops execution

祝你好運♂️♂️

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM