Basic threading in JavaScript; multiple threads of execution in JavaScript

I was searching the web yesterday for this and didn’t really find a simple way of doing this and suddenly, I remembered something.

It’s extremely simple, someone could build on it with all sorts of features but for now, here is an example:
http://images.code-head.com/code/javascript/js-threads.html

As you can see there are two counters, one is counting up and the other down simultaneously.

Here is the start_thread function:

             function thread_start(callback) {
		setTimeout(callback, 1);
		return true;
	}

The trick is that setTimeout *does not* block the execution ;)

I hope this helps someone :)

Comments (0)   Filed under: Fun, General, JavaScript, Programming, jQuery   Posted by: Codehead on June 30, 2009

Missing from Python: kill-able threads and popens with timeouts…

It would be nice to be able to kill threads anytime you wanted, there are solutions to this but are not part of the Python library and I didn’t yet get a chance to try them… Actually there is only one promising solution to this.

The other thing I would love to have in Python is to be able to call popen with a second parameter which is a timeout, That is (obviously) popen would give up and return if the call was taking more than x number of seconds…

Comments (0)   Filed under: Programming, Python   Posted by: Codehead on May 15, 2009

Python win32api and pywintypes.error: (5, ‘TerminateProcess’, ‘Access is denied.’)

I was working on a piece of code that was supposed to close a bunch of processes ‘with the same name’ right after each other and was getting this error message:
pywintypes.error: (5, ‘TerminateProcess’, ‘Access is denied.’)

The problem was that I was doing this in a loop and Windows didn’t get enough time to close the first process yet. To fix this, just add a time.sleep(0.5) in your loop and you will be all set…

I hope this helps :)

Here is the code I’m using (found in Python mailing list):

import time
import win32api, win32pdhutil, win32con
import win32pdh, string
 
# ***********************************************************************
# ***********************************************************************
def GetAllProcesses():
  object = "Process"
  items, instances = win32pdh.EnumObjectItems(None,None,object, 
win32pdh.PERF_DETAIL_WIZARD)
  return instances
# ***********************************************************************
 
 
# ***********************************************************************
# ***********************************************************************
def GetProcessID ( name ) :
  object = "Process"
  items, instances = win32pdh.EnumObjectItems(None,None,object, win32pdh.PERF_DETAIL_WIZARD)
 
  val = None
  if name in instances :
    hq = win32pdh.OpenQuery()
    hcs = []
    item = "ID Process"
    path = win32pdh.MakeCounterPath( (None,object,name, None, 0, item) )
    hcs.append(win32pdh.AddCounter(hq, path))
    win32pdh.CollectQueryData(hq)
    time.sleep(0.01)
    win32pdh.CollectQueryData(hq)
 
    for hc in hcs:
      type, val = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
      win32pdh.RemoveCounter(hc)
    win32pdh.CloseQuery(hq)
    return val
# ***********************************************************************
 
# ***********************************************************************
# ***********************************************************************
def Kill_Process_pid ( pid ) :
  handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pid) #get process handle
  win32api.TerminateProcess(handle, -1) #kill by handle
  win32api.CloseHandle(handle)        #close api
# ***********************************************************************
 
 
# ***********************************************************************
# ***********************************************************************
def Kill_Process ( name ) :
  pid = GetProcessID(name)
  if pid:
    try:
        Kill_Process_pid(pid)
        return True
    except:
        pass
  else:
    return False
# ***********************************************************************

I just modified the last function, it’s kind of funny I know but that works for me, then I call it like:

print 'Killing IEs...',
while Kill_Process('iexplore'):
   time.sleep(0.5)
print 'Done!'
Comments (0)   Filed under: Programming, Python   Posted by: Codehead on

Ungzip a string in Python - gzinflate in Python

I was very surprised that I couldn’t do this easily in Python, so here is the solution to this:

import zlib
# ...
ungziped_str = zlib.decompressobj().decompress('x\x9c' + gziped_str)

Wow, PHP does it like this:

