Terminate local bottle server

I had my local bottle server running and my Terminal shut down. After reboot, I wasn’t able to start the server again because it was in use. I had to kill the open port connection. My bottle was running on 8081 so I searched for protocols using that port:

netstat -nap | grep 8081
tcp   0    0 0.0.0.0:8081     0.0.0.0:*      LISTEN      6491/python

Next I killed the PID by executing:

kill -9 6491

Python – Sorting JSON by Keys

Python doesn’t allow sorting of a dictionary. But while converting dictionary to a JSON, you can explicitly sort it so that the resulting JSON is sorted by keys. This is true for multi-dimentional dictionary. In case you need to sort a multi-dimentional JSON, convert it to dictionary and then dump it back to JSON with sorted flag.

ud = json.loads(js)
print js # Unsorted JSON
print ud # JSON to Dictionary (Unsorted)
print json.dumps(ud, sort_keys=True) # This JSON will be sorted recursively

jQuery – POST Data via jquery.POST

The previous post shows the old school way to POST data using Ajax (XMLHttp). jQuery surprisingly does all that for you within a wrapper. Here’s an equivalent for the jQuery post:


$(document).ready(function(){
	$( '[name="eval|hostname"]' ).bind("blur", getAvailableHost);
});

function getAvailableHost() {
	var $hostname = $( this );  //-- getAvailableHost() was set as an eventHandler on bind. So this refers to the hostname textbox
	var $spanHostname = $( "#id_availableHostname" ); //-- Span where the available hostname from post response will be set
	
	if ($hostname.val().match(/^[a-z0-9]+[a-z0-9-]*[a-z0-9]+$/g) == null) {
		alert("Enter valid hostname. Only Alphanumeric and Hyphens (in the middle of string) allowed.");
		$hostname.focus();
		return false;
	}
	$.post("getHostnameProxy.php",
		{
			hostname : $hostname.val(),
			domain : "yourdomain.com"
		},
		function(data, status) {
			//-- Expected data and status are returned by the server
	})
		.done(function(data) {
			var obj = jQuery.parseJSON(data);
			$spanHostname.text(obj.AvailableHost);	//-- Can also be accessed via obj['AvailableHost']
		})
		.fail(function() {
			$spanHostname.text('');
	});
	return true;
}

PS: The ‘hostname’ and ‘domain’ posted by the Ajax call can be accessed on the server using from the POST packet. e.g. PHP server script can access via $_POST['hostname'] and $_POST['domain']

JavaScript – POST data via AJAX call

Here’s how I used the old school way to POST data to a URL using AJAX.

function getNextHost() {
	var $hostname = $( '[name="eval|hostname"]' ); //Input provided to check if it already exists in DB
	var _hostname = $hostname.val();
	var $spanHostname = $( "#id_availableHostname" );	//Span where the result will be displayed

	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			var jsn = eval ("(" + xmlhttp.responseText + ")");
			//alert(jsn["ResultCode"]);
			
			//--- Setting the available Hostname adjacent to the input field ---
			$spanHostname.text(jsn['AvailableHost']);
		}
	}
	xmlhttp.open("POST","getHostnameProxy.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("hostname=".concat(_hostname).concat("&domain=yourDomain.com"));
	return true;
}

The data submitted can be accessed by the server via $_POST['hostname'], $_POST['domain']