def get_images(task): destiny = basedir + "/taken_files/image/" + str(task.id) if not path.exists(destiny): makedirs(destiny) task.status = "started" db.session.commit() http = PoolManager() page = http.request("GET", task.url) content_page = page.data coding = page.getheader('charset') soup = BeautifulSoup(content_page, "html.parser", from_encoding=coding) images_list = soup.find_all('img') for image in images_list: file_name = image['src'].split("/")[-1] path_file = destiny + "/" + file_name p = Paths(task_id=task.id, path=str(path_file)) db.session.add(p) db.session.commit() try: with open(path_file, "wb") as out_image: http = PoolManager().request("GET", task.url + image['src'], preload_content=False) for chunk in http.stream(1024): out_image.write(chunk) except Exception as e: task.status = "error: {}".format(e) task.status = "finished"