#!/usr/bin/python
import os
import re
import sys
import urllib
"""Logpuzzle exercise
Given an apache logfile, find the puzzle urls and download the images.
Here's what a puzzle url looks like:
10.254.254.28 - - [06/Aug/2007:00:13:48 -0700] "GET /~foo/puzzle-bar-aaab.jpg HTTP/1.0" 302 528 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"
"""
def read_urls(filename):
"""Returns a list of the puzzle urls from the given log file,
extracting the hostname from the filename itself.
Screens out duplicate urls and returns the urls sorted into
increasing order."""
hostname=os.path.basename(filename)[os.path.basename(filename).index('_')+1:]
cont=open(filename).read()
all_urls=re.findall(r'GET (\S+)',cont)
urls=[]
for url in all_urls:
if 'images/puzzle' in url and 'http://'+hostname+'/'+url not in urls:
urls.append('http://'+hostname+'/'+url)
return sorted(urls)
def download_images(img_urls, dest_dir):
"""Given the urls already in the correct order, downloads
each image into the given directory.
Gives the images local filenames img0, img1, and so on.
Creates an index.html in the directory
with an img tag to show each local image file.
Creates the directory if necessary.
"""
f=open(os.path.join(dest_dir,'index.html'),'w')
f.write('<html><body>')
temp=0
for url in img_urls:
image=urllib.urlretrieve(url,os.path.join(dest_dir,'img'+str(temp)+'.jpg'))
f.write('<img src='+os.path.join(dest_dir,'img'+str(temp))+'.jpg>')
temp=temp+1
f.write('</body></html>')
f.close()
def main():
args = sys.argv[1:]
if not args:
print 'usage: [--todir dir] logfile '
sys.exit(1)
todir = ''
if args[0] == '--todir':
todir = args[1]
del args[0:2]
img_urls = read_urls(args[0])
if todir:
download_images(img_urls, todir)
else:
print '\n'.join(img_urls)
if __name__ == '__main__':
main()
分享到:
相关推荐
pip安装,用于已有python2.7,但没有pip的情况下,下载后直接python get-pip2.7.py即可自动安装pip
Python是世界上最受欢迎的编程语言之一,特别是在数据科学、机器学习和Web开发领域。在Python的生态系统中,`pip`是一个至关...通过下载和使用`get-pip.py`,你可以确保在Python 2.7环境中也能享受到`pip`带来的便利。
Python 2.7.x是Python编程语言的一个历史悠久的版本,主要在Windows平台上广泛使用。这个压缩包包含了Python 2.7.x的32位和64位安装程序,分别是"python-2.7_32.15.msi"和"python-2.7.13.amd64.msi"。 1. **Python ...
MySQL-python-1.2.3.win32-py2.7.msi
python learning.py,非常适合用于学习Python的小伙伴~
标题中的"PIL-1.1.7.win32-py2.7.exe"是指Python Imaging Library(PIL)的一个特定版本,适用于32位Windows操作系统,并且与Python 2.7版本兼容。PIL是Python中用于图像处理的库,提供了丰富的图像处理功能,包括...
python_base.py: 千行代码入门Python python_visual.py: 15张图入门Matplotlib python_visual_animation.py: 使用Matplotlib画动态图实例 python_spider.py: 一个很“水”的Python爬虫入门代码文件 python_weibo.py:...
这个压缩包包含了两个版本的MySQL-python安装程序:一个适用于32位系统(MySQL-python-1.2.3.win32-py2.7.exe),另一个适用于64位系统(MySQL-python-1.2.3.win-amd64-py2.7.exe)。这两个文件都是为Python 2.7版本...
Python 2.7是Python编程语言的一个重要版本,特别是在它发布的时候,它是广泛使用的版本之一。这个AMD64.msi安装包是专为64位Windows操作系统设计的,确保在这些系统上无缝运行Python解释器及其相关的库和工具。...
收集了很久的Window64位Python扩展包,是python的开发必备,列表如下,pycrypto-2.6.win64-py2.7\pywin32-216.win64-py2.7\PyYAML-3.10.win64-py2.7\pyzmq-2.1.11.win64-py2.7\MySQL-python-1.2.3.win64-py2.7\...
**Python库:Hyperlambda-0.0.2-py2.7.egg** Hyperlambda是Python编程语言中的一款开源库,主要用于简化复杂的数据处理和配置管理任务。在Python的生态系统中,库扮演着至关重要的角色,它们扩展了Python的功能,...
包含Python MySQL-python-1.2.3.win32-py2.7.msi setuptools-0.6c11.win32-py2.7.exe chromedriver_win_23.0.1240.0.zip IEDriverServer_Win32_2.25.3.zip
pandas-0.10.1.win32-py2.7.exe pandas-0.10.1.win32-py2.7.exe pandas-0.10.1.win32-py2.7.exe pandas-0.10.1.win32-py2.7.exe pandas-0.10.1.win32-py2.7.exe pandas-0.10.1.win32-py2.7.exe pandas-0.10.1.win32-...
pandas-0.10.0.win32-py2.7.exe pandas-0.10.0.win32-py2.7.exe pandas-0.10.0.win32-py2.7.exe pandas-0.10.0.win32-py2.7.exe pandas-0.10.0.win32-py2.7.exe pandas-0.10.0.win32-py2.7.exe pandas-0.10.0.win32-...
标题中的"libpython2.7.so.1"是一个动态链接库文件,它是Python 2.7版本的核心库在Linux系统中的实现。动态链接库(Dynamic Link Library)是Linux系统中的一种共享对象,允许多个程序同时使用同一份代码,节省内存...
python ipin.py
在window下安装的时候可能没有自带安装easy_install 需要我们手动安装首先下载easy_install的安装包,下载地址:http://pypi.python.org/pypi/setuptools在页面上找到 ez_setup.py下载...PyYAML-3.10.win32-py2.7.exe
python-dateutil-2.2.win32-py2.7.exe,python-dateutil-2.2.win32-py2.7.exe
【VCForPython27.msi】是微软Visual C++ for Python 2.7的安装程序,它是Python 2.7版本的一个重要组件。这个安装包主要用于解决Python在运行时依赖的C++编译器库问题,特别是对于那些使用了C扩展模块的Python库,如...
Python 2.7是Python编程语言的一个早期稳定版本,尤其对于Windows用户来说,它提供了在该操作系统上进行开发和执行Python程序的能力。本压缩包包含了Python 2.7的Windows版安装所需的所有基本组件和库。 `README....