<?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/"
	>

<channel>
	<title>Mithro rants about stuff &#187; Python</title>
	<atom:link href="http://blog.mithis.net/archives/category/python/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.mithis.net</link>
	<description></description>
	<lastBuildDate>Fri, 16 Dec 2011 06:02:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Reading cookies from most Firefox versions&#8230;</title>
		<link>http://blog.mithis.net/archives/python/94-reading-cookies-firefox</link>
		<comments>http://blog.mithis.net/archives/python/94-reading-cookies-firefox#comments</comments>
		<pubDate>Mon, 19 Jan 2009 23:59:49 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=94</guid>
		<description><![CDATA[Yesterday, I wrote about how to reading the cookies from Firefox 3.0 from Python. This code snippet extends the previous example by adding code which finds the cookie file on various different operating systems (Windows, Linux and Mac OS X). Hope this helps people who need to do this. #! /usr/bin/env python # Reading the [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote about how to reading the <a href="http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python">cookies from Firefox 3.0</a> from Python. This code snippet extends the previous example by adding code which finds the cookie file on various different operating systems (Windows, Linux and Mac OS X). Hope this helps people who need to do this.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Reading the cookie's from Firefox/Mozilla. Supports Firefox 3.0 and Firefox 2.x</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Author: Noah Fontes &lt;nfontes AT cynigram DOT com&gt;, </span>
<span style="color: #808080; font-style: italic;">#         Tim Ansell &lt;mithro AT mithis DOT com&gt;</span>
<span style="color: #808080; font-style: italic;"># License: MIT</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> sqlite2cookie<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">cStringIO</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">StringIO</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> pysqlite2 <span style="color: #ff7700;font-weight:bold;">import</span> dbapi2 <span style="color: #ff7700;font-weight:bold;">as</span> sqlite
&nbsp;
    con = sqlite.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
&nbsp;
    cur = con.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cur.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;select host, path, isSecure, expiry, name, value from moz_cookies&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    ftstr = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FALSE&quot;</span>,<span style="color: #483d8b;">&quot;TRUE&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
    s = <span style="color: #dc143c;">StringIO</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot;<span style="color: #000099; font-weight: bold;">\</span>
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
&quot;&quot;&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> cur.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        s.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>
            item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, ftstr<span style="color: black;">&#91;</span>item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>,
            ftstr<span style="color: black;">&#91;</span>item<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    s.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
    cookie_jar = <span style="color: #dc143c;">cookielib</span>.<span style="color: black;">MozillaCookieJar</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cookie_jar._really_load<span style="color: black;">&#40;</span>s, <span style="color: #483d8b;">''</span>, <span style="color: #008000;">True</span>, <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> cookie_jar
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">cookielib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">ConfigParser</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Set up cookie jar paths</span>
<span style="color: #ff7700;font-weight:bold;">def</span> _get_firefox_cookie_jar <span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
    profiles_ini = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>path, <span style="color: #483d8b;">'profiles.ini'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>profiles_ini<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Open profiles.ini and read the path for the first profile</span>
    profiles_ini_reader = <span style="color: #dc143c;">ConfigParser</span>.<span style="color: #dc143c;">ConfigParser</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
    profiles_ini_reader.<span style="color: black;">readfp</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span>profiles_ini<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    profile_name = profiles_ini_reader.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Profile0'</span>, <span style="color: #483d8b;">'Path'</span>, <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
    profile_path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>path, profile_name<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>profile_path<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>profile_path, <span style="color: #483d8b;">'cookies.sqlite'</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>profile_path, <span style="color: #483d8b;">'cookies.sqlite'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>profile_path, <span style="color: #483d8b;">'cookies.txt'</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>profile_path, <span style="color: #483d8b;">'cookies.txt'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _get_firefox_nt_cookie_jar <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473846</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">_winreg</span>
        <span style="color: #ff7700;font-weight:bold;">import</span> win32api
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Cannot load winreg -- running windows and win32api loaded?'</span><span style="color: black;">&#41;</span>
    key = <span style="color: #dc143c;">_winreg</span>.<span style="color: black;">OpenKey</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">_winreg</span>.<span style="color: black;">HKEY_CURRENT_USER</span>, r<span style="color: #483d8b;">'Software<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\W</span>indows<span style="color: #000099; font-weight: bold;">\C</span>urrentVersion<span style="color: #000099; font-weight: bold;">\E</span>xplorer<span style="color: #000099; font-weight: bold;">\S</span>hell Folders'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        result = <span style="color: #dc143c;">_winreg</span>.<span style="color: black;">QueryValueEx</span><span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">'AppData'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">WindowsError</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        key.<span style="color: black;">Close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> ret<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> == <span style="color: #dc143c;">_winreg</span>.<span style="color: black;">REG_EXPAND_SZ</span>:
            result = win32api.<span style="color: black;">ExpandEnvironmentStrings</span><span style="color: black;">&#40;</span>ret<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            result = ret<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> _get_firefox_cookie_jar<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>result, r<span style="color: #483d8b;">'Mozilla<span style="color: #000099; font-weight: bold;">\F</span>irefox<span style="color: #000099; font-weight: bold;">\P</span>rofiles'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _get_firefox_posix_cookie_jar <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> _get_firefox_cookie_jar<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'~/.mozilla/firefox'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _get_firefox_mac_cookie_jar <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># First of all...</span>
    result = _get_firefox_cookie_jar<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'~/Library/Mozilla/Firefox/Profiles'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> result == <span style="color: #008000;">None</span>:
        result = _get_firefox_cookie_jar<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'~/Library/Application Support/Firefox/Profiles'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> result
&nbsp;
FIREFOX_COOKIE_JARS = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'nt'</span>: _get_firefox_nt_cookie_jar,
    <span style="color: #483d8b;">'posix'</span>: _get_firefox_posix_cookie_jar,
    <span style="color: #483d8b;">'mac'</span>: _get_firefox_mac_cookie_jar
<span style="color: black;">&#125;</span>
&nbsp;
cookie_jar = <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
    cookie_jar = FIREFOX_COOKIE_JARS<span style="color: black;">&#91;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">name</span><span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyError</span>:
    cookie_jar = <span style="color: #008000;">None</span>
&nbsp;
path = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Path to cookie jar file [%s]: '</span> <span style="color: #66cc66;">%</span> cookie_jar<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Some input specified, set it</span>
    cookie_jar = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">realpath</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span>path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> cookie_jar.<span style="color: black;">endswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.sqlite'</span><span style="color: black;">&#41;</span>:
    cookie_jar = sqlite2cookie<span style="color: black;">&#40;</span>cookie_jar<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
    cookie_jar = <span style="color: #dc143c;">cookielib</span>.<span style="color: black;">MozillaCookieJar</span><span style="color: black;">&#40;</span>cookie_jar<span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p><i>Edit: The latest version of this code can be found at <a href="http://blog.mithis.com/cgi-bin/gitweb.cgi">http://blog.mithis.com/cgi-bin/gitweb.cgi</a> and includes numerous fixes and updates.</i></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/python/94-reading-cookies-firefox/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>$#%#! UTF-8 in Python</title>
		<link>http://blog.mithis.net/archives/python/91-utf-8-in-python</link>
		<comments>http://blog.mithis.net/archives/python/91-utf-8-in-python#comments</comments>
		<pubDate>Mon, 19 Jan 2009 03:54:08 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[$LANG]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[sys.stdout]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=91</guid>
		<description><![CDATA[This is not a post about using UTF-8 properly in Python, but doing evil, evil things. Python dutifully respects the $LANG environment variable on the terminal. It turns out that a lot of the time this variable is totally wrong, it&#8217;s set to something like C even though the terminal is UTF-8 encoding. The problem [...]]]></description>
			<content:encoded><![CDATA[<p>This is <b>not</b> a post about using UTF-8 properly in Python, but doing <i>evil, evil</i> things.</p>
<p>Python dutifully respects the $LANG environment variable on the terminal. It turns out that a lot of the time this variable is totally wrong, it&#8217;s set to something like C even though the terminal is UTF-8 encoding. </p>
<p>The problem is that there is no easy way to change a file&#8217;s encoding after it&#8217;s open, well until this horrible hack! The following code will force the output encoding of stdout to UTF-8 even if started with LANG=C.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># License: MIT</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> u<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\u</span>263A&quot;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
    <span style="color: #ff7700;font-weight:bold;">print</span> e
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">encoding</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> ctypes <span style="color: #ff7700;font-weight:bold;">import</span> pythonapi, py_object, c_char_p
PyFile_SetEncoding = pythonapi.<span style="color: black;">PyFile_SetEncoding</span>
PyFile_SetEncoding.<span style="color: black;">argtypes</span> = <span style="color: black;">&#40;</span>py_object, c_char_p<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> PyFile_SetEncoding<span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>, <span style="color: #483d8b;">&quot;UTF-8&quot;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> u<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\u</span>263A&quot;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
    <span style="color: #ff7700;font-weight:bold;">print</span> e</pre></div></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/python/91-utf-8-in-python/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading Firefox 3.x cookies in Python</title>
		<link>http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python</link>
		<comments>http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python#comments</comments>
		<pubDate>Mon, 19 Jan 2009 03:28:28 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[cookie jar]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[sqlite]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=90</guid>
		<description><![CDATA[I found the following code snippet on my hard drive today. It allows you to access Firefox 3.x cookies in Python. Firefox 3.x moved away from the older text file format to a sqlite database. This code is useful if you want to access something behind an authentication gateway and you also access the page [...]]]></description>
			<content:encoded><![CDATA[<p>I found the following code snippet on my hard drive today. It allows you to access <a href="http://www.getfirefox.com" target="_self">Firefox 3.x</a> cookies in Python. Firefox 3.x moved away from the older text file format to a <a href="http://www.sqlite.org/">sqlite</a> database.</p>
<p>This code is useful if you want to access something behind an authentication gateway and you also access the page through your web browser. You can also use this code to convert a sqlite database into a cookie file <a href="http://curl.haxx.se/">CURL</a> can read.</p>
<p>I didn&#8217;t write this code, it was written by Noah Fontes when we where doing some scraping of the <a href="http://code.google.com/soc">Google Summer of Code</a> website (before I joined Google).</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Protocol implementation for handling gsocmentors.com transactions</span>
<span style="color: #808080; font-style: italic;"># Author: Noah Fontes nfontes AT cynigram DOT com</span>
<span style="color: #808080; font-style: italic;"># License: MIT</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> sqlite2cookie<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">cStringIO</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">StringIO</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> pysqlite2 <span style="color: #ff7700;font-weight:bold;">import</span> dbapi2 <span style="color: #ff7700;font-weight:bold;">as</span> sqlite
&nbsp;
    con = sqlite.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
&nbsp;
    cur = con.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cur.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;select host, path, isSecure, expiry, name, value from moz_cookies&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    ftstr = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FALSE&quot;</span>,<span style="color: #483d8b;">&quot;TRUE&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
    s = <span style="color: #dc143c;">StringIO</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot;<span style="color: #000099; font-weight: bold;">\</span>
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
&quot;&quot;&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> cur.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        s.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>
            item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, ftstr<span style="color: black;">&#91;</span>item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>,
            ftstr<span style="color: black;">&#91;</span>item<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    s.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
    cookie_jar = <span style="color: #dc143c;">cookielib</span>.<span style="color: black;">MozillaCookieJar</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cookie_jar._really_load<span style="color: black;">&#40;</span>s, <span style="color: #483d8b;">''</span>, <span style="color: #008000;">True</span>, <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> cookie_jar</pre></div></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cool Python, Swaping two variables</title>
		<link>http://blog.mithis.net/archives/ideas/64-python-swap-var</link>
		<comments>http://blog.mithis.net/archives/ideas/64-python-swap-var#comments</comments>
		<pubDate>Sat, 10 Nov 2007 15:07:39 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Ideas]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/archives/ideas/64-python-swap-var</guid>
		<description><![CDATA[Some people say &#8220;you learn something new everyday&#8221; or something like that. Today someone on #python showed me a cool trick I never would have thought of on my own. Often there is a time when you want to swap the contents of two variables. The most popular way to do this is using a [...]]]></description>
			<content:encoded><![CDATA[<p>Some people say &#8220;you learn something new everyday&#8221; or something like that. Today someone on <a href="irc://irc.freenode.org/#python">#python</a> showed me a cool trick I never would have thought of on my own.</p>
<p>Often there is a time when you want to swap the contents of two variables. The most popular way to do this is using a third variable as shown below:</p>
<blockquote><p>
temp = a<br />
a = b<br />
b = temp
</p></blockquote>
<p>This looks sucky and doesn&#8217;t really express very well what you want to do. A much better way to do this in Python is with the following magic line:</p>
<blockquote><p>
a, b = b, a
</p></blockquote>
<p>Doesn&#8217;t that look so much better? And it is very clear to anyone who has used Python before what is going on. To think, I have been using Python for about 7 years now and never thought of doing that.</p>
<p>Just thought I would share this tidbit.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/ideas/64-python-swap-var/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Skimpy, Scheme in Python</title>
		<link>http://blog.mithis.net/archives/tp/45-schemepy</link>
		<comments>http://blog.mithis.net/archives/tp/45-schemepy#comments</comments>
		<pubDate>Sun, 22 Jul 2007 17:36:02 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[Thousand Parsec]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/archives/tp/45-schemepy</guid>
		<description><![CDATA[For something a bit different, I decided to work on making embedding Scheme in Python easier. I&#8217;ve previously been using the cool PyScheme, however it hasn&#8217;t been updated in quite a long time (since 2004) and is quite slow. The reason I would want to do something crazy like this is that Thousand Parsec use [...]]]></description>
			<content:encoded><![CDATA[<p>For something a bit different, I decided to work on making embedding <a href="http://www.schemers.org/">Scheme</a> in <a href="http://www.python.org/" title="Python">Python</a> easier. I&#8217;ve previously been using the cool <a href="http://http://hkn.eecs.berkeley.edu/~dyoo/python/pyscheme/" title="PyScheme">PyScheme</a>, however it hasn&#8217;t been updated in quite a long time (since 2004) and is quite slow.</p>
<p>The reason I would want to do something crazy like this is that <a href="http://www.thousandparsec.net/">Thousand Parsec</a> use a subset of Scheme called <a href="http://www.thousandparsec.net/tp/dev/documents/ncl.php" title="TPCL">TPCL</a>. The is used to transmit information from the server to clients about rules for creating designs. Servers also need to be able to parse <a href="http://www.thousandparsec.net/tp/dev/documents/ncl.php" title="TPCL">TPCL</a> for &#8220;dumb clients&#8221; which can&#8217;t parse <a href="http://www.thousandparsec.net/tp/dev/documents/ncl.php" title="TPCL">TPCL</a> for themselves.</p>
<p>Recent developments by <a href="http://dystopicfro.blogspot.com/index.html" title="Fro&#039;s Blog">DystopicFro</a> on his <a href="http://code.google.com/soc" title="Summer of Code">Summer of Code</a> project, a <a href="http://git.thousandparsec.net/gitweb/gitweb.cgi?p=tpruledev.git;a=summary">Ruleset</a><a href="http://git.thousandparsec.net/gitweb/gitweb.cgi?p=tpruledev.git;a=summary"> Development Environment</a> have meant that he also needs the ability to parser <a href="http://www.thousandparsec.net/tp/dev/documents/ncl.php" title="TPCL">TPCL</a> (and specifically the ability to detect errors). This got us chatting about PyScheme and it&#8217;s inadequacies.</p>
<p>What I have decided to do is create a module called <a href="http://git.thousandparsec.net/gitweb/gitweb.cgi?p=schemepy.git;a=summary">SchemePy</a> (pronounced Skimpy). On platforms where speed is of no concern, we will fall back to using a modified version of PyScheme. However, we can also use C scheme systems such as <a href="http://www.gnu.org/software/guile/guile.html" title="Guile">Guile</a> (or other libraries) to improve speed.</p>
<p>Why have multiple implementations? It stops us from using custom things in one scheme implementation which are not compatible with other implementations. It also makes installation easier for the user, as they are much more likely to already have a compatible scheme library installed. Different scheme&#8217;s also have different speed advantages.</p>
<p>So far I have got the <a href="http://www.gnu.org/software/guile/guile.html" title="Guile">Guile</a> wrapper 95% working. It&#8217;s written mainly in <a href="http://www.python.org/" title="Python">Python</a> using <a href="http://python.net/crew/theller/ctypes/" title="Ctypes">ctypes</a>. I needed a small C helper module as well because of the extensive Macro&#8217;s used by Guile. So far, you can convert between Guile and Python types easily, you can register Python functions into the Guile context and exceptions are caught. There is also the ability to pass  python objects thru the Scheme environment to Python functions. I would like to thank the guys who hang out on <a href="irc://irc.freenode.org/#guile">#guile</a> for all their help, it has made doing this wrapper much easier.</p>
<p>I&#8217;m happy enough with the outcome. My guess it will be between 10 and 20 times faster then PyScheme, but I&#8217;ve yet to do any benchmarking. I&#8217;m going to move to wrapping <a href="http://www.plt-scheme.org/software/mzscheme/">mzscheme</a> too soon enough. It should be much easier to do now that I have gotten most of the hard stuff sorted out. I think a lot of it will be common between the implementations.</p>
<p>What I really need to do is get a test-suit working. Once I have more then one implementation working  it will be very important to make sure that they all work the same way, the only way I can see to do that properly is to have a test suite which I can run every implementation against.</p>
<p>One thing which might be really cool to investigate is using a similar system to <a href="http://web.archive.org/web/20040607171956/http://www.caddr.com/code/lython/">lython</a> which compiles Lisp <a href="http://en.wikipedia.org/wiki/Sexp">s-expressions</a> directly to python byte code. If this was done well it should be the fastest method as it would mean no type conversion needs to be done.</p>
<p>Overall this has been quite a good learning experience. I have improve my ctype skills quite a lot (this wasn&#8217;t my first ctypes wrapper, that being <a href="http://git.thousandparsec.net/repos/libmng-py/doc/" title="libmng-py">libmng-py</a>, a Python wrapper around <a href="http://www.libmng.com/">libmng</a>). I also understand how <a href="http://www.schemers.org/">Scheme</a> works quite a lot better now.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/tp/45-schemepy/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

