Last active
August 29, 2015 13:59
-
-
Save jpstroop/10996832 to your computer and use it in GitHub Desktop.
PrincetonPy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from PIL.ImageFile import Parser | |
from os import makedirs, path, unlink | |
import subprocess | |
import sys | |
KDU_EXPAND='/usr/local/bin/kdu_expand' | |
LIB_KDU='/usr/local/lib/libkdu_v72R.so' | |
TMP='/tmp' | |
INPUT_JP2='/home/jstroop/workspace/loris/tests/img/01/02/0001.jp2' | |
OUT_JPG='/tmp/test.jpg' | |
### cmds, etc. | |
pipe_fp = '%s/mypipe.bmp' % (TMP,) | |
kdu_cmd = '%s -i %s -o %s' % (KDU_EXPAND, INPUT_JP2, pipe_fp) | |
mkfifo_cmd = '/usr/bin/mkfifo %s' % (pipe_fp,) | |
rmfifo_cmd = '/bin/rm %s' % (pipe_fp,) | |
# make a named pipe | |
mkfifo_resp = subprocess.check_call(mkfifo_cmd, shell=True) | |
if mkfifo_resp == 0: | |
print 'mkfifo OK' | |
# write kdu_expand's output to the named pipe | |
kdu_expand_proc = subprocess.Popen(kdu_cmd, shell=True, | |
bufsize=-1, stderr=subprocess.PIPE, stdout=subprocess.PIPE, | |
env={ 'LD_LIBRARY_PATH' : KDU_EXPAND }) | |
# open the named pipe and parse the stream | |
with open(pipe_fp, 'rb') as f: | |
p = Parser() | |
while True: | |
s = f.read(1024) | |
if not s: | |
break | |
p.feed(s) | |
im = p.close() | |
# finish kdu | |
kdu_exit = kdu_expand_proc.wait() | |
if kdu_exit != 0: | |
map(sys.stderr.write, kdu_expand_proc.stderr) | |
else: | |
# if kdu was successful, save to a jpg | |
map(sys.stdout.write, kdu_expand_proc.stdout) | |
im.save(OUT_JPG, quality=90) | |
# remove the named pipe | |
rmfifo_resp = subprocess.check_call(rmfifo_cmd, shell=True) | |
if rmfifo_resp == 0: | |
print 'rm fifo OK' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wsgiref.simple_server import make_server | |
from wsgiref.util import request_uri | |
from wsgiref.headers import Headers | |
def report(environ, key): | |
return 'The %s is %s' % (' '.join(key.lower().split('_')), environ[key],) | |
def application(environ, start_response): | |
info = [] | |
info.append('URI was %s' % (request_uri(environ),)) # helper | |
info.append('The scheme is %s' % (environ['wsgi.url_scheme'],)) | |
interesting_keys = ['SERVER_NAME','SERVER_PORT','SCRIPT_NAME','PATH_INFO','QUERY_STRING'] | |
[ info.append(report(environ, key)) for key in interesting_keys ] | |
body = '\n'.join(info) | |
headers = [ | |
('Content-Type', 'text/plain'), | |
('Content-Length', str(len(body))) | |
] | |
start_response('200 OK', headers) | |
return [body] # must be an iterable! | |
# host # port # callable | |
server = make_server('localhost', 5007, application) | |
server.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wsgiref.simple_server import make_server | |
def application(environ, start_response): | |
start_response('200 OK', []) | |
return ['Hello World!'] | |
# host # port # callable | |
server = make_server('localhost', 5007, application) | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment