我尝试创建一个线程,它将下载多个文件,并命名它们



我想从网上下载文件并命名它们,再加上我想创建一个线程,这样我就可以同时下载不同的文件。

import urllib.request
import threading

class mythread(threading.Thread):
def __init__(self, threadID, Name, URL):
threading.Thread.__init__(self)
self.threadID = threadID
self.Name = Name
self.URL = URL
def run(self):
print("Starting :" + self.Name + " n ")
download_data(self.URL, 1, self.Name)
print("Exiting :" + self.Name + " n ")

def download_data(URL, delay, Name):
URL = " "
response = urllib.request.urlopen(URL)
data = response.read()
text = data.decode('utf-8')
while text:
time.sleep(delay)
if text is None:
break
print(text)     
thread1 = mythread(1, romeo, https: // www.py4e.com/code3/romeo-full.txt) 
thread2 = mythread(2, romeo2, https: // www.py4e.com/code3/romeo.txt)

thread1.start()
thread2.start()
thread1.join()
thread2.join()

不需要打印,我这样做只是为了调试。

这是一些示例站点

为什么要覆盖URL参数?您试过删除这一行吗?

你可能会受益于更多的日志记录,什么是调试器,它如何帮助我诊断问题?

最新更新