replaced php with python version of shipping app
This commit is contained in:
parent
aede938583
commit
102d2b87f0
@ -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);
|
|
||||||
?>
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
@ -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);
|
|
||||||
?>
|
|
@ -1,32 +1,94 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import cgi
|
||||||
from wsgiref.simple_server import make_server
|
from wsgiref.simple_server import make_server
|
||||||
|
|
||||||
|
|
||||||
def main(sysargs=sys.argv[:]):
|
def main(sysargs=sys.argv[:]):
|
||||||
app = ShippingApp()
|
|
||||||
port = 18080
|
port = 18080
|
||||||
server = make_server('0.0.0.0', port, app)
|
server = make_server('0.0.0.0', port, shipping_app)
|
||||||
print('serving {0.__class__.__name__} on port {1}'.format(app, port))
|
print('serving {0.__name__} on port {1}'.format(shipping_app, port))
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
class ShippingApp(object):
|
def shipping_app(environ, start_response):
|
||||||
|
path_info = environ.get('PATH_INFO', '').strip('/')
|
||||||
|
|
||||||
def __call__(self, environ, start_response):
|
handler = {
|
||||||
start_response('200 OK', [('content-type', 'text/plain')])
|
'text': plaintext_handler,
|
||||||
return ['oker doke']
|
'xml': xml_handler,
|
||||||
|
}.get(path_info)
|
||||||
|
|
||||||
@classmethod
|
if handler:
|
||||||
def get_shipping_options(cls, zipcode, pounds):
|
try:
|
||||||
base_cost = (float(zipcode) / 10000.0) + (pounds * 5.0)
|
return handler(environ, start_response)
|
||||||
return {
|
except Exception:
|
||||||
"Next Day": int(base_cost * 4),
|
start_response('500 Internal Server Error', [
|
||||||
"Two Day Air": int(base_cost * 2),
|
('content-type', 'text/plain'),
|
||||||
"Saver Ground": int(base_cost)
|
])
|
||||||
}
|
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__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user