17.12.2012 Views

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

370<br />

La bibliothèque standard<br />

TROISIÈME PARTIE<br />

# mise en place du handler<br />

handler = logging.StreamHandler()<br />

logger.addHandler(handler)<br />

def get_page(url):<br />

"""Extrait le contenu et les liens"""<br />

# VOIR EXERCICE 12<br />

return '', []<br />

def process_url(query, url):<br />

"""Traite une page et ses liens"""<br />

logger.info('Processing %s' % url)<br />

if len(processed_urls) >= 100:<br />

return<br />

if url in processed_urls:<br />

return<br />

try:<br />

content, links = get_page(url)<br />

processed_urls[url] = query in content<br />

for link in links:<br />

process_url(query, url)<br />

except TimeoutError:<br />

pass<br />

def launch_work(query, urls):<br />

logger.info('Launching process')<br />

pool = Pool(4)<br />

try:<br />

results = [pool.apply_async(process_url, (query, url))<br />

for url in urls]<br />

for res in results:<br />

res.get()<br />

finally:<br />

pool.close()<br />

pool.join()<br />

logger.info('Done.')<br />

URLS = ['http://python.org',]<br />

if __name__ == '__main__':<br />

launch_work(sys.argv[1], URLS)<br />

for url, found in processed_urls.items():<br />

if not found:<br />

continue<br />

print url

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!