$ungziped_str = gzinflate($gziped_str);
Comments (0)   Filed under: Programming, Python   Posted by: Codehead on May 6, 2009

Python “Unhandled exception in thread started by Error in sys.excepthook:”

I was getting this error message and I was using a Queue object to queue some jobs and block everything until all threads are done with:

the_queue.join()

Well, in my particular case, I was getting these error messages the work was not done in worker threads:

Unhandled exception in thread started by
Error in sys.excepthook:

Original exception was:

So what I did was this:

threads = []
for i in range(0, max_threads):
   thread = Worker()
   thread.start()
   threads.append(thread)
 
# And then...
for thread in threads:
   thread.join()

And this fixed my issue, this will make sense to people with this problem ;)

Comments (0)   Filed under: Programming, Python   Posted by: Codehead on

Manually move WHM/cPanel accounts - WHM account transfer function timeout

Before you do it
You will have to make sure that both Apache web servers are the same version, I didn’t and it caused some problems.

My hosting company also mentioned that both cPanels must be the same version too, but I’m not sure about this.

You will also need root access to both servers and an SSH client like putty, so go ahead and connect to both servers.

Manually Moving WHM/cPanel accounts
Follow these steps:
1 - In your old server do:
/scripts/pkgacct “username” <------- Don't type the quotes

2 - After the first step is done do:
scp /home/username.tar.gz root@IP:/home/ <------- Where IP is the IP of your destination server

You will be asked for the root password.

3 - In your destination server do:
cd /home
/scripts/restorepkg “username” <------- Don't type the quotes

Disclaimer
Do this at your own risk, it worked fine on my version of WHM/cPanel/CentOS.

Comments (0)   Filed under: Server, WHM/cPanel   Posted by: Codehead on April 13, 2009

The nx service is not available or the nx access was disabled - CentOS

If you follow the instruction here:
http://wiki.centos.org/HowTos/FreeNX

You will get an error message from NX client:
The nx service is not available or the nx access was disabled

To fix this, connect to your SSH and type:
cat /var/lib/nxserver/home/.ssh/client.id_dsa.key

Copy the text you get and in your NX client, on your login dialog box go to:
Configure… > General Tab > Key

And paste the text there and you will be fine :)

Comments (2)   Filed under: CentOS, Server   Posted by: Codehead on April 2, 2009

PML - A Python template engine

I finally decided to release PML as an open source software. I developed this template engine for a project that unfortunately never happened using Python.

Here is a sample application using PML and Yahoo! Search API:
http://web-search.code-head.com/

You can download it here:

[D O W N L O A D]

The web search example is also included in the download.

PML was written to be fast, simple and compact. Here are some of it’s features:
1 - Template filters
2 - Output filters
3 - Template variable filters
4 - Template cache - default
5 - Bytecode cache - default
6 - Complete output buffer cache
7 - Garbage collection
8 - Output compression - GZIP
9 - A powerful, quick compiler
10 - Ability to add helpers easily
11 - Ability to add custom compiler tags
12 - Auto escaping your variables, even lists, dicts, and tuples
13 - Compile templates once until you edit them

And more.

I will write a tutorial in my next post.

Comments (0)   Filed under: Programming, Python, Web Design, Web Development   Posted by: Codehead on March 31, 2009

A PHP Competition 2

Say that you are generating an XML sitemap for your site and there are 5,000 URLs.

Here is the code:

<?php
 
	$buffer =
'<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
';
 
	$results = fetch_5000_urls_order_by_importance();
	$count = 0;
	while ($row = $results->fetch_next_url()) {
		$priority = ##########################################;
		$url = $row['url'];
		$date = date('Y-m-d', $row['date']);
		$buffer .= "
			<url>
				<loc>$url</loc>
				<lastmod>$date</lastmod>
				<changefreq>weekly</changefreq>
				<priority>$priority</priority>
			</url>
			";
	}
 
	$buffer .= '
	</urlset>
	';
 
	echo $buffer;
 
?>

