You Are Here Home > Python

Python

Check To See If a File Exists

import os
 
if os.path.exists(path_to_file):
   # Do stuff
Check To See If a File Exists
Comments (0)   Filed under: Python   Posted by: Codehead on May 22, 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…

Missing from Python: kill-able threads and popens with timeouts…
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!'
Python win32api and pywintypes.error: (5, ‘TerminateProcess’, ‘Access is denied.’)
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);
Ungzip a string in Python – gzinflate in Python
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 ;)

Python “Unhandled exception in thread started by Error in sys.excepthook:”
Comments (0)   Filed under: Programming, Python   Posted by: Codehead on

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.

PML – A Python template engine
Comments (0)   Filed under: Programming, Python, Web Design, Web Development   Posted by: Codehead on March 31, 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/

A little Program For Monitoring Your Websites, With Alarm
Comments (1)   Filed under: Python, Server   Posted by: Codehead on February 20, 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 :)

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

Python ImportError: cannot import name X

This was strange, I had a bunch of classes in a file and was trying to import one of them from a file in a child folder.

The package looked like this:

/main_classes.py <– “import child” was in here way on top
/child/__init__.py <– For every file in this folder, import it
/child/some_file.py <– Import a class in main_classes.py *Error*

The reason was that I was doing “import child” way on top before implementing the class I was importing in some_file.py.

I moved the “import child” line from the top of the main_class.py to the constructor of the class I was implementing and it fixed the issue.

I hope this made sense :) (and I know it will to the person with this problem ;) )

Python ImportError: cannot import name X
Comments (5)   Filed under: Python   Posted by: Codehead on November 22, 2008

Decorating Python’s sys.stdout

Try this:

class stdoutflip:
 
    def __init__(self, sys):
        self.stdout = sys.stdout
        sys.stdout = self
 
    def write(self, txt):
        txt = list(txt)
        txt.reverse()
        self.stdout.write(''.join(txt))
 
 
class stdoutupper:
 
    def __init__(self, sys):
        self.stdout = sys.stdout
        sys.stdout = self
 
    def write(self, txt):
        self.stdout.write(txt.upper())

To test it do this:

out = stdoutupper(stdoutflip(sys))

Now try printing stuff:

print "Hello Python!"

:)

Decorating Python’s sys.stdout
Comments (0)   Filed under: Fun, Programming, Python   Posted by: Codehead on November 14, 2008
Older Posts »