<?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>kerneljack&#039;s diary &#187; scripting</title>
	<atom:link href="http://www.kerneljack.com/category/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerneljack.com</link>
	<description></description>
	<lastBuildDate>Sun, 27 Feb 2011 13:22:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Doing quick tasks in Python</title>
		<link>http://www.kerneljack.com/2006/03/31/doing-quick-tasks-in-python/</link>
		<comments>http://www.kerneljack.com/2006/03/31/doing-quick-tasks-in-python/#comments</comments>
		<pubDate>Fri, 31 Mar 2006 21:27:13 +0000</pubDate>
		<dc:creator>kerneljack</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[computers]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://kerneljack.wordpress.com/2006/03/31/doing-quick-tasks-in-python/</guid>
		<description><![CDATA[A lot of the time at work I have to come up with quick solutions to various programming problems, and I mean *real* quick. There is usually very little time for finding the right library or open source toolkit that &#8230; <a href="http://www.kerneljack.com/2006/03/31/doing-quick-tasks-in-python/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A lot of the time at work I have to come up with quick solutions to various programming problems, and I mean *real* quick. There is usually very little time for finding the right library or open source toolkit that has already solved the problem or some part of it and then to figure out how to integrate that into our own workflow. It is usually much quicker to simply write a few scripts that get the job done.</p>
<p>In such situations I increasingly find myself solving these problems using the Python programming language. I&#8217;ve usually whipped up scripts using bash scripts and even Perl but I often find that I have to &#8220;re-learn&#8221; Perl again and again, or at least the part that I&#8217;m temporarily using to solve some problem. Python just seems (and looks) so much natural and cleaner. I&#8217;ve been fascinated by Ruby recently after my brush with Ruby on Rails and I am seriously trying to find a project to use it in all the time!</p>
<p>Anyway I wanted to give some examples of Pythons&#8217; ability to solve quick problems. I&#8217;ve used it to make remote backups, check if certain services are running remotely and to fetch and delete mail off a remote server. For example, the following snippet of code can be used to send mail:</p>
<blockquote>
<pre>import smtplib

mailServer = smtplib.SMTP(serverURL)
mailServer.sendmail(sender, to, message)
mailServer.quit()</pre>
</blockquote>
<p>Really sweet and simple. I&#8217;m sure you can do this in only 1 line or even half a line in Perl (or even Python!) but this is really clear and concise. When I needed to write a script to send mail, I simply googled for it and dug this up in literally 20 secs! That&#8217;s one other reason I like writing scripts in Python: you can always google for snippets of code to do stuff.</p>
<p>Similarly, the following can be used to check if SSH is running and accepting connections on a remote server:</p>
<blockquote>
<pre>IDENT_STRING = "SSH"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, int(PORT)))
data = s.recv(1024)
s.close()

if data.find(IDENT_STRING) == -1:
sys.exit (1)
# if everything OK, exit normally
sys.exit(0)</pre>
</blockquote>
<p>If you put this in a script and run it, you can use the exit code (echo #?) to determine if SSH was running or not. Very useful if you&#8217;re writing a script to make backups to a remote server; you need to make sure that the service is up and running on the server and that it is accepting connections from the local machine.</p>
<p>I will try to see if Ruby makes any of these tasks even easier to do or perhaps easier to write and comprehend. I might even give Groovy a try since Java is my day job and Groovy is described as a scripting language for Java programmers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerneljack.com/2006/03/31/doing-quick-tasks-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