What you want to do is to give each URL a priority; URLs between 0 to 1000 will have a priority of 0.9
1000 to 2000 => 0.8
2000 to 3000 => 0.7
3000 to 5000 => 0.6
4000 to 5000 => 0.5

Replace ########################################## with the code that calculates the priorities, and remember not to use ; in it ;)

Comments (3)   Filed under: Fun, PHP, Programming   Posted by: Codehead on March 30, 2009

How to clean the screen with PHP in Linux (cls, Linux, PHP)

Here is the trick, in your script use:

system("clear");

I hope this helps someone.

Comments (2)   Filed under: Operating Systems, PHP, Programming   Posted by: Codehead on March 17, 2009

A PHP script for dealing with DoS attacks

Here is a simple script that will show you what IP addresses are making how many requests to your server.

<?php
 
## Functions ##
 
function getIP($line) {
	ereg("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}",$line,$regMatch);
	$ip = $regMatch[0];
	if($ip) return $ip; else return "false";
}
 
function processString($string, $size = 18) {
	$string = "[ ".$string;
	$length = strlen($string);
	$toAdd = $size - $length;
 
	for($x = 0; $x < $toAdd; $x++) {
		$string = $string." ";
	}
	$string = $string."]";
	return $string;
}
 
## Code ##
 
while (true) {
	$cmd = "netstat -n | awk '{ print $5 }'";
	exec($cmd, $netstatArray);
	$ipArray = array();
 
	foreach($netstatArray as $line) {
		$ip = getIP($line);
		if($ip != "false" && ip != "127.0.0.1") {
			if(array_key_exists($ip, $ipArray))
						 {
									$ipArray[$ip]+=1;
						 }
						 else // if not, count=1
						 {
									$ipArray[$ip] = 1;
						 }
		}
	}
 
	asort($ipArray);
 
	system("clear");
	foreach($ipArray as $ip => $count) {
		if ($count < 15)
			continue;
		echo processString($ip);
		echo "\t" .processString(gethostbyaddr($ip), 55);
		echo "\tTimes Accessed: " .$count ."\n";
	}
 
	echo str_repeat("-", 50) ."\n";
	exec("top -n 1", $top_str);
	preg_match("#load average:(.+)#i", $top_str[0], $match);
	echo "Load Average: " .$match[1] ."\n";
	echo str_repeat("-", 50) ."\n";
	echo 'Showing $count >= 15: (Escape with ctrl+c)' ."\n";
 
	sleep(10);
}
 
?>

After identifying the IP addresses that are sending many requests at once to crash your server, you can ban them using a firewall software. I personally recommend APF: http://rfxnetworks.com/apf.php

You can do:
apf -d THEIPADDRESS SOMECOMMENTLIKEPOSSIBLEDOS

This script was originally written by a former employee of Acenet Inc and was modified by me. Acenet Inc is a great web hosting company with great support and fantastic staff members who will help you 24×7.

Here is some info about Denial of Service attacks (DoS attacks).
http://en.wikipedia.org/wiki/Denial-of-service_attack

I hope this helps someone.

Comments (2)   Filed under: PHP, Security, Server   Posted by: Codehead on March 16, 2009

Another great TED talk: David Bolinsky: Fantastic voyage inside a cell

Comments (0)   Filed under: Fun, Science   Posted by: Codehead on February 26, 2009

A Tiny MySQL++ Tutorial; C++ and MySQL; MySQL++ Example

I wrote a post about how to install MySQL++ and I thought I will write a quick tutorial on how to use it too.

This is a very basic MySQL++ program and it’s very self explanatory:

#include <mysql++.h>
#include <stdlib.h>
 
using namespace std;
using namespace mysqlpp;
 
int main() {
    try {
        Connection conn(false);
        conn.connect("DB NAME", "DB HOST probably localhost", "DB USER", "DB PASS");
        Query query = conn.query();
    } catch (BadQuery er) { // handle any connection or                      
        // query errors that may come up 
        cerr << "Error: " << er.what() << endl; 
        return -1;
    } catch (const BadConversion& er) {
        // Handle bad conversions
        cerr << "Conversion error: " << er.what() << endl <<
                "\tretrieved data size: " << er.retrieved <<
                ", actual size: " << er.actual_size << endl;
        return -1;
    } catch (const Exception& er) {
        // Catch-all for any other MySQL++ exceptions
        cerr << "Error: " << er.what() << endl;
        return -1;
    }
 
    return (EXIT_SUCCESS);
}

