Auxiliary remote server to download files

A recent study found that doing depth model download speed is too slow, the reason may be outside the network. So did a small plug-in, using the auxiliary remote server to download files.

You need to prepare:
a remote server (with TCP Interface), a local computer (python environment)
required knowledge:
1, PyQt basics
2, TCP knowledge
3, QT multithreading knowledge

PYQT do multi-threaded interface &

If you do not understand PYQT, reference may be five minutes to master.

As shown, we set up our UI interface, three main parts: the port number, the download link, log.
Here Insert Picture Description
The code can refer to:

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.ok = False
        self.port_default = 0
        self.content_tmp = ''
        self.initUI()
    #UI界面涉及
    def initUI(self):
        #1 说明内容
        self.lbl2 = QLabel(self)
        self.lbl2.setText(
            "1、You should input Port first.\n2、Enter the URL and click the download button\n3、If you have some problem, send email to\n [email protected]")
        self.lbl2.move(30, 10)
        #2 下载区
        #2.1 文本URL
        self.lbl = QLabel(self)
        self.lbl.setText("Url:")
        self.lbl.move(30, 93)
        #2.2 链接URL
        self.file_url = QLineEdit(self)
        self.file_url.move(50, 90)
        self.file_url.setGeometry(50, 90, 250, 23)
        #2.3 下载按钮
        download_button = QPushButton('Download', self)
        download_button.clicked.connect(self.download)
        download_button.move(320, 90)
        #3 端口号按钮
        self.download_button = QPushButton('No Port', self)
        self.download_button.clicked.connect(self.login)
        self.download_button.move(400, 10)
        #4 日志
        self.tmp = QTextEdit(self)
        self.tmp.move(50, 120)
        self.tmp.setGeometry(50, 120, 350, 70)

        #5 标题
        self.setGeometry(300, 300, 500, 200)
        self.setWindowTitle('服务器加速下载器@cloudcver')
        self.show()
    def login(self):
        text, ok = QInputDialog.getText(self, 'Set Port',
                                        'Enter your port:')
        if ok:
            self.port_default = int(text)
            self.download_button.setText(text)
            self.content_tmp = self.content_tmp + "%s:Set port at %s\n"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),text)
            self.tmp.setText(self.content_tmp)
            self.ok = True
    def download(self):
        if self.ok:
            self.content_tmp = self.content_tmp + "%s:Send the task to server\n" % (
                time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
            self.tmp.setText(self.content_tmp)
            self.thread = DownloadThread()
            self.thread.set_port_url(self.port_default,self.file_url.text())  # 创建
            self.thread.trigger.connect(self.finish)  # 连接信号
            self.thread.start()  # 开始线程
        else:
            reply = QMessageBox.question(self, 'Message',
                                         "You should set port first", QMessageBox.Yes)
    def finish(self):
        self.content_tmp = self.content_tmp + "%s:Finish!\n" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        self.tmp.setText(self.content_tmp)

The interface is very simple not say, mainly talk about multi-threading content!
Focus look here!
Focus look here!
Focus look here!

1, see the download function, ok this parameter represents the port number has been set. Initial ok is False, in Port button input after the changes to True.
2, where we launched a multi-threaded DownloadThread (), which defines a thread class that the following are a few ways:
2.1 set_port_url () method is our own definition, in order to set some parameters.
2.2 run is a function of a parent class, we need to reconstruct it, this is the function to be executed when the execution thread. That is self.thread.start () function after the start of the implementation of this run.
Pyqt 2.3 trigger is a signal that we created, we can connect it to a function, and then perform the signal after passing exit in the run, perform the function it represents the connection after the launch.
Specific code can be seen:

class DownloadThread(QThread):
    trigger = pyqtSignal()

    def __int__(self):
        super(DownloadThread, self).__init__()
    def set_port_url(self,port_default,url):
        self.port_default = port_default
        self.url = url
    def run(self):
		##一会补充!
        # # 循环完毕后发出信号
        # print('1线程运行完了')
        self.trigger.emit()

TCP service

Published 139 original articles · won praise 26 · views 30000 +

Guess you like

Origin blog.csdn.net/CLOUD_J/article/details/104752716