<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Mozillazg&#039;s Blog</title>
	<atom:link href="http://mozillazg.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mozillazg.wordpress.com</link>
	<description>Just another WordPress.com site</description>
	<lastBuildDate>Wed, 04 Jan 2012 10:05:26 +0000</lastBuildDate>
	<language>zh-cn</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mozillazg.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Mozillazg&#039;s Blog</title>
		<link>http://mozillazg.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mozillazg.wordpress.com/osd.xml" title="Mozillazg&#039;s Blog" />
	<atom:link rel='hub' href='http://mozillazg.wordpress.com/?pushpress=hub'/>
		<item>
		<title>[Python]从爱词霸下载单词读音音频文件</title>
		<link>http://mozillazg.wordpress.com/2011/12/30/python-get-iciba-com-audio-file/</link>
		<comments>http://mozillazg.wordpress.com/2011/12/30/python-get-iciba-com-audio-file/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 10:43:43 +0000</pubDate>
		<dc:creator>mozillazg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[BeautifulSoup]]></category>
		<category><![CDATA[爱词霸]]></category>
		<category><![CDATA[读音文件]]></category>
		<category><![CDATA[iciba]]></category>
		<category><![CDATA[mp3]]></category>
		<category><![CDATA[sound]]></category>

		<guid isPermaLink="false">http://mozillazg.wordpress.com/?p=137161</guid>
		<description><![CDATA[主要功能 输入单词下载读音文件 文件保存到单词开头字母的目录下 可选英音或美音，默认为美音 系统环境 Windows 7 Ultimate 32-bit Python 2.6.6 + BeautifulSoup 模块 代码 #!/usr/bin/env python # -*- coding: utf-8 -*- """从爱词霸下载单词读音音频文件 """ import urllib2 import re import os from BeautifulSoup import BeautifulSoup, SoupStrainer def get(word, headers=None, lang='US'): """获取音频文件链接 &#62;&#62;&#62; get('receipt') http://res.iciba.com/resource/amp3/1/0/1e/11/1e11b989ba2f5e161cdad604bf3de90b.mp3 """ # 设置 header user_agent = ('Mozilla/5.0 (Windows NT 6.1; rv:9.0) ' [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137161&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>主要功能</h2>
<ul>
<li>输入单词下载读音文件</li>
<li>文件保存到单词开头字母的目录下</li>
<li>可选英音或美音，默认为美音</li>
</ul>
<h2>系统环境</h2>
<ul>
<li>Windows 7 Ultimate 32-bit</li>
<li>Python 2.6.6 + BeautifulSoup 模块</li>
</ul>
<h2>代码</h2>
<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""从爱词霸下载单词读音音频文件
"""

import urllib2
import re
import os
from BeautifulSoup import BeautifulSoup, SoupStrainer

def get(word, headers=None, lang='US'):
    """获取音频文件链接
    &gt;&gt;&gt; get('receipt')

http://res.iciba.com/resource/amp3/1/0/1e/11/1e11b989ba2f5e161cdad604bf3de90b.mp3

    """
    # 设置 header
    user_agent = ('Mozilla/5.0 (Windows NT 6.1; rv:9.0) '
                  + 'Gecko/20100101 Firefox/9.0')
    if headers is None:
        headers = {'User-Agent' : user_agent}
    url ='http://www.iciba.com/%s/' % urllib2.quote(word)
    # 读取网页内容
    request = urllib2.Request(url=url, headers=headers)
    try:
        html = urllib2.urlopen(request).read()
    except:
        print u'网络故障！'
        return None
    else:
        # 获取发音所在链接标签
        links = SoupStrainer('a', title=re.compile(ur'真人发音|电脑合成语音'))
        if links:
            audios = [str(tag) for tag in BeautifulSoup(html, parseOnlyThese=links)]
            soup = BeautifulSoup(''.join(audios))
            audios = soup.findAll('a')
            # print audios
            if 'US' in lang.upper() and len(audios) &gt; 1: # 'US'
                audio = audios[1]['onclick'].split("'")[1]
            else: # 'UK'
                audio = audios[0]['onclick'].split("'")[1]
            return audio
        else:
            return None

def save(url, word, headers=None, savedir=''):
    """保存音频文件
    """
    if url is None:
        print u'无音频文件可下载！'
    else:
        # 设置 header
        user_agent = ('Mozilla/5.0 (Windows NT 6.1; rv:9.0) '
                      + 'Gecko/20100101 Firefox/9.0')
        if headers is None:
            headers = {'User-Agent' : user_agent}
        headers['Referer'] = 'http://www.iciba.com/%s/' % urllib2.quote(word)
        # 读取网页内容
        request = urllib2.Request(url=url, headers=headers)
        try:
            file_data = urllib2.urlopen(request).read()
        except:
            print u'网络故障！'
        else:
            starts = 'abcdefghijklmnopqrstuvwxyz'
            if not savedir:
                savedir = '.'
            # 将文件保存到单词开头字母的文件夹中
            for i in starts:
                if word.startswith(i):
                    savedir = savedir + os.sep + i
                    break
            else:
                savedir = savedir + os.sep + '0-9'
            if not os.path.exists(savedir):
                os.makedirs(savedir)
            # 保存后的文件路径
            file_name = savedir + os.sep + word +'.mp3'
            # print file_name
            # 如果文件名已存在
            if os.path.exists(file_name):
                print u'同名文件已存在！'
            else:
                with open(file_name,'wb') as output:
                    # 写入数据，即保存文件
                    output.write(file_data)

def main():
    while True:
        word = raw_input("&gt; ").strip()
        if not word: break
        save(get(word, lang='uk'), word, savedir='audio')

if __name__ == '__main__':
    main()
</code></pre>
<p>最新版本：<a href="https://github.com/mozillazg/python-mini-script">get_iciba_word_audio/audio.py</a></p>
<br />Filed under: <a href='http://mozillazg.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mozillazg.wordpress.com/tag/audio/'>audio</a>, <a href='http://mozillazg.wordpress.com/tag/beautifulsoup/'>BeautifulSoup</a>, <a href='http://mozillazg.wordpress.com/tag/%e7%88%b1%e8%af%8d%e9%9c%b8/'>爱词霸</a>, <a href='http://mozillazg.wordpress.com/tag/%e8%af%bb%e9%9f%b3%e6%96%87%e4%bb%b6/'>读音文件</a>, <a href='http://mozillazg.wordpress.com/tag/iciba/'>iciba</a>, <a href='http://mozillazg.wordpress.com/tag/mp3/'>mp3</a>, <a href='http://mozillazg.wordpress.com/tag/python/'>Python</a>, <a href='http://mozillazg.wordpress.com/tag/sound/'>sound</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mozillazg.wordpress.com/137161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mozillazg.wordpress.com/137161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mozillazg.wordpress.com/137161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mozillazg.wordpress.com/137161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mozillazg.wordpress.com/137161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mozillazg.wordpress.com/137161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mozillazg.wordpress.com/137161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mozillazg.wordpress.com/137161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mozillazg.wordpress.com/137161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mozillazg.wordpress.com/137161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mozillazg.wordpress.com/137161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mozillazg.wordpress.com/137161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mozillazg.wordpress.com/137161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mozillazg.wordpress.com/137161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137161&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mozillazg.wordpress.com/2011/12/30/python-get-iciba-com-audio-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://res.iciba.com/resource/amp3/1/0/1e/11/1e11b989ba2f5e161cdad604bf3de90b.mp3" length="4935" type="audio/mpeg" />
<enclosure url="http://res.iciba.com/resource/amp3/1/0/1e/11/1e11b989ba2f5e161cdad604bf3de90b.mp3" length="4935" type="audio/mpeg" />
	
		<media:content url="http://1.gravatar.com/avatar/95032e8eb950c9c93a56b381ee273e15?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mozillazg</media:title>
		</media:content>
	</item>
		<item>
		<title>[python] IOError: [Errno 2] No usable temporary directory found in [&#039;/tmp&#039;, &#039;/var/tmp&#039;, &#039;/usr/tmp&#039;, &#039;/data1/www/htdocs/405/webpytest/1&#039;]</title>
		<link>http://mozillazg.wordpress.com/2011/12/26/python-no-usable-temporary-directory-found-in/</link>
		<comments>http://mozillazg.wordpress.com/2011/12/26/python-no-usable-temporary-directory-found-in/#comments</comments>
		<pubDate>Mon, 26 Dec 2011 09:34:15 +0000</pubDate>
		<dc:creator>mozillazg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[No-usable-temporary-directory-found-in]]></category>
		<category><![CDATA[tempfile]]></category>
		<category><![CDATA[tempfile.tempdir]]></category>

		<guid isPermaLink="false">http://mozillazg.wordpress.com/?p=137155</guid>
		<description><![CDATA[前段时间使用 SAE 时，出现一个有关临时文件目录的错误。 错误信息 IOError: [Errno 2] No usable temporary directory found in ['/tmp', '/var/tmp', '/usr/tmp', '/data1/www/htdocs/405/webpytest/1'] 原因 由于没有 SAE 的系统临时文件目录的可写权限，所以报临时目录错误。 解决 手动指定程序所用的具有可写权限的临时目录 import tempfile tempfile.tempdir = 'tempdir' # tempfile.tempdir = sae.core.get_tmp_dir() # SAE 平台的临时目录 ps1. sae.core.get_tmp_dir() 目录暂时不具有可写权限，会报错。 ps2. 未指定临时目录时， Python 会在以下列表中查找具有可写权限的目录： Python查找一个标准目录列表, 将第一个用户有权限在其中创建文件的目录来设置tempdir . 这个列表是: 环境变量TMPDIR中的目录名. 环境变量TEMP中的目录. 环境变量TMP中的目录. 平台指定的位置: 在RiscOS上, 由Wimp$ScrapDir指定目录名字. 在Windows上, 以C:$backslash$TEMP, C:$backslash$TMP, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137155&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>前段时间使用 SAE 时，出现一个有关临时文件目录的错误。</p>
<h2>错误信息</h2>
<blockquote>
<p>IOError: [Errno 2] No usable temporary directory found in ['/tmp', '/var/tmp', '/usr/tmp', '/data1/www/htdocs/405/webpytest/1'] </p>
</blockquote>
<h2>原因</h2>
<p>由于没有 SAE 的系统临时文件目录的可写权限，所以报临时目录错误。</p>
<h2>解决</h2>
<p>手动指定程序所用的具有可写权限的临时目录        </p>
<pre><code>import tempfile
tempfile.tempdir = 'tempdir'
# tempfile.tempdir = sae.core.get_tmp_dir() # SAE 平台的临时目录
</code></pre>
<p>ps1.   sae.core.get_tmp_dir() 目录暂时不具有可写权限，会报错。</p>
<p>ps2. 未指定临时目录时， Python 会在以下列表中查找具有可写权限的目录：</p>
<blockquote>
<p>Python查找一个标准目录列表, 将第一个用户有权限在其中创建文件的目录来设置tempdir . 这个列表是:</p>
<ol>
<li>
<p>环境变量TMPDIR中的目录名.</p>
</li>
<li>
<p>环境变量TEMP中的目录.</p>
</li>
<li>
<p>环境变量TMP中的目录.</p>
</li>
<li>
<p>平台指定的位置:</p>
<ul>
<li>在RiscOS上, 由Wimp$ScrapDir指定目录名字.</li>
<li>在Windows上, 以C:$backslash$TEMP, C:$backslash$TMP, $backslash$TEMP, 和 $backslash$TMP按序查找目录.</li>
<li>在其他平台上, 以/tmp, /var/tmp, 和/usr/tmp按序查找目录.</li>
</ul>
</li>
<li>
<p>最后一个是当前工作目录.</p>
</li>
</ol>
</blockquote>
<h3>更新 sae.core.get_tmp_dir() 不能用的问题</h3>
<blockquote>
<p>在index.wsgi最前面打上这个patch，可以临时绕过本地文件系统的tempfile</p>
<p>但是需要注意上传文件过大的问题</p>
<pre><code>import os

import tempfile
import cStringIO

def TemporaryFile(mode='w+b', bufsize=-1, suffix="",
                   prefix='', dir=None):
    return cStringIO.StringIO()
tempfile.TemporaryFile = TemporaryFile

import sae
import sae.storage
</code></pre>
</blockquote>
<h2>参考</h2>
<ul>
<li>http://pymotwcn.readthedocs.org/en/latest/documents/tempfile.html</li>
<li>http://www.douban.com/group/topic/26053959/</li>
</ul>
<br />Filed under: <a href='http://mozillazg.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mozillazg.wordpress.com/tag/no-usable-temporary-directory-found-in/'>No-usable-temporary-directory-found-in</a>, <a href='http://mozillazg.wordpress.com/tag/python/'>Python</a>, <a href='http://mozillazg.wordpress.com/tag/tempfile/'>tempfile</a>, <a href='http://mozillazg.wordpress.com/tag/tempfile-tempdir/'>tempfile.tempdir</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mozillazg.wordpress.com/137155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mozillazg.wordpress.com/137155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mozillazg.wordpress.com/137155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mozillazg.wordpress.com/137155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mozillazg.wordpress.com/137155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mozillazg.wordpress.com/137155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mozillazg.wordpress.com/137155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mozillazg.wordpress.com/137155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mozillazg.wordpress.com/137155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mozillazg.wordpress.com/137155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mozillazg.wordpress.com/137155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mozillazg.wordpress.com/137155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mozillazg.wordpress.com/137155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mozillazg.wordpress.com/137155/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137155&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mozillazg.wordpress.com/2011/12/26/python-no-usable-temporary-directory-found-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95032e8eb950c9c93a56b381ee273e15?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mozillazg</media:title>
		</media:content>
	</item>
		<item>
		<title>[vim][python]修改 pylint.vim</title>
		<link>http://mozillazg.wordpress.com/2011/11/05/fx-pylint-vim/</link>
		<comments>http://mozillazg.wordpress.com/2011/11/05/fx-pylint-vim/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 02:58:43 +0000</pubDate>
		<dc:creator>mozillazg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Vim]]></category>
		<category><![CDATA[pylint]]></category>
		<category><![CDATA[pylint.vim]]></category>

		<guid isPermaLink="false">http://mozillazg.wordpress.com/?p=137147</guid>
		<description><![CDATA[最近试用了一下 pylint.vim，结果 :Pylint 命令显示如下结果： 基本上没什么用，因此做了如下更改： 将 69 行附近的 "CompilerSet makeprg=(echo\ '[%]';\ pylint\ -r\ y\ %) 改为： CompilerSet makeprg=pylint\ --reports=n\ --output-format=parseable\ %:p 75行附近的 "CompilerSet efm=%+P[%f],%t:\ %#%l:%m,%Z,%+IYour\ code%m,%Z,%-G%.%# 改为： CompilerSet efm=%A%f:%l:\ [%t%.%#]\ %m,%Z%p^^,%-C%.%# 更改后 :Plint 命令结果如下： 参考 Integrate Pylint and Pychecker support Filed under: Python, Vim Tagged: pylint, pylint.vim, Python, Vim<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137147&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>最近试用了一下 pylint.vim，结果 <code>:Pylint</code> 命令显示如下结果：</p>
<p><img src="https://github.com/mozillazg/my-blog-file/raw/master/2011/11/2011-11-05-09-36-5.jpg" alt="before" title="" /></p>
<p>基本上没什么用，因此做了如下更改：</p>
<p><img src="https://github.com/mozillazg/my-blog-file/raw/master/2011/11/2011-11-05-09-38-4.jpg" alt="change" title="" /></p>
<p>将 69 行附近的</p>
<pre><code>"CompilerSet makeprg=(echo\ '[%]';\ pylint\ -r\ y\ %)
</code></pre>
<p>改为：</p>
<pre><code>CompilerSet makeprg=pylint\ --reports=n\ --output-format=parseable\ %:p
</code></pre>
<p>75行附近的</p>
<pre><code>"CompilerSet efm=%+P[%f],%t:\ %#%l:%m,%Z,%+IYour\ code%m,%Z,%-G%.%#
</code></pre>
<p>改为：</p>
<pre><code>CompilerSet efm=%A%f:%l:\ [%t%.%#]\ %m,%Z%p^^,%-C%.%#
</code></pre>
<p>更改后 <code>:Plint</code> 命令结果如下：</p>
<p><img src="https://github.com/mozillazg/my-blog-file/raw/master/2011/11/2011-11-05-09-37-2.jpg" alt="after" title="" /></p>
<h2>参考</h2>
<ul>
<li><a href="http://vim.wikia.com/wiki/Integrate_Pylint_and_Pychecker_support">Integrate Pylint and Pychecker support</a></li>
</ul>
<br />Filed under: <a href='http://mozillazg.wordpress.com/category/python/'>Python</a>, <a href='http://mozillazg.wordpress.com/category/vim/'>Vim</a> Tagged: <a href='http://mozillazg.wordpress.com/tag/pylint/'>pylint</a>, <a href='http://mozillazg.wordpress.com/tag/pylint-vim/'>pylint.vim</a>, <a href='http://mozillazg.wordpress.com/tag/python/'>Python</a>, <a href='http://mozillazg.wordpress.com/tag/vim/'>Vim</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mozillazg.wordpress.com/137147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mozillazg.wordpress.com/137147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mozillazg.wordpress.com/137147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mozillazg.wordpress.com/137147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mozillazg.wordpress.com/137147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mozillazg.wordpress.com/137147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mozillazg.wordpress.com/137147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mozillazg.wordpress.com/137147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mozillazg.wordpress.com/137147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mozillazg.wordpress.com/137147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mozillazg.wordpress.com/137147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mozillazg.wordpress.com/137147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mozillazg.wordpress.com/137147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mozillazg.wordpress.com/137147/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137147&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mozillazg.wordpress.com/2011/11/05/fx-pylint-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95032e8eb950c9c93a56b381ee273e15?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mozillazg</media:title>
		</media:content>

		<media:content url="https://github.com/mozillazg/my-blog-file/raw/master/2011/11/2011-11-05-09-36-5.jpg" medium="image">
			<media:title type="html">before</media:title>
		</media:content>

		<media:content url="https://github.com/mozillazg/my-blog-file/raw/master/2011/11/2011-11-05-09-38-4.jpg" medium="image">
			<media:title type="html">change</media:title>
		</media:content>

		<media:content url="https://github.com/mozillazg/my-blog-file/raw/master/2011/11/2011-11-05-09-37-2.jpg" medium="image">
			<media:title type="html">after</media:title>
		</media:content>
	</item>
		<item>
		<title>[python]查看文件类型</title>
		<link>http://mozillazg.wordpress.com/2011/11/05/python-view-or-get-the-type-of-a-file/</link>
		<comments>http://mozillazg.wordpress.com/2011/11/05/python-view-or-get-the-type-of-a-file/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 02:57:41 +0000</pubDate>
		<dc:creator>mozillazg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[MIME]]></category>
		<category><![CDATA[mimetypes]]></category>
		<category><![CDATA[文件类型]]></category>

		<guid isPermaLink="false">http://mozillazg.wordpress.com/?p=137145</guid>
		<description><![CDATA[Python 内置模块 mimetypes 可以实现简单的基于 MIME 类型猜测文件类型的功能。 用法示例： &#62;&#62;&#62; import mimetypes &#62;&#62;&#62; mimetypes.guess_type('D:\Temp\hello.py') ('text/x-python', None) &#62;&#62;&#62; mimetypes.guess_type('D:\Temp\6d81800ad4af5450b1351d82.jpg') ('image/jpeg', None) 更详细的功能见：mimetypes — Map filenames to MIME types Filed under: Python Tagged: MIME, mimetypes, Python, 文件类型<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137145&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Python 内置模块 <strong>mimetypes</strong> 可以实现简单的基于 MIME 类型猜测文件类型的功能。</p>
<p>用法示例：</p>
<pre><code>&gt;&gt;&gt; import mimetypes
&gt;&gt;&gt; mimetypes.guess_type('D:\Temp\hello.py')
('text/x-python', None)
&gt;&gt;&gt; mimetypes.guess_type('D:\Temp\6d81800ad4af5450b1351d82.jpg')
('image/jpeg', None)
</code></pre>
<p>更详细的功能见：<a href="http://docs.python.org/library/mimetypes.html">mimetypes — Map filenames to MIME types</a></p>
<br />Filed under: <a href='http://mozillazg.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mozillazg.wordpress.com/tag/mime/'>MIME</a>, <a href='http://mozillazg.wordpress.com/tag/mimetypes/'>mimetypes</a>, <a href='http://mozillazg.wordpress.com/tag/python/'>Python</a>, <a href='http://mozillazg.wordpress.com/tag/%e6%96%87%e4%bb%b6%e7%b1%bb%e5%9e%8b/'>文件类型</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mozillazg.wordpress.com/137145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mozillazg.wordpress.com/137145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mozillazg.wordpress.com/137145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mozillazg.wordpress.com/137145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mozillazg.wordpress.com/137145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mozillazg.wordpress.com/137145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mozillazg.wordpress.com/137145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mozillazg.wordpress.com/137145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mozillazg.wordpress.com/137145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mozillazg.wordpress.com/137145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mozillazg.wordpress.com/137145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mozillazg.wordpress.com/137145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mozillazg.wordpress.com/137145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mozillazg.wordpress.com/137145/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137145&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mozillazg.wordpress.com/2011/11/05/python-view-or-get-the-type-of-a-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95032e8eb950c9c93a56b381ee273e15?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mozillazg</media:title>
		</media:content>
	</item>
		<item>
		<title>[python][web.py]上传图片并生成略缩图</title>
		<link>http://mozillazg.wordpress.com/2011/10/27/webpy-upload-image-and-create-thumbnail/</link>
		<comments>http://mozillazg.wordpress.com/2011/10/27/webpy-upload-image-and-create-thumbnail/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 12:58:12 +0000</pubDate>
		<dc:creator>mozillazg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[web.py]]></category>
		<category><![CDATA[略缩图]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[上传]]></category>

		<guid isPermaLink="false">http://mozillazg.wordpress.com/?p=137142</guid>
		<description><![CDATA[[web.py]上传图片并生成略缩图 生成略缩图时，用到了第三方库 PIL(Python Imaging Library) 。 upload.py import web import Image urls = ('/upload', 'Upload') render = web.template.render('templates/',) class Upload: def GET(self): web.header("Content-Type","text/html; charset=utf-8") return render.upload('') def POST(self): x = web.input(myfile={}) filedir = './static' # change this to the directory you want to store the file in. if 'myfile' in x: # to check if [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137142&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>[web.py]上传图片并生成略缩图</h1>
<p>生成略缩图时，用到了第三方库 <a href="http://www.pythonware.com/products/pil/">PIL(Python Imaging Library)</a> 。</p>
<h2>upload.py</h2>
<pre><code>import web
import Image

urls = ('/upload', 'Upload')
render = web.template.render('templates/',)
class Upload:

    def GET(self):
        web.header("Content-Type","text/html; charset=utf-8")
        return render.upload('')

    def POST(self):
        x = web.input(myfile={})
        filedir = './static' # change this to the directory you want to store the file in.
        if 'myfile' in x: # to check if the file-object is created
            filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
            filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
            fout = open(filedir +'/'+ filename,'wb') # creates the file where the uploaded file should be stored
            fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
            fout.close() # closes the file, upload complete.

            infile = filedir +'/'+filename
            outfile = infile + ".thumbnail"
            im = Image.open(filedir +'/'+filename)
            im.thumbnail((128, 128))
            im.save(outfile, im.format)

        return render.upload(outfile)

if __name__ == "__main__":
   app = web.application(urls, globals())
   app.run()
</code></pre>
<h2>upload.html</h2>
<pre><code>$def with(src)

&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;
&lt;form method="POST" enctype="multipart/form-data" action=""&gt;
&lt;input type="file" name="myfile" /&gt;
&lt;br/&gt;
&lt;input type="submit" /&gt;
&lt;/form&gt;
$if src:
    &lt;img src="$src" /&gt;
&lt;/body&gt;&lt;/html&gt;
</code></pre>
<h2>参考</h2>
<ul>
<li><a href="http://webpy.org/cookbook/fileupload">Web.py Cookbook(File Upload)</a></li>
<li><a href="http://webpy.org/cookbook/storeupload">Web.py Cookbook(Store an uploaded file)</a></li>
<li>pil-handbook(Reading and Writing Images)</li>
</ul>
<br />Filed under: <a href='http://mozillazg.wordpress.com/category/python/'>Python</a>, <a href='http://mozillazg.wordpress.com/category/python/web-py/'>web.py</a> Tagged: <a href='http://mozillazg.wordpress.com/tag/%e7%95%a5%e7%bc%a9%e5%9b%be/'>略缩图</a>, <a href='http://mozillazg.wordpress.com/tag/pil/'>PIL</a>, <a href='http://mozillazg.wordpress.com/tag/python/'>Python</a>, <a href='http://mozillazg.wordpress.com/tag/web-py/'>web.py</a>, <a href='http://mozillazg.wordpress.com/tag/%e4%b8%8a%e4%bc%a0/'>上传</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mozillazg.wordpress.com/137142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mozillazg.wordpress.com/137142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mozillazg.wordpress.com/137142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mozillazg.wordpress.com/137142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mozillazg.wordpress.com/137142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mozillazg.wordpress.com/137142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mozillazg.wordpress.com/137142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mozillazg.wordpress.com/137142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mozillazg.wordpress.com/137142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mozillazg.wordpress.com/137142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mozillazg.wordpress.com/137142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mozillazg.wordpress.com/137142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mozillazg.wordpress.com/137142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mozillazg.wordpress.com/137142/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mozillazg.wordpress.com&amp;blog=14111795&amp;post=137142&amp;subd=mozillazg&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mozillazg.wordpress.com/2011/10/27/webpy-upload-image-and-create-thumbnail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95032e8eb950c9c93a56b381ee273e15?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mozillazg</media:title>
		</media:content>
	</item>
	</channel>
</rss>
