Search This Blog


Thursday, November 12, 2015

Flask in python 3 under windows

If you are trying to run flask under Python 3 on Windows and you get the following stack trace:

Traceback (most recent call last):
  File "", line 4, in
  File "C:\Users\Peter Ellis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\", line 772, in run
    run_simple(host, port, self, **options)
  File "C:\Users\Peter Ellis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\werkzeug\", line 666, in run_simple
    os.set_inheritable(s.fileno(), True)
OSError: [Errno 9] Bad file descriptor

The issue appears to be that on *nix you can use os.set_inheritable on sockets but on Windows you need os.set_handle_inheritable. (See also this patch Unfortunately I don't have time right now to write the necessary tests as the werkzeug master tests error on windows (Python crashes) so the solution is either:

  1. Patch werkzeug locally
  2. Set WERKZEUG_RUN_MAIN to 'true' (bypasses the offending code

Tuesday, February 12, 2013

Shared libraries in JBoss 7 using eclipse

Posting this because it took a bunch of trail and error to figure out.

If you need to add non-maven external libraries to a an EAR project created using the JBoss tools you probably want to put them in ear-project\src\main\application\lib

Yes you can probably use maven and have a local repository as well, this has the advantage or reducing the number of moving parts and working more easily with a CI setup that assumes ant

Friday, January 6, 2012

Eclipse related fail

I recently wasted a bunch of time on this error so am putting it here to try and prevent it happening to anyone else.

When Launching Tomcat from eclipse I kept getting:
java.lang.NoClassDefFoundError: org/apache/log4j/Logger

Turns out Eclipse decided to only half-deploy the servlet. The easy solution was to go to workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps and delete the folder.

After than removing the servlet, restarting, then adding it again fixed the issue.

Monday, November 29, 2010

Screenscrape a website using cookies

Snippet of code to script a site that uses login cookies. Contains a couple of common helpers

import cookielib
import urllib2
from lxml.html import parse
from urllib import urlencode
import cookielib
import urllib2
import urlparse

txheaders = {'User-agent': 'Pony automation bot'}
Request = urllib2.Request

def login_required(func):
"""Gives a sensible error message if func is called without logging in"""
def wrapped_func(*args, **kwargs):
if args[0].logged_in or args[0].__logged_in:
return func(*args, **kwargs)
return (False, "Not logged in")
return wrapped_func

class Site(object):
def __init__(self, base):
self.base = base
self.__logged_in = False
self.cj = cookielib.LWPCookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))

def __request_body(self, theurl, txdata):
req = Request(theurl, txdata, txheaders)
return parse(

Wednesday, October 13, 2010

Generate a list of files under a directory with full paths

Handy more often than one might think.

from itertools import chain
import os

list(chain(*[map(lambda y: os.path.join(x[0], y), x[2]) for x in os.walk("tmp")])))

Tuesday, June 1, 2010

Boilerplate code for a python-tk GUI

This is the most common boilerplate for python-tk guis that I wind up writing. Posted here for ease of copy-paste because typing this out again was irritating me

from Tkinter import *
import threading, Queue, sys, os
from optparse import OptionParser
from ConfigParser import SafeConfigParser

__version__ = "0.0.1"

class GUI(object):
def __init__(self, root, queue, shutdown, config):
self.root = root
self.config = config
# Widget creation goes here

def process_events(self):

class ThreadedClient(object):
def __init__(self, root, conf):
self.root = root
self.config = SafeConfigParser()
self.config_file = os.path.abspath(conf)
self.queue = Queue.Queue()
self.gui = GUI(self.root, self.queue, self.shutdown, self.config)
self.running = True
self.work_thread = threading.Thread(target=self.secure_thread)

def periodic_call(self):
self.root.after(200, self.periodic_call)
if not self.running:

def secure_thread(self):
while self.running:
msg = self.queue.get(0)
except Queue.Empty:
# App work goes here to kepe the gui responsive

def shutdown(self):
self.running = False

if __name__ == "__main__":
parser = OptionParser(usage="%prog [-c]", version=__version__)
parser.add_option("-c", "--config", metavar="FILE",
help="Read config from FILE", default="")
(options, args) = parser.parse_args()
root = Tk()
if args:
app = ThreadedClient(root, options.config, args[0])
app = ThreadedClient(root, options.config)