<?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>Fedora &#187; Python</title>
	<atom:link href="http://gofedora.com/archives/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://gofedora.com</link>
	<description>How Tos, Tutorials, Tips and Tricks</description>
	<lastBuildDate>Wed, 14 Sep 2011 15:10:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How To: Install and Use Twython (Python Wrapper for Twitter API)</title>
		<link>http://gofedora.com/how-to-install-use-twython-python-wrapper-for-twitter-api/</link>
		<comments>http://gofedora.com/how-to-install-use-twython-python-wrapper-for-twitter-api/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 07:49:51 +0000</pubDate>
		<dc:creator>Kulbir Saini</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Installation]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tips - Tricks]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Code Demo]]></category>
		<category><![CDATA[Proxy Support]]></category>
		<category><![CDATA[Twitter API]]></category>
		<category><![CDATA[Twython]]></category>

		<guid isPermaLink="false">http://gofedora.com/?p=826</guid>
		<description><![CDATA[
As promised in my previous post, here is a brief howto on getting started with twython. The main advantage of Twython over several other python (or any other language) wrappers for Twitter API is that it works even when you are behind your organizations proxy.
Download Twython
You can download latest version of twython from twython page [...]<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></description>
			<content:encoded><![CDATA[<p><a class="post_image_link" href="http://gofedora.com/how-to-install-use-twython-python-wrapper-for-twitter-api/" title="Permanent link to How To: Install and Use Twython (Python Wrapper for Twitter API)"><img class="post_image alignnone" src="http://gofedora.com/wp-content/uploads/2009/09/twython.jpg" width="320" height="220" alt="Twython (Twitter API in Python)" title="How To: Install and Use Twython (Python Wrapper for Twitter API)" /></a>
</p><p>As promised in my <a href="http://gofedora.com/love-twitter-love-python-love-twython/" target="_blank">previous post</a>, here is a brief howto on getting started with twython. The main advantage of Twython over several other python (or any other language) wrappers for Twitter API is that it works even when you are behind your organizations proxy.</p>
<h3><span style="text-decoration: underline;"><strong>Download Twython</strong></span></h3>
<p>You can download latest version of twython from <a href="http://github.com/ryanmcgrath/twython/tree/master" target="_blank">twython page on github</a>. You can either clone using git (if you have git installed) or can click the download button.</p>
<h3><span style="text-decoration: underline;"><strong>Install Twython</strong></span></h3>
<p>Once you are done with extracting the downloaded tar file. Change directory to twython and run these command as root.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>fedora ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git clone git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>ryanmcgrath<span style="color: #000000; font-weight: bold;">/</span>twython.git
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>fedora ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> twython<span style="color: #000000; font-weight: bold;">/</span>dist
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>fedora dist<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> twython-0.8.tar.gz
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>fedora dist<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> twython-<span style="color: #000000;">0.8</span><span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>fedora dist<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ python setup.py build
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>fedora dist<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></td></tr></table></div>

<h3><span style="text-decoration: underline;"><strong>Use Twython from Python Interpreter</strong></span></h3>
<p>Below is a direct copy paste lines from my interpreter. See how things are working (learning by doing).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>saini@bordeaux ~<span style="color: black;">&#93;</span>$ python
Python <span style="color: #ff4500;">2.6</span> <span style="color: black;">&#40;</span>r26:<span style="color: #ff4500;">66714</span>, Mar <span style="color: #ff4500;">17</span> <span style="color: #ff4500;">2009</span>, <span style="color: #ff4500;">11</span>:<span style="color: #ff4500;">44</span>:<span style="color: #ff4500;">21</span><span style="color: black;">&#41;</span> 
<span style="color: black;">&#91;</span>GCC 4.4.0 <span style="color: #ff4500;">20090313</span> <span style="color: black;">&#40;</span>Red Hat 4.4.0-<span style="color: #ff4500;">0.26</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> on linux2
Type <span style="color: #483d8b;">&quot;help&quot;</span>, <span style="color: #483d8b;">&quot;copyright&quot;</span>, <span style="color: #483d8b;">&quot;credits&quot;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">&quot;license&quot;</span> <span style="color: #ff7700;font-weight:bold;">for</span> more information.
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># first of all, import twython module</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">import</span> twython
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Authenticate your twitter account with your twitter username</span>
... <span style="color: #808080; font-style: italic;"># and password with twitter.setup method.</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> client = twython.<span style="color: black;">setup</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Basic'</span>,<span style="color: #483d8b;">'myusername'</span>,<span style="color: #483d8b;">'mypassword'</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> client.<span style="color: black;">authenticated</span>
<span style="color: #008000;">True</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;">#Lets update our current status on twitter with some cool message.</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> client.<span style="color: black;">updateStatus</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Testing #twython. The coolest #TwitterAPI :)'</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Now go and check your current status on twitter. Surprised!!!</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Get your or anyone's followers</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> client.<span style="color: black;">getFollowersIDs</span><span style="color: black;">&#40;</span>screen_name=<span style="color: #483d8b;">'gofedora'</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Output truncated.</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #808080; font-style: italic;"># Get help for any function.</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">print</span> client.<span style="color: black;">createFriendship</span>.__doc__
createFriendship<span style="color: black;">&#40;</span><span style="color: #008000;">id</span> = <span style="color: #008000;">None</span>, user_id = <span style="color: #008000;">None</span>, screen_name = <span style="color: #008000;">None</span>, follow = <span style="color: #483d8b;">&quot;false&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
	Allows the authenticating users to follow the <span style="color: #dc143c;">user</span> specified <span style="color: #ff7700;font-weight:bold;">in</span> the ID parameter.
	<span style="color: black;">Returns</span> the befriended <span style="color: #dc143c;">user</span> <span style="color: #ff7700;font-weight:bold;">in</span> the requested format when successful. <span style="color: black;">Returns</span> a
	<span style="color: #dc143c;">string</span> describing the failure condition when unsuccessful. <span style="color: black;">If</span> you are already
	friends <span style="color: #ff7700;font-weight:bold;">with</span> the <span style="color: #dc143c;">user</span> an HTTP <span style="color: #ff4500;">403</span> will be returned.
&nbsp;
	<span style="color: black;">Parameters</span>:
		<span style="color: #66cc66;">**</span> Note: One of the following <span style="color: #ff7700;font-weight:bold;">is</span> required. <span style="color: black;">&#40;</span><span style="color: #008000;">id</span>, user_id, screen_name<span style="color: black;">&#41;</span>
		<span style="color: #008000;">id</span> - Required. <span style="color: black;">The</span> ID <span style="color: #ff7700;font-weight:bold;">or</span> screen name of the <span style="color: #dc143c;">user</span> to befriend.
		<span style="color: black;">user_id</span> - Required. <span style="color: black;">Specfies</span> the ID of the <span style="color: #dc143c;">user</span> to befriend. <span style="color: black;">Helpful</span> <span style="color: #ff7700;font-weight:bold;">for</span> disambiguating when a valid <span style="color: #dc143c;">user</span> ID <span style="color: #ff7700;font-weight:bold;">is</span> also a valid screen name. 
		<span style="color: black;">screen_name</span> - Required. <span style="color: black;">Specfies</span> the screen name of the <span style="color: #dc143c;">user</span> to befriend. <span style="color: black;">Helpful</span> <span style="color: #ff7700;font-weight:bold;">for</span> disambiguating when a valid screen name <span style="color: #ff7700;font-weight:bold;">is</span> also a <span style="color: #dc143c;">user</span> ID. 
		<span style="color: black;">follow</span> - Optional. <span style="color: black;">Enable</span> notifications <span style="color: #ff7700;font-weight:bold;">for</span> the target <span style="color: #dc143c;">user</span> <span style="color: #ff7700;font-weight:bold;">in</span> addition to becoming friends. 
<span style="color: #66cc66;">&gt;&gt;&gt;</span></pre></td></tr></table></div>

<p>So now you are ready to do wonders with twython. Write your own code and blog/brag about it <img src='http://gofedora.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="How To: Install and Use Twython (Python Wrapper for Twitter API)" /> </p>
<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gofedora.com/how-to-install-use-twython-python-wrapper-for-twitter-api/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>I Love Twitter, I Love Python = I Love Twython</title>
		<link>http://gofedora.com/love-twitter-love-python-love-twython/</link>
		<comments>http://gofedora.com/love-twitter-love-python-love-twython/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 01:44:16 +0000</pubDate>
		<dc:creator>Kulbir Saini</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Review]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Python-Twitter]]></category>
		<category><![CDATA[Tweepy]]></category>
		<category><![CDATA[Twitter API]]></category>
		<category><![CDATA[Twython]]></category>

		<guid isPermaLink="false">http://gofedora.com/?p=817</guid>
		<description><![CDATA[
I was really late to join Twitter. As soon as I joined Twitter, I started exploring various aspects of Twitter and hence the Twitter API. While searching for a wrapper for Twitter API in python, I came across python-twitter which was good and tweepy which was ok and I finally found twython. Twython is awesome [...]<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></description>
			<content:encoded><![CDATA[<p><a class="post_image_link" href="http://gofedora.com/love-twitter-love-python-love-twython/" title="Permanent link to I Love Twitter, I Love Python = I Love Twython"><img class="post_image alignnone" src="http://gofedora.com/wp-content/uploads/2009/09/twython.jpg" width="320" height="220" alt="Twython (Twitter Python)" title="I Love Twitter, I Love Python = I Love Twython" /></a>
</p><p>I was really late to join Twitter. As soon as I joined Twitter, I started exploring various aspects of Twitter and hence the Twitter API. While searching for a wrapper for Twitter API in python, I came across <a href="http://code.google.com/p/python-twitter/" target="_blank">python-twitter</a> which was good and <a href="http://gitorious.org/tweepy" target="_blank">tweepy</a> which was ok and I finally found twython. <a href="http://github.com/ryanmcgrath/twython/tree" target="_blank">Twython</a> is awesome and more importantly upto date with Twitter API.</p>
<p>While I was using twython, I found one fact very irritating. It didn&#8217;t have docstrings. And I had to go to Twitter API Wiki for description of every single function <img src='http://gofedora.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' title="I Love Twitter, I Love Python = I Love Twython" />  I talked to <a href="http://ryanmcgrath.org/" target="_blank">Ryan</a> (Twython developer, who works all day long at his day job with <a href="http://webs.com" target="_blank">webs.com</a> and develops twython when everybody sleeps <img src='http://gofedora.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' title="I Love Twitter, I Love Python = I Love Twython" />  ) about the same and offered to write docstrings for twython.</p>
<p>I spent a few hours and <a href="http://github.com/ryanmcgrath/twython/commit/88d89f56520a353edd07542b073c5521bd4fd810" target="_blank">sent a patch with docstrings</a> for all the functions. Now twython has complete documentation. Also Ryan is trying his best to implement wrappers for the newly introduced functions in Twitter API. So, currently Twython is THE best wrapper available in python for Twitter API.</p>
<p>PS : Next post will be &#8220;How to use twython&#8221;.</p>
<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gofedora.com/love-twitter-love-python-love-twython/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To: Boot Fedora Faster</title>
		<link>http://gofedora.com/how-to-boot-fedora-faster/</link>
		<comments>http://gofedora.com/how-to-boot-fedora-faster/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 17:57:38 +0000</pubDate>
		<dc:creator>Kulbir Saini</dc:creator>
				<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Fedora]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[Tips - Tricks]]></category>
		<category><![CDATA[Boot Faster]]></category>
		<category><![CDATA[Daemon Forking]]></category>
		<category><![CDATA[Fork]]></category>
		<category><![CDATA[Startup Time]]></category>
		<category><![CDATA[System Services]]></category>

		<guid isPermaLink="false">http://172.17.8.64/gofedora/?p=297</guid>
		<description><![CDATA[
Note: These tricks apply to any Linux based OS. But I have tested them only on Fedora, so can&#8217;t say whether they&#8217;ll work on other Linux(s).
My current Fedora installation is now almost one and a half years old. Yes. I am still using Fedora 7   I have Fedora 10 on my other machine. [...]<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></description>
			<content:encoded><![CDATA[<p><a class="post_image_link" href="http://gofedora.com/how-to-boot-fedora-faster/" title="Permanent link to How To: Boot Fedora Faster"><img class="post_image alignnone" src="http://gofedora.com/wp-content/uploads/2008/12/Fedora-Boot-Process.jpg" width="492" height="279" alt="Fedora Boot Process" title="How To: Boot Fedora Faster" /></a>
</p><p><strong>Note:</strong> These tricks apply to any Linux based OS. But I have tested them only on Fedora, so can&#8217;t say whether they&#8217;ll work on other Linux(s).</p>
<p>My current Fedora installation is now almost one and a half years old. Yes. I am still using Fedora 7 <img src='http://gofedora.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' title="How To: Boot Fedora Faster" />  I have Fedora 10 on my other machine. Coming to the agenda, my Fedora installation has grown beyond control and I have services from named, squid, drbl, privoxy, vsftpd, vbox*, smb and what not on a personal desktop. These services really force my system startup to slow down to more than two minutes. While shutting down, its very easy to just cut the power supply but while booting up I can&#8217;t help and it frustrates me. And what frustrates me further that I have 4GB DDR2 RAM and AMD64 X2 5600+ (2.8GHz x 2) and booting time is still more than two minutes.</p>
<h4><span style="text-decoration: underline;"><strong>Agenda</strong></span></h4>
<ul>
<li>Boot Fedora faster using whatever techniques possible.</li>
</ul>
<h4><a name="hack-1"></a></h4>
<p>Remove the services from normal order and delay their execution to a later stage. So, services like network, squid, privoxy, named, vsftpd, smb etc. doesn&#8217;t make sense unless I am not logged in and using them. Let us start them after we have login screen.</p>
<p><a name="step-1-1"></a></p>
<p>Turn off all the services by using the command</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>bordeaux ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># chkconfig service_name off</span></pre></div></div>

<p>where service_name is the service you want to turn off.</p>
<p><a name="step-1-2"></a></p>
<p>Now create a file <em>/etc/startup.sh</em>. Enter a line like this</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>bordeaux ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># service service_name start</span></pre></div></div>

<p>for every service that you have turned off in the Step 1.1 and you want it to be running after your machine starts up. Now, your startup.sh file should look like this</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">service network start <span style="color: #000000; font-weight: bold;">&amp;</span>
service sshd start <span style="color: #000000; font-weight: bold;">&amp;</span>
modprobe it87 <span style="color: #000000; font-weight: bold;">&amp;</span>
modprobe k8temp <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>iptraf <span style="color: #660033;">-s</span> eth0 <span style="color: #660033;">-B</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>iptraf <span style="color: #660033;">-s</span> lo <span style="color: #660033;">-B</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
service squid start <span style="color: #000000; font-weight: bold;">&amp;</span>
service privoxy start <span style="color: #000000; font-weight: bold;">&amp;</span>
service httpd start <span style="color: #000000; font-weight: bold;">&amp;</span>
service mysqld start <span style="color: #000000; font-weight: bold;">&amp;</span>
service named start <span style="color: #000000; font-weight: bold;">&amp;</span>
service smb start <span style="color: #000000; font-weight: bold;">&amp;</span>
service vboxdrv start <span style="color: #000000; font-weight: bold;">&amp;</span>
service vboxnet start <span style="color: #000000; font-weight: bold;">&amp;</span>
service vsftpd start <span style="color: #000000; font-weight: bold;">&amp;</span></pre></div></div>

<p><a name="step-1-3"></a></p>
<p>Add the following line to <em>/etc/rc.local</em> file</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>startup.sh <span style="color: #000000; font-weight: bold;">&amp;</span></pre></div></div>

<p>Done!!! Notice the &amp;s in both files. They are for execution in background so that a process can block boot process. You&#8217;ll observe a drop of 10-20 seconds in system startup time.</p>
<h4><a name="hack-2"></a></h4>
<p><strong>Problem with <a href="#hack-1">Hack #1</a></strong> : The execution is not really parallel. It executes like a process in the background. So we can&#8217;t get the real advantage of parallel execution.</p>
<p><a href="#hack-2">Hack #2</a> solves this problem. Now we don&#8217;t put processes in background. We use daemon forking to fork a separate daemon process which will start all the services for us in parallel. Here we&#8217;ll get the real advantage and startup time will decrease further.</p>
<p><a name="step-2-1"></a></p>
<p>This step is totally similar to <a href="#step-1-1">Step 1.1</a>. So skipping it.</p>
<p><a name="step-2-2"></a></p>
<p>This step is also similar to <a href="#step-1-2">Step 1.2</a>. The <em>/etc/startup.sh</em> file should look like this.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">service network start
service xinetd start
service crond start
service anacron start
service atd start
service sshd start
service rpcbind start
service rpcgssd start
service rpcimapd start
modprobe it87
modprobe k8temp
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>iptraf <span style="color: #660033;">-s</span> eth0 <span style="color: #660033;">-B</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>iptraf <span style="color: #660033;">-s</span> lo <span style="color: #660033;">-B</span>
service nasd start
service squid start
service privoxy start
service httpd start
service iptables start
service lm_sensors start
service mysqld start
service named start
service nfs start
service nfslock start
service smb start
service vboxdrv start
service vboxnet start
service vsftpd start
service autofs start
service smartd start</pre></div></div>

<p>Notice the absence of &amp;s in the file.</p>
<p><a name="step-2-3"></a></p>
<p>Download the attached <em>startup.py</em> file attached at the end of this post or copy paste the following code to <em>/etc/startup.py</em> file.</p>

<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;"># (C) Copyright 2008 Kulbir Saini</span>
<span style="color: #808080; font-style: italic;"># License : GPL</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;">def</span> fork_daemon<span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;This function forks a daemon.&quot;&quot;&quot;</span>
    <span style="color: #808080; font-style: italic;"># Perform double fork</span>
    r = <span style="color: #483d8b;">''</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">fork</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># Parent</span>
        <span style="color: #808080; font-style: italic;"># Wait for the child so that it doesn't defunct</span>
        <span style="color: #dc143c;">os</span>.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># Return a function</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>  <span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #66cc66;">*</span>x, <span style="color: #66cc66;">**</span>kw: r
    <span style="color: #808080; font-style: italic;"># Otherwise, we are the child</span>
    <span style="color: #808080; font-style: italic;"># Perform second fork</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">setsid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">umask</span><span style="color: black;">&#40;</span>077<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">chdir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">fork</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">os</span>._exit<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> wrapper<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;Wrapper function to be returned from generator.
        Executes the function bound to the generator and then
        exits the process&quot;&quot;&quot;</span>
        f<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">os</span>._exit<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> wrapper
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> start_services<span style="color: black;">&#40;</span>startup_file<span style="color: black;">&#41;</span>:
    command = <span style="color: #483d8b;">'/bin/bash '</span> + startup_file + <span style="color: #483d8b;">' &gt; /dev/null 2&gt; /dev/null '</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span>command<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    forkd = fork_daemon<span style="color: black;">&#40;</span>start_services<span style="color: black;">&#41;</span>
    forkd<span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Executing '</span>, <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'[  OK  ]'</span></pre></div></div>

<p><a name="step-2-4"></a></p>
<p>Add the following line to your /etc/rc.local file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>startup.py <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>startup.sh</pre></div></div>

<p>Thats it. Done!!! Now you&#8217;ll experience a boost of about 25-30 seconds of decrease in boot time.</p>
<p><span style="text-decoration: underline;"><strong>Stats of my machine</strong></span></p>
<p>With all services started in normal order : 2minutes.<br />
With <a href="#hack-1">Hack #1</a> : 1minute 42 seconds.<br />
With <a href="#hack-2">Hack #2</a> : 1minute.</p>
<p><strong>Warning</strong> : These hacks may break your system and can make it unusable. Use at your own risk.</p>
<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gofedora.com/how-to-boot-fedora-faster/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>How To: Write Custom Redirector or Rewritor Plugin For Squid in Python</title>
		<link>http://gofedora.com/how-to-write-custom-redirector-rewritor-plugin-squid-python/</link>
		<comments>http://gofedora.com/how-to-write-custom-redirector-rewritor-plugin-squid-python/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 19:31:26 +0000</pubDate>
		<dc:creator>Kulbir Saini</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Proxy Server]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Squid]]></category>
		<category><![CDATA[Tips - Tricks]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Redirector]]></category>
		<category><![CDATA[Rewritor]]></category>

		<guid isPermaLink="false">http://172.17.8.64/gofedora/?p=211</guid>
		<description><![CDATA[Mission
To write a custom Python program which can act as a plugin for Squid to redirect a given URL to another URL. This is useful when already existing redirector plugins for Squid doesn&#8217;t suit your needs or you want everything of your own.
Use Cases

When you want to redirect URLs using a database like mysql or [...]<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></description>
			<content:encoded><![CDATA[<p></p><h4><b>Mission</b></h4>
<p>To write a custom Python program which can act as a plugin for Squid to redirect a given URL to another URL. This is useful when already existing redirector plugins for Squid doesn&#8217;t suit your needs or you want everything of your own.</p>
<h4><b>Use Cases</b></h4>
<ol>
<li>When you want to redirect URLs using a database like mysql or postgresql.</li>
<li>When you want to redirect based on mappings stored in simple text files.</li>
<li>When you want to build a redirector which can learn by itself using AI techniques <img src='http://gofedora.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' title="How To: Write Custom Redirector or Rewritor Plugin For Squid in Python" /> </li>
</ol>
<h4><b>How to proceed</b></h4>
<p>From <a href="http://wiki.squid-cache.org/SquidFaq" target="_blank">Squid FAQ</a>,</p>
<blockquote><p><i>The redirector program must read URLs (one per line) on standard input, and write rewritten URLs or blank lines on standard output.  Note that the redirector program can not use buffered I/O.  Squid writes additional information after the URL which a redirector can use to make a decision.<i></i></i></p></blockquote>
<p>The format of the line read from the standard input by the program is as follows.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">URL ip-address<span style="color: #000000; font-weight: bold;">/</span>fqdn ident method
<span style="color: #666666; font-style: italic;"># for example</span>
http:<span style="color: #000000; font-weight: bold;">//</span>saini.co.in 172.17.8.175<span style="color: #000000; font-weight: bold;">/</span>saini.co.in - GET -</pre></td></tr></table></div>

</p>
<p>The implementation sounds very simple and it is indeed very simple to implement. The only thing that should be taken care of is the unbuffered I/O. You should immediately flush the output to standard output once decision is taken.</p>
<p>For this howto, we assume we have a method called &#8216;<i>modify_url()</i>&#8216; which returns either a blank line or a modified URL to which the client should be redirected.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">def</span> modify_url<span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>:
    <span style="color: #008000;">list</span> = line.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span><span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># first element of the list is the URL</span>
    old_url = <span style="color: #008000;">list</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    new_url = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>
    <span style="color: #808080; font-style: italic;"># take the decision and modify the url if needed</span>
    <span style="color: #808080; font-style: italic;"># do remember that the new_url should contain a '\n' at the end.</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> old_url.<span style="color: black;">endswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.avi'</span><span style="color: black;">&#41;</span>:
        new_url = <span style="color: #483d8b;">'http://fedora.co.in/errors/accessDenied.html'</span> + new_url
    <span style="color: #ff7700;font-weight:bold;">return</span> new_url
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    <span style="color: #808080; font-style: italic;"># the format of the line read from stdin is</span>
    <span style="color: #808080; font-style: italic;"># URL ip-address/fqdn ident method</span>
    <span style="color: #808080; font-style: italic;"># for example</span>
    <span style="color: #808080; font-style: italic;"># http://saini.co.in 172.17.8.175/saini.co.in - GET -</span>
    line = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<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;"># new_url is a simple URL only</span>
    <span style="color: #808080; font-style: italic;"># for example</span>
    <span style="color: #808080; font-style: italic;"># http://fedora.co.in</span>
    new_url = modify_url<span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>new_url<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

</p>
<p>Save the above file somewhere. We save this example file in <i>/etc/squid/custom_redirect.py</i>. Now, we have the function for redirecting clients. We need to configure squid to use custom_redirect.py . Below is the squid configuration for telling squid to use the above program as redirector.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Add these lines to /etc/squid/squid.conf file.</span>
<span style="color: #666666; font-style: italic;"># /usr/bin/python should be replaced by the path to python executable if you installed it somewhere else.</span>
redirect_program <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>squid<span style="color: #000000; font-weight: bold;">/</span>custom_redirect.py
<span style="color: #666666; font-style: italic;"># Number of instances of the above program that should run concurrently.</span>
<span style="color: #666666; font-style: italic;"># 5 is good enough but you should go for 10 at least. Anything below 5 would not work properly.</span>
redirect_children <span style="color: #000000;">5</span></pre></td></tr></table></div>

</p>
<p>Now, start/reload/restart squid. That&#8217;s all we need to write and use a custom redirector plugin for squid.</p>
<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gofedora.com/how-to-write-custom-redirector-rewritor-plugin-squid-python/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How To: Write Custom Basic Authentication Plugin for Squid in Python</title>
		<link>http://gofedora.com/how-to-write-custom-basic-authentication-plugin-squid-python/</link>
		<comments>http://gofedora.com/how-to-write-custom-basic-authentication-plugin-squid-python/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 19:37:19 +0000</pubDate>
		<dc:creator>Kulbir Saini</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Proxy Server]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Squid]]></category>
		<category><![CDATA[Tips - Tricks]]></category>
		<category><![CDATA[Basic Authentication]]></category>
		<category><![CDATA[LDAP]]></category>
		<category><![CDATA[NTLM]]></category>
		<category><![CDATA[Plugin]]></category>

		<guid isPermaLink="false">http://172.17.8.64/gofedora/?p=217</guid>
		<description><![CDATA[Mission
To write a Python program which can be used to authenticate for Squid proxy server. This is useful when you don&#8217;t want to configure complex systems like LDAP, ntlm etc.
Use Cases

When you want to authenticate clients using mysql database.
When you want to authenticate clients using flat files or /etc/passwd file or some custom service on [...]<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></description>
			<content:encoded><![CDATA[<p></p><h4><b>Mission</b></h4>
<p>To write a Python program which can be used to authenticate for Squid proxy server. This is useful when you don&#8217;t want to configure complex systems like LDAP, ntlm etc.</p>
<h4><b>Use Cases</b></h4>
<ol>
<li>When you want to authenticate clients using mysql database.</li>
<li>When you want to authenticate clients using flat files or /etc/passwd file or some custom service on your network.</li>
</ol>
<h4><b>How to proceed</b></h4>
<p>From auth_param section in squid.conf file:</p>
<blockquote><p><i>Specify the command for the external authenticator. Such a program reads a line containing &quot;username password&quot; and replies &quot;OK&quot; or &quot;ERR&quot; in an endless loop. &quot;ERR&quot; responses may optionally be followed by a error description available as %m in the returned error page.</p>
<p> By default, the basic authentication scheme is not used unless a program is specified.</i></p></blockquote>
<p>That clearly states that our python program should read a line from standard input (stdin) and write the appropriate response to the standard output (stdout). But there are some issues with I/O. The output should be unbuffered and should be flushed to standard output immediately after the response is known.</p>
<p>So, lets see a small program where we authenticate using a function &#8216;<i>matchpassword()</i>&#8216;. This function returns <i>True</i> when username, password pair matches and returns <i>False</i> when they mismatch.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
&nbsp;
<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;">socket</span>
<span style="color: #483d8b;">&quot;&quot;&quot;USAGE:The function returns True if the user and passwd match False otherwise&quot;&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> matchpasswd<span style="color: black;">&#40;</span>login,passwd<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Write your own function definition. </span>
    <span style="color: #808080; font-style: italic;"># Use mysql, files, /etc/passwd or some service or whatever you want</span>
    <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    <span style="color: #808080; font-style: italic;"># read a line from stdin</span>
    line = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># remove '\n' from line</span>
    line = line.<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;"># extract username and password from line</span>
    username = line<span style="color: black;">&#91;</span>:line.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    password = line<span style="color: black;">&#91;</span>line.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span><span style="color: black;">&#41;</span>+<span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> matchpasswd<span style="color: black;">&#40;</span>username, password<span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'OK<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ERR<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># Flush the output to stdout.</span>
    <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

</p>
<p>Save the above file somewhere. We save this example file in <i>/etc/squid/custom_auth.py</i> .Now, we have the function for authenticating clients. We need to configure squid to use custom_auth.py . Below is the squid configuration for telling squid to use the above program as basic authenticator.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># you need to specify /usr/bin/python if your file is not executable and needs an interpreter to be invoked.</span>
<span style="color: #666666; font-style: italic;"># Replace /usr/bin/python with /usr/bin/php , if you write auth program in php.</span>
auth_param basic program <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>squid<span style="color: #000000; font-weight: bold;">/</span>custom_auth.py
<span style="color: #666666; font-style: italic;"># how many instances of the above program should run concurrently</span>
auth_param basic children <span style="color: #000000;">5</span>
<span style="color: #666666; font-style: italic;"># display some message to clients when they are asked for username, password</span>
auth_param basic realm Please enter your proxy server username and password
<span style="color: #666666; font-style: italic;"># for how much time the authentication should be valid</span>
auth_param basic credentialsttl <span style="color: #000000;">2</span> hours
<span style="color: #666666; font-style: italic;"># whether username, password should be case sensitive or not</span>
auth_param basic casesensitive on</pre></td></tr></table></div>

</p>
<p>Now, to force clients to authenticate, configure the acls as follow. Below we assume, you want to force all clients on your lan to authenticate for using proxy server.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># acl to force proxy authentication</span>
acl authenticated proxy_auth REQUIRED
<span style="color: #666666; font-style: italic;"># acl to define IPs from your lan</span>
acl lan src 192.168.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">16</span>
<span style="color: #666666; font-style: italic;"># acl to force clients on your lan to authenticate</span>
http_access allow lan authenticated</pre></td></tr></table></div>

</p>
<p>Now, reload/restart squid. That&#8217;s all we need to write and use a custom authentication plugin for squid.</p>
<h4><b>Limitation</b></h4>
<p>Username can&#8217;t contain spaces. Otherwise program will not be able to parse/extract username, password from standard input.</p>
<p><a href="http://gofedora.com/">Fedora Tutorials</a> and <a href="http://cachevideos.com/">Videocache</a> by <a href="http://saini.co.in/">Kulbir Saini</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gofedora.com/how-to-write-custom-basic-authentication-plugin-squid-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