This will connect to your database and creates a query object ready to go. Save this as test.cpp

To compile this, you will have to create a Makefile in the same folder, so create a file and name it “Makefile” (no quotes) and add these to it:

CXX := g++
CXXFLAGS := -I/usr/include/mysql -I/usr/local/include/mysql++
LDFLAGS := -L/usr/local/lib -lmysqlpp -lmysqlclient -lnsl -lz -lm
EXECUTABLE := main
 
all: test
 
clean:
	rm -f $(EXECUTABLE) *.o

If you get funny errors, make sure there are no extra spaces in this file, and there is only one “tab” behind the line: rm -f $(EXECUTABLE) *.o

Now, you can make and run this by doing:
make
./test

Now, I’m going to show you how to execute some queries and you can go ahead and experiment on your own tables.

Here is a INSERT query followed by a SELECT; this will hopefully cover a lot, because INSERT is a type of query that doesn’t return anything and you need to escape values in order to INSERT.
SELECT on the other hand returns rows, although there are 3 ways that they can be done but here is a simple way that works for me.

#include <mysql++.h>
#include <stdlib.h>
 
using namespace std;
using namespace mysqlpp;
 
int main() {
    try {
        Connection conn(false);
        conn.connect("DB NAME", "DB HOST probably localhost", "DB USER", "DB PASS");
        Query query = conn.query();
 
        /* To insert stuff with escaping */
        query << "INSERT INTO some_table " <<
                     "VALUES (" <<
                     "'', " << /* This is left empty because the column is AUTO_INCREMENT */
                     "\"" << escape << some_var_that_contains_some_value << "\"" <<
                     ");";
        query.execute();
        /* That's it for INSERT */
 
        /* Now SELECT */
        query << "SELECT * FROM biz LIMIT 10";
        StoreQueryResult ares = query.store();
        for (size_t i = 0; i < ares.num_rows(); i++)
           cout << "Name: " << ares[i]["name"] << " - Address: " << ares[i]["address"] << endl;
 
        /* Let's get a count of something */
        query << "SELECT COUNT(*) AS row_count FROM biz";
        StoreQueryResult bres = query.store();
        cout << "Total rows: " << bres[0]["row_count"];
 
    } catch (BadQuery er) { // handle any connection or                      
        // query errors that may come up 
        cerr << "Error: " << er.what() << endl; 
        return -1;
    } catch (const BadConversion& er) {
        // Handle bad conversions
        cerr << "Conversion error: " << er.what() << endl <<
                "\tretrieved data size: " << er.retrieved <<
                ", actual size: " << er.actual_size << endl;
        return -1;
    } catch (const Exception& er) {
        // Catch-all for any other MySQL++ exceptions
        cerr << "Error: " << er.what() << endl;
        return -1;
    }
 
    return (EXIT_SUCCESS);
}

Again, whenever you want to run your code do:
make
./test

I wrote this because something like this would help myself a lot.

Good Luck :)

Update

query.reset();

Will reset the query object; this is useful when you are (in your program) generating a MySQL query but must discard it and generate another query (based on some condition).

Comments (5)   Filed under: C++, MySQL, Programming   Posted by: Codehead on February 25, 2009

Installing MySQL++; How to install MySQL++ on Linux-CentOS

There is a lack of documentation on how to do this, I guess they assume that you must know a lot IF you are trying to use this library.

But here are step by step instructions on how to do it.

What you need
Root access to your server.
An SSH client, like putty, it’s free.

Let’s do it
1 - Run your SSH client and connect to your server using root.

2 - Do:

cd /

Note: I always make a folder like this

mkdir Tools
cd Tools

