<?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>MadSoft &#187; Python</title>
	<atom:link href="http://www.madsoft.org/topics/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.madsoft.org</link>
	<description></description>
	<lastBuildDate>Sat, 12 Dec 2009 20:47:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using the Rcon protocol with Python</title>
		<link>http://www.madsoft.org/2009/11/26/using-the-rcon-protocol-with-python/</link>
		<comments>http://www.madsoft.org/2009/11/26/using-the-rcon-protocol-with-python/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 07:32:12 +0000</pubDate>
		<dc:creator>Alec Hussey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[protocol]]></category>
		<category><![CDATA[rcon]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://www.madsoft.org/?p=82</guid>
		<description><![CDATA[Within the PC gaming community, it is quite useful to be able to control or monitor your servers programmatically. The almost universal standard being Rcon or &#8220;Remote Connection.&#8221; The protocol is overall very simple, it only requires a standard UDP socket and that you send a packet header with the login credentials per each request you [...]]]></description>
			<content:encoded><![CDATA[<p>Within the PC gaming community, it is quite useful to be able to control or monitor your servers programmatically. The almost universal standard being Rcon or &#8220;Remote Connection.&#8221; The protocol is overall very simple, it only requires a standard UDP socket and that you send a packet header with the login credentials per each request you make.</p>
<p>We will start by importing the python socket module and creating a UDP connection to the server.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
   sock = <span style="color: #dc143c;">socket</span>.<span style="color: #dc143c;">socket</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">socket</span>.<span style="color: black;">AF_INET</span>, <span style="color: #dc143c;">socket</span>.<span style="color: black;">SOCK_DGRAM</span><span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #808080; font-style: italic;"># Connect to CoD4 server at 8.9.17.24:28960</span>
   <span style="color: #808080; font-style: italic;"># Will also work for most other games in a similar fasion</span>
   sock.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;8.9.17.24&quot;</span>, <span style="color: #ff4500;">28960</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p><span id="more-82"></span><br />
Now that we have established a connection with the game server, we may now start sending it rcon commands. Just remember that the protocol requires that you have a packet header of <code>0xFF0xFF0xFF0xFFrcon password</code> where the password is replaced with your rcon password. You can then follow the packet header by the command you wish to run on the server. In this example we are simply going to print the server status.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">	sock.<span style="color: black;">send</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FFrcon xxxxxxxxxx status&quot;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> sock.<span style="color: black;">recv</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">65565</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Which will display a result similar to this:</p>
<blockquote><p>����print<br />
map: mp_broadcast<br />
num score ping guid name lastmsg address qport rate<br />
&#8212; &#8212;&#8211; &#8212;- &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212; &#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; &#8212;&#8211; &#8212;&#8211;<br />
0 262 137 666a7a82ac0897a213a3a220a1f59893 [0.o]BlameTruth^7 50 80.73.221.70:-8128 31736 25000<br />
1 42 160 42e130756a877c36f71b4f25a96e07cd luluno^7 50 84.195.170.81:-2965 -30455 25000<br />
2 52 36 829a446fd0b0d79f52d8adab5bbbe343 Zack^7 50 173.24.37.249:28960 22992 25000<br />
3 642 31 d3aa0bf47f9c1734b2f7b21089ccafb9 Outlaw^7 0 76.228.193.186:28960 -11398 25000<br />
4 142 110 c53129735bd532ada06d00289e7d609b Hasenpflug^7 50 24.7.31.6:28960 -5755 25000<br />
5 392 96 f44087b315b41f77be8176664e4241f5 [KoS]Karma^7 50 76.14.127.157:28960 5397 25000<br />
6 40 52 4dc7f80bea865494032e4dd782adac8d I Sketchy I^7 0 173.30.206.39:28960 -393 25000<br />
7 0 152 f6aa1b9d5a9865b80e756f43674807e9 fRoz1n1^7 50 99.29.116.44:28960 -17501 25000<br />
8 180 256 79ebdaf076b3301a7c2b95e109ca87d0 REBORN YEH^7 0 58.170.56.2:28960 23402 25000</p></blockquote>
<p>Finally all you have to do is close the connection with sock.close() and the entirety of the code will look something like this:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
	sock = <span style="color: #dc143c;">socket</span>.<span style="color: #dc143c;">socket</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">socket</span>.<span style="color: black;">AF_INET</span>, <span style="color: #dc143c;">socket</span>.<span style="color: black;">SOCK_DGRAM</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># Connect to CoD4 server at 8.9.17.24:28960</span>
	<span style="color: #808080; font-style: italic;"># Will also work for most other games in a similar fasion</span>
	sock.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;8.9.17.24&quot;</span>, <span style="color: #ff4500;">28960</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	sock.<span style="color: black;">send</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FFrcon xxxxxxxxxx status&quot;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> sock.<span style="color: black;">recv</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">65565</span><span style="color: black;">&#41;</span>
	sock.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>As noted, the Rcon protocol is extremely simple and its up to you to think of how you can use Rcon bots to improve the quality and functionality of your game servers. Just remember that you do not have to sock.recv() data from the server every time to send data and remember that the server may send data in chunks in which case you may not receive everything you need in one transmission. But overall its as simple as shown in this tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.madsoft.org/2009/11/26/using-the-rcon-protocol-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interfacing Banshee 1.0 with DBus and Python</title>
		<link>http://www.madsoft.org/2008/06/10/interfacing-banshee-10-with-dbus-and-python/</link>
		<comments>http://www.madsoft.org/2008/06/10/interfacing-banshee-10-with-dbus-and-python/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 03:35:28 +0000</pubDate>
		<dc:creator>Alec Hussey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[banshee]]></category>
		<category><![CDATA[dbus]]></category>
		<category><![CDATA[interface]]></category>

		<guid isPermaLink="false">http://www.madsoft.org/?p=30</guid>
		<description><![CDATA[Ever since the DBUS API change (more like overhaul) during the development of Banshee 1.0 and developers haven&#8217;t yet started supporting it in their plugins so I decided that I would play around with it. From what I have seen, it seems that the perception is that DBus is hard and complicated but its actually [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since the DBUS API change (more like overhaul) during the development of Banshee 1.0 and developers haven&#8217;t yet started supporting it in their plugins so I decided that I would play around with it. From what I have seen, it seems that the perception is that DBus is hard and complicated but its actually really easy and makes things very simple. Essentially you use a DBus debugger (because in most cases, documentation for an applications&#8217; DBus API is not available) like D-Feet to look up which interfaces, methods, properties, and signals are available to you. Then use them to do what you want.</p>
<p><span id="more-30"></span></p>
<p><a href="http://www.madsoft.org/wp-content/uploads/2008/06/dfeet_dbus_debugger.png"><img class="alignnone size-medium wp-image-32" title="dfeet_dbus_debugger" src="http://www.madsoft.org/wp-content/uploads/2008/06/dfeet_dbus_debugger-300x180.png" alt="Bansshe DBus API in D-Feet" width="300" height="180" /></a></p>
<p>Now all we have to do is make a connection to Banshee via DBus and start calling methods. For this we will just use an interactive python interpreter which can be accessed by simply typing &#8220;python&#8221; at any command line prompt. Firstly, we will import the DBuslibrary.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> dbus</pre></div></div>

</blockquote>
<p>Next, we will make a connection to the session bus.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">bus = dbus.<span style="color: black;">SessionBus</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Then we will get Banshee&#8217;s PlayerEngine object which will let us control the player itself (Play, Pause, Next, Previous, Current Track, etc).</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">banshee = bus.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;org.bansheeproject.Banshee&quot;</span>, <span style="color: #483d8b;">&quot;/org/bansheeproject/Banshee/PlayerEngine&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>The first argument we pass, &#8220;org.bansheeproject.Banshee&#8221;, is the interface we want to connect to and &#8220;/org/bansheeproject/Banshee/PlayerEngine&#8221; is the path to the object that we want to use. Now we can start doing stuff with Banshee. You can pretty much take any of the methods listed in D-Feet and run them. For example:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">banshee.<span style="color: black;">Open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;file:///home/maddog39/Music/Eisbrecher - Antikörper.mp3&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>And to play the song&#8230;</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">banshee.<span style="color: black;">Play</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Once you have had enough&#8230;.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">banshee.<span style="color: black;">Pause</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Although note that you dont have to using the Open() method to use the Play() and Pause() methods, but Open() lets you open an arbitrary media file otherwise those other methods use the currently playing track. Now, to access properties you generally just take the name of the property and prepend &#8216;Get&#8217; to it and make it a method. So for example:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">banshee.<span style="color: black;">GetCanPause</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Returns:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">dbus.Boolean(False)</pre></div></div>

</blockquote>
<p>Or if we wanted to display all the information about the currently playing track we might do&#8230;</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> key <span style="color: #ff7700;font-weight:bold;">in</span> currentTrack.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> key + <span style="color: #483d8b;">&quot;: &quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>currentTrack<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Which gives us:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">album: Issues
rating: 0
name: Falling Away From Me
artist: Korn
URI: file:///home/maddog39/Music/Korn%20-%20Beating%20Me%20Down.mp3
length: 271.211
disc: 0
track-count: 16
year: 1999
track-number: 2</pre></div></div>

</blockquote>
<p>Another cool thing you can do with DBus is receive signals. What this allows us to do is run code when a given signal is triggered or received. We do this by connecting a python method with a signal on our bus. So if we wanted to do something every time Banshee changes its state (playing, paused, loading, loaded, etc) this is what would happen. We would first have to setup a main loop as it is required for a signals to function.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> gobject
<span style="color: #ff7700;font-weight:bold;">from</span> dbus.<span style="color: black;">mainloop</span>.<span style="color: black;">glib</span> <span style="color: #ff7700;font-weight:bold;">import</span> DBusGMainLoop
dbus.<span style="color: black;">mainloop</span>.<span style="color: black;">glib</span>.<span style="color: black;">DBusGMainLoop</span><span style="color: black;">&#40;</span>set_as_default=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Then we would create a callback method which would be run every time the signal is triggered.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> stateChanged<span style="color: black;">&#40;</span>state<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;State Changed: %s&quot;</span> <span style="color: #66cc66;">%</span> state</pre></div></div>

</blockquote>
<p>Now we can add our signal receiver to our method.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">bus.<span style="color: black;">add_signal_receiver</span><span style="color: black;">&#40;</span>stateChanged,
dbus_interface=<span style="color: #483d8b;">&quot;org.bansheeproject.Banshee.PlayerEngine&quot;</span>,
                        signal_name=<span style="color: #483d8b;">&quot;StateChanged&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Finally, at the end of a script that uses signals we would want to run our main loop.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">gobject.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Well thats pretty much it! Just keep in mind that this was not intended to be a tutorial but merely a guideline on how to do interact and do things with Banshee over DBus. So feel free to sit down and write some of your own scripts based on what you have learned here and I hope this helps. I have already written my own program to integrate Banshee into another application which I will talk about in another article. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.madsoft.org/2008/06/10/interfacing-banshee-10-with-dbus-and-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easily create IRC bots in Python with PyBotlib</title>
		<link>http://www.madsoft.org/2008/05/09/easily-create-irc-bots-in-python-with-pybotlib/</link>
		<comments>http://www.madsoft.org/2008/05/09/easily-create-irc-bots-in-python-with-pybotlib/#comments</comments>
		<pubDate>Fri, 09 May 2008 19:19:02 +0000</pubDate>
		<dc:creator>Alec Hussey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[botlib]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[irc]]></category>

		<guid isPermaLink="false">http://www.madsoft.org/2008/05/09/easily-create-irc-bots-in-python-with-pybotlib/</guid>
		<description><![CDATA[Back when I wrote ProxyBot, I was frustrated by the fact that there was a lack of maintained and documented third party libraries for the IRC client protocol. So I essentially wrote my own implementation of the IRC client protocol for use with the bots that I write. Well I wrote the library to take [...]]]></description>
			<content:encoded><![CDATA[<p>Back when I wrote ProxyBot, I was frustrated by the fact that there was a lack of maintained and documented third party libraries for the IRC client protocol. So I essentially wrote my own implementation of the IRC client protocol for use with the bots that I write. Well I wrote the library to take advantage of OOP (Object Oriented Programming for those who don&#8217;t know) which in turn made it really quick and easy for me to throw together a bot.</p>
<p>So this is a simple little tutorial on how to write a basic &#8220;Hello World&#8221; bot using PyBotlib. The library itself is only a single file so I have not bothered to repackage it or write setup scripts as you will only need import that single file within your application. You may get the latest source code for the library at any time <a title="botlib.py" href="http://ircproxybot.googlecode.com/svn/trunk/botlib.py" target="_blank">here</a> (google code/SVN) or <a title="PyBotlib 1.0" href="http://www.madsoft.org/wp-content/uploads/2008/05/botlib.py" target="_blank">here</a> (blog). So here we go&#8230;</p>
<p><span id="more-28"></span><br />
Lets start by defining our bot class and its constructor.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> botlib
&nbsp;
<span style="color: #808080; font-style: italic;"># Create a new class for our bot, extending the Bot class from botlib</span>
<span style="color: #ff7700;font-weight:bold;">class</span> HelloWorldBot<span style="color: black;">&#40;</span>botlib.<span style="color: black;">Bot</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, server, channel, nick, password=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        botlib.<span style="color: black;">Bot</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, server, <span style="color: #ff4500;">6667</span>, channel, nick<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Send nickserv password if available</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> password <span style="color: #66cc66;">!</span>= <span style="color: #008000;">None</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">protocol</span>.<span style="color: black;">privmsg</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nickserv&quot;</span>, <span style="color: #483d8b;">&quot;identify&quot;</span> <span style="color: #66cc66;">%</span> password<span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Next we are going to want to override the __actions__ function of the Bot class which will handle all of our bots actions. This acts as essentially a main loop and is called infinitely until the bot is shutdown. In here we put code for our commands, responders, whatever other interaction with the IRC channel that we need.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> __actions__<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        botlib.<span style="color: black;">Bot</span>.__actions__<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Create a Hello World responder/command</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> botlib.<span style="color: black;">check_found</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">data</span>, <span style="color: #483d8b;">&quot;!hello&quot;</span><span style="color: black;">&#41;</span>:
            <span style="color: #808080; font-style: italic;"># Get the senders username</span>
            username = <span style="color: #008000;">self</span>.<span style="color: black;">get_username</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;"># Send user a message in response</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">protocol</span>.<span style="color: black;">privmsg</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">channel</span>, <span style="color: #483d8b;">&quot;Hello %s!&quot;</span> <span style="color: #66cc66;">%</span> username<span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>So here, we have created a simple command called !hello which runs the code underneath its if statement when an instance of that command is found in the chat. Then we call the get_username() function of the Bot class (remember that we inherited from the Bot class) to get the user name of the person who initiated the command. After that, we simply send a message back to the channel using the equivalent function for IRC&#8217;s PRIVMSG command. Now all we to do is instantiate and run our new bot.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"> <span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #808080; font-style: italic;"># Create new instance of our bot and run it</span>
    HelloWorldBot<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;irc.wyldryde.org&quot;</span>, <span style="color: #483d8b;">&quot;#maddog39&quot;</span>, <span style="color: #483d8b;">&quot;HelloWorldBot&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Keep in mind that when you are instantiating Bot object, that because PyBotlib is entirely threaded, you can make as many instances of your bot as you would like. I do have a BotManager class in PyBotlib intended for managing sets of multiple bots on the same process however I have never actually tested it so your results may vary. Here is the script in its entirety.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> botlib
&nbsp;
<span style="color: #808080; font-style: italic;"># Create a new class for our bot, extending the Bot class from botlib</span>
<span style="color: #ff7700;font-weight:bold;">class</span> HelloWorldBot<span style="color: black;">&#40;</span>botlib.<span style="color: black;">Bot</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, server, channel, nick, password=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        botlib.<span style="color: black;">Bot</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, server, <span style="color: #ff4500;">6667</span>, channel, nick<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Send nickserv password if availible</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> password <span style="color: #66cc66;">!</span>= <span style="color: #008000;">None</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">protocol</span>.<span style="color: black;">privmsg</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nickserv&quot;</span>, <span style="color: #483d8b;">&quot;identify&quot;</span> <span style="color: #66cc66;">%</span> password<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> __actions__<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        botlib.<span style="color: black;">Bot</span>.__actions__<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Create a Hello World responder/command</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> botlib.<span style="color: black;">check_found</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">data</span>, <span style="color: #483d8b;">&quot;!hello&quot;</span><span style="color: black;">&#41;</span>:
            <span style="color: #808080; font-style: italic;"># Get the senders username</span>
            username = <span style="color: #008000;">self</span>.<span style="color: black;">get_username</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;"># Send user a message in response</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">protocol</span>.<span style="color: black;">privmsg</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">channel</span>, <span style="color: #483d8b;">&quot;Hello %s!&quot;</span> <span style="color: #66cc66;">%</span> username<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #808080; font-style: italic;"># Create new instance of our bot and run it</span>
    HelloWorldBot<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;irc.wyldryde.org&quot;</span>, <span style="color: #483d8b;">&quot;#maddog39&quot;</span>, <span style="color: #483d8b;">&quot;HelloWorldBot&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

</blockquote>
<p>Finally, to run your new bot, simply issue the command:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python helloworldbot.py</pre></div></div>

</blockquote>
<p>At the terminal prompt or command prompt (windows). Also note that you must make sure that your botlib.py file is in the same directory as your bot. Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.madsoft.org/2008/05/09/easily-create-irc-bots-in-python-with-pybotlib/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
