| 1 |
# -*-python-*- |
|---|
| 2 |
|
|---|
| 3 |
__package__ = "wscompose/client.py" |
|---|
| 4 |
__version__ = "1.0" |
|---|
| 5 |
__author__ = "Aaron Straup Cope" |
|---|
| 6 |
__url__ = "http://www.aaronland.info/python/wscompose" |
|---|
| 7 |
__date__ = "$Date: 2008/01/04 06:23:46 $" |
|---|
| 8 |
__copyright__ = "Copyright (c) 2007-2008 Aaron Straup Cope. BSD license : http://www.modestmaps.com/license.txt" |
|---|
| 9 |
|
|---|
| 10 |
import urllib |
|---|
| 11 |
import httplib |
|---|
| 12 |
import Image |
|---|
| 13 |
import StringIO |
|---|
| 14 |
import string |
|---|
| 15 |
import re |
|---|
| 16 |
|
|---|
| 17 |
class httpclient : |
|---|
| 18 |
|
|---|
| 19 |
def __init__ (self, host='127.0.0.1', port=9999) : |
|---|
| 20 |
self.__host__ = host |
|---|
| 21 |
self.__port__ = port |
|---|
| 22 |
|
|---|
| 23 |
# ########################################################## |
|---|
| 24 |
|
|---|
| 25 |
def fetch (self, args) : |
|---|
| 26 |
|
|---|
| 27 |
img = None |
|---|
| 28 |
meta = {} |
|---|
| 29 |
|
|---|
| 30 |
params = urllib.urlencode(args) |
|---|
| 31 |
url = "%s:%s" % (self.__host__, self.__port__) |
|---|
| 32 |
endpoint = "/?%s" % params |
|---|
| 33 |
|
|---|
| 34 |
# maybe always POST or at least add it as an option... |
|---|
| 35 |
|
|---|
| 36 |
try : |
|---|
| 37 |
conn = httplib.HTTPConnection(url) |
|---|
| 38 |
conn.request("GET", endpoint) |
|---|
| 39 |
res = conn.getresponse() |
|---|
| 40 |
except Exception, e : |
|---|
| 41 |
raise e |
|---|
| 42 |
|
|---|
| 43 |
if res.status != 200 : |
|---|
| 44 |
|
|---|
| 45 |
if res.status == 500 : |
|---|
| 46 |
errmsg = "(%s) %s" % (res.getheader('x-errorcode'), res.getheader('x-errormessage')) |
|---|
| 47 |
raise Exception, errmsg |
|---|
| 48 |
else : |
|---|
| 49 |
raise Exception, res.message |
|---|
| 50 |
|
|---|
| 51 |
# fu.PYTHON ... |
|---|
| 52 |
re_xheader = re.compile(r"^x-wscompose-", re.IGNORECASE) |
|---|
| 53 |
|
|---|
| 54 |
for key, value in res.getheaders() : |
|---|
| 55 |
|
|---|
| 56 |
if re_xheader.match(key) : |
|---|
| 57 |
|
|---|
| 58 |
parts = key.split("-") |
|---|
| 59 |
parts = map(string.lower, parts) |
|---|
| 60 |
|
|---|
| 61 |
major = parts[2] |
|---|
| 62 |
minor = parts[3] |
|---|
| 63 |
|
|---|
| 64 |
if not meta.has_key(major) : |
|---|
| 65 |
meta[major] = {} |
|---|
| 66 |
|
|---|
| 67 |
meta[major][minor] = value |
|---|
| 68 |
|
|---|
| 69 |
data = res.read() |
|---|
| 70 |
conn.close() |
|---|
| 71 |
|
|---|
| 72 |
try : |
|---|
| 73 |
img = Image.open(StringIO.StringIO(data)) |
|---|
| 74 |
except Exception, e : |
|---|
| 75 |
raise e |
|---|
| 76 |
|
|---|
| 77 |
return (img, meta) |
|---|
| 78 |
|
|---|
| 79 |
# ########################################################## |
|---|