Note: This is version 3.0.9 obviously, it’s best if you check this web page and download the latest source code: http://www.tangentsoft.net/mysql++/

wget http://www.tangentsoft.net/mysql++/releases/mysql++-3.0.9.tar.gz
tar xvfz mysql++-3.0.9.tar.gz
cd mysql++-3.0.9
./configure
make
make install

3 - Now, in order for:

#include <mysql++.h>

To work, you will have to add a line to your ld.so.conf; you will find this file (hopefully) in /etc so go to:
/etc
And edit ld.so.conf and add this line to the end of it:
/usr/local/lib

Note: If ld.so.conf is not in /etc you can try “whereis ld.so.conf” (no quotes)

Save it and run:

ldconfig

And you are all set.

Please also note that, this worked on my CentOS and might not work exactly like this on your distribution.

Comments (3)   Filed under: C++, MySQL, Programming   Posted by: Codehead on

Slowing down Yahoo! Slurp (Yahoo! Search Bot)

A few weeks ago one of our servers started to crash every few days.

After investigating this issue for a while we found 100s of IP addresses from Yahoo! and Inktomi Corporation which is the company who developed Yahoo! Slurp.

So basically Yahoo! was launching DOS attacks against our server although unintentional but very annoying. It also shows that their technology is not as advanced as Google or other search engines or it’s buggy, Whatever you think, crashing people’s servers with your search bot is not cool at all and shows that you have to work on it a little more…

If you have the same problem here is a simple solution Yahoo! suggest; open your robots.txt (create it if you don’t have it and place it in your root folder) and add these lines to it:

User-agent: Slurp
Crawl-delay: 1

Only Yahoo! will understand the line Crawl-delay: X and Yahoo! suggests that you use a small number between 0.5 and 1.

Comments (0)   Filed under: Annoying Stuff, Search Engines, Server   Posted by: Codehead on February 22, 2009

A little Program For Monitoring Your Websites, With Alarm

I hope someone will find this useful:

###
 #    Hamid Alipour
###
 
###
 # Config
#####################################################
urls = ['http://www.P U T  Y O U R  U R L S  H E R E.com/']
url_timeout = 5
sleep_time = 5 * 60
failed_count_before_alarm = 10
sleep_time_on_failure = 15
#####################################################
 
import urllib2
import winsound
import time
 
class SiteMonitor:
 
    def __init__(self):
        pass
 
    def start(self):
        while True:
            for url in urls:
                if not self.check_url(url):
                    if not self.is_it_live(url):
                        self.alarm()
            self.sleep(sleep_time)
 
    def is_it_live(self, url):
        failed_count = 0
        while failed_count < failed_count_before_alarm:
            if not self.check_url(url):
                failed_count += 1
            else:
                return True
            self.sleep(sleep_time_on_failure)
        return False
 
    def check_url(self, url):
        print 'Checking...' + url + '...',
        try:
            urllib2.urlopen(url, timeout=url_timeout)
        except:
            print 'Failed!'
            return False
        print 'Done it\'s up!'
        return True
 
    def alarm(self):
        print 'Too many failures...'
        while True:
            print 'Alarm...'
            winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
 
    def sleep(self, seconds):
        print 'Sleeping for ' + str(seconds) + ' second(s)...'
        time.sleep(seconds)
        print 'Waking up...'
 
def main():
    SiteMonitor().start()
 
if __name__ == "__main__":
    main()

Save it as sitemonitor.py and run it like:
python sitemonitor.py

Or just double click on the file.

Please note: in order to run this, you will have to install Python:
http://www.python.org/download/

Comments (0)   Filed under: Python, Server   Posted by: Codehead on February 20, 2009

Web History Book; Find Out Who Owned Your Domain!

So many people might not know that there is a service that is saving web pages since 1996 and is like a history book of websites.

The following pages might load slow, be patient.

You can see the old Facebook:
http://web.archive.org/web/20040212031928/http://www.thefacebook.com/

Facebook started as thefacebook.com because facebook.com was taken and you can see here what was on facebook.com:
http://web.archive.org/web/20000823042335/http://www.facebook.com/

They bought facebook.com in 2005 for $200,000!!!

Here you can see how simple Yahoo looked like back in 1996:
http://web.archive.org/web/19961017235908/http://www2.yahoo.com/

Microsoft:
http://web.archive.org/web/19961020014044/http://www.microsoft.com/
(Pretty good for 1996 though)

Or baby Google:
http://web.archive.org/web/19981111184551/http://google.com/ :)

Google, same year, 1996:
http://web.archive.org/web/19981202230410/http://www.google.com/

Here is Digg:
http://web.archive.org/web/20041211042536/http://digg.com/

And twitter:
http://web.archive.org/web/20061006030354/http://twitter.com/

You can also see here that some guy owned code-head.com but he gave up on it:
http://web.archive.org/web/20010719161538/http://www.code-head.com/

As you probably know by now the site is archive.org.

Enjoy :)

Comments (0)   Filed under: Fun, General, Internet   Posted by: Codehead on January 29, 2009

Web App Security: XSS Attacks

Today, I saw a funny comment on a website:

<script> alert('0wn3d by X - X') </script>
 
<META HTTP-EQUIV=Refresh CONTENT="0; URL=Some URL">

In case you don’t know about these types of attacks, an attacker will write this comment on a blog (or any sort of web application) and if the application doesn’t escape it before displaying it, this code will display an alert box and then redirects your visitors to whatever the URL is right away.

So again, if I visit this page, I see the alert box and will be redirected to another page on the Internet.

To prevent this, you will have to escape all user generated content before displaying them on your pages, in PHP:

function html_escape($str) {
   return htmlentities($str, ENT_QUOTES, 'utf-8');
}

In Python:

import cgi
 
# ...
 
def escape_html(value)
   return cgi.escape(value, True)

These types of attacks are called Cross-Site Scripting or XSS:
http://en.wikipedia.org/wiki/Cross-site_scripting

Good Luck :)

Comments (0)   Filed under: PHP, Python, Security, Web Development   Posted by: Codehead on January 21, 2009

Eclipse “Launch Failed. Binary Not Found.” and Netbeans

Last night I needed a C++ IDE right away; I had Eclipse for writing Python and knew that it had a C/C++ extension called CDT.

So I installed this CDT and I also had MingW and Cygwin installed but the only project I could compile was the sample Hello World project.

Whenever I made an empty project, Eclipse responded with “Launch Failed. Binary Not Found.”.

I read a few articles online but no luck and I didn’t want to spend a lot of time on it so I decided to try Netbeans.

After I installed Netbeans, BAM, it detected Cygwin and compiled everything right away!!!

That is what I wanted, I wanted an IDE that I can just make a CPP file and compile without any extra steps and Netbeans did that for me!

I’m not a C++ or Eclipse guru but I’m a normal user who is searching for simplicity and doesn’t have a lot of time to waste on things like this.

Comments (7)   Filed under: C Programming, IDEs, Programming   Posted by: Codehead on January 10, 2009

C++ needs a higher level standard library

I’m sure I’m not the only one who thought about this but I think one of the things that holds me back from using it more is the fact that C++ doesn’t have a higher level standard library.

A while back I was thinking of writing a thread library that works both on Windows (my desktop) and Linux (my server) and I gave up and used Python for the project.

The problem is that Python is not fast enough for parts of my project and I have to use either C or C++ but I will go with C++ because of it’s Map and Vector containers etc.

I’m not sure why they didn’t implement all the good stuff that are in Python standard library in C++ but I think it’s really time.

I also found this great set of tools that might be in the next C++ standard library:
http://www.boost.org/

This is exactly what C++ needs; I don’t see why cross platform libraries for threading, database access etc. shouldn’t be included in C++ standard library.

It has a lot of great features; objects, operator overloading, many kinds of containers, templates, namespaces and it’s very fast.

Comments (0)   Filed under: C Programming, Low Level, Programming   Posted by: Codehead on January 9, 2009
Older Posts »