<?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; sockets</title>
	<atom:link href="http://www.madsoft.org/tags/sockets/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>
	</channel>
</rss>
