replaced php with python version of shipping app

This commit is contained in:
Dan Buch 2010-02-17 22:46:18 -05:00
parent aede938583
commit 102d2b87f0
4 changed files with 77 additions and 53 deletions

View File

@ -1,11 +0,0 @@
<?php
// shipping.php contains our get_shipping_options($zipcode, $pounds) function.
// It returns an array mapping service name to price in US dollars.
require ('shipping.php');
$options = get_shipping_options($_REQUEST[zipcode], $_REQUEST[pounds]);
foreach ($options as $service => $price) {
$result[] = "$service: $price USD";
}
print implode("\n", $result);
?>

View File

@ -1,12 +0,0 @@
<?php
// Returns an array of made-up shipping options.
function get_shipping_options($zipcode, $pounds) {
$baseCost = round($zipcode / 10000) + ($pounds * 5);
$options = array( "Next Day" => $baseCost * 4,
"Two Day Air" => $baseCost * 2,
"Saver Ground" => $baseCost);
return $options;
}
?>

View File

@ -1,15 +0,0 @@
<?php
// shipping.php contains our get_shipping_options($zipcode, $pounds) function.
// It returns an array mapping service name to price in US dollars.
require('shipping.php');
$options = get_shipping_options($_REQUEST["zipcode"], $_REQUEST["pounds"]);
$results[] = "<options>";
foreach ($options as $service => $price) {
$results[] = "<option><service>$service</service><price>$price</price></option>";
}
$results[] = "</options>";
print implode("\n", $results);
?>

View File

@ -1,32 +1,94 @@
from __future__ import print_function
import sys
import cgi
from wsgiref.simple_server import make_server
def main(sysargs=sys.argv[:]):
app = ShippingApp()
port = 18080
server = make_server('0.0.0.0', port, app)
print('serving {0.__class__.__name__} on port {1}'.format(app, port))
server = make_server('0.0.0.0', port, shipping_app)
print('serving {0.__name__} on port {1}'.format(shipping_app, port))
server.serve_forever()
return 0
class ShippingApp(object):
def shipping_app(environ, start_response):
path_info = environ.get('PATH_INFO', '').strip('/')
def __call__(self, environ, start_response):
start_response('200 OK', [('content-type', 'text/plain')])
return ['oker doke']
handler = {
'text': plaintext_handler,
'xml': xml_handler,
}.get(path_info)
@classmethod
def get_shipping_options(cls, zipcode, pounds):
base_cost = (float(zipcode) / 10000.0) + (pounds * 5.0)
return {
"Next Day": int(base_cost * 4),
"Two Day Air": int(base_cost * 2),
"Saver Ground": int(base_cost)
}
if handler:
try:
return handler(environ, start_response)
except Exception:
start_response('500 Internal Server Error', [
('content-type', 'text/plain'),
])
return ['OUCH']
else:
ret = 'nothin at {0!r}'.format(path_info)
start_response('404 Not Found', [
('content-type', 'text/plain'),
('content-length', str(len(ret)))
])
return [ret]
def get_params(environ):
params = cgi.parse(fp=environ.get('wsgi.input'), environ=environ)
for key, value in params.iteritems():
if len(value) == 1:
params[key] = value[0]
return params
def xml_handler(environ, start_response):
params = get_params(environ)
zipcode = int(params.get('zipcode', 0))
pounds = int(params.get('pounds', 0))
ret = ['<options>']
for service, price in get_shipping_options(zipcode, pounds).iteritems():
ret.append('<option><service>{service}</service>'
'<price>{price}</price></option>'.format(**locals()))
ret.append('</options>')
body = '\n'.join(ret)
start_response('200 OK', [
('content-type', 'text/xml'),
('content-length', str(len(body)))
])
return [body]
def plaintext_handler(environ, start_response):
params = get_params(environ)
zipcode = int(params.get('zipcode', 0))
pounds = int(params.get('pounds', 0))
ret = []
for service, price in get_shipping_options(zipcode, pounds).iteritems():
ret.append('{service}: {price} USD'.format(**locals()))
body = '\n'.join(ret)
start_response('200 OK', [
('content-type', 'text/plain'),
('content-length', str(len(body)))
])
return [body]
def get_shipping_options(zipcode, pounds):
base_cost = (float(zipcode) / 10000.0) + (pounds * 5.0)
return {
"Next Day": int(base_cost * 4),
"Two Day Air": int(base_cost * 2),
"Saver Ground": int(base_cost)
}
if __name__ == '__main__':