- Python 2.7
- Virtualenv + Pip
- Pyramid
git clone <repo_url>
cd <repo>
virtualenv .
source bin/activate
pip install Pyramid
python server.py
# Navigate to http://localhost:8080/ or http://localhost:8080/bus/
bin | |
include | |
lib |
from wsgiref.simple_server import make_server | |
from pyramid.config import Configurator | |
from pyramid.response import Response | |
DOMAIN = '0.0.0.0' | |
PORT = 8080 | |
def bus_routes(request): | |
# TODO: Docstrings | |
return Response('<form action="/response/" method="GET">Enter the bus no<input ' | |
'type="text" name="data" id="data"/><input type="submit">' | |
'</form>') | |
def bus_info(request): | |
""" | |
This method takes `data`/`bus number` as input and returns the Bus Route | |
as String if its a valid bus number. Else it will return "Invalid" | |
""" | |
route1 = ('Corporation', 'Chamrajpet', 'NR colony', 'Chikkallasandra') | |
route2 = ('Netkallappa circle', 'Chamrajpet', 'Navrang', 'Sujatha') | |
route3 = ('Gandhi Bazaar', 'Jayanagar 4th block', 'East End', 'BTM Layout') | |
route4 = ('Lalbagh', 'Nanda Theatre', 'Sangam Circle', 'Banashankari') | |
"""to fetch the data from the text box.""" | |
data = request.GET['data'] | |
if data == '210N': | |
# TODO: Move this to a template | |
return Response('<b>'"Starting"'</b>'+':'+"Majestic"+" "+'<br>''<b>'"Ending"'</b>'+':'+"Uttarahalli"+" "+'<br>''<b>' "Stops in the route: " '</b>'+'<br>' ' '.join(str(place) for place in route1)) | |
elif data == '77B': | |
# TODO: Move this to a template | |
return Response('<b>'"Starting"'</b>'+':'+"NR colony"+" "+'<br>''<b>'"Ending"'</b>'+':'+"Mahalakshmi Layout"+" "+'<br>''<b>' "Stops in the route: " '</b>'+'<br>' ' '.join(str(place) for place in route2)) | |
elif data == '3E': | |
# TODO: Move this to a template | |
return Response('<b>'"Starting"'</b>'+':'+"Banashankari 3rd stage"+" "+'<br>''<b>'"Ending"'</b>'+':'+"BTM layout"+" "+'<br>''<b>' "Stops in the route: " '</b>'+'<br>' ' '.join(str(place) for place in route3)) | |
elif data == '12': | |
# TODO: Move this to a template | |
return Response('<b>'"Starting"'</b>'+':'+"KBS"+" "+'<br>''<b>'"Ending"'</b>'+':'+"Banashankari"+" "+'<br>''<b>' "Stops in the route: " '</b>'+'<br>' ' '.join(str(place) for place in route4)) | |
# TODO: Move this to a template | |
return Response("Invalid") | |
def create_configuration(): | |
""" | |
Prepares configuration and adds routes to it | |
""" | |
config = Configurator() | |
config.add_route('home', '/') | |
# TODO: check @view_config decorator | |
config.add_view(bus_routes, route_name='home') | |
config.add_route('bus', '/bus/') | |
config.add_view(bus_routes, route_name='bus') | |
config.add_route('bus_info', '/response') | |
config.add_view(bus_info, route_name='bus_info') | |
return config | |
def start_server(config): | |
""" | |
Runs a pyramid server at the configured domain:port | |
""" | |
app = config.make_wsgi_app() | |
server = make_server(DOMAIN, PORT, app) | |
print 'Server Started listening on %s:%s' % (DOMAIN, PORT) | |
server.serve_forever() | |
if __name__ == '__main__': | |
config = create_configuration() | |
start_server(config) |
Its missing the homepage. Package installation steps. Help text for the /bus/
url. Why routeX
is global ?
Also add docstrings, tests and you can drop (
and )
in the if/elif conditions. Last else
statement is redundant. If its a non-changing sequence, you should be defining that as a tuple
(Check routeX variables). Use Templates for the form and response.
break the file into multiple files for better readability and maintainability.
config.py
templates
directoryserver.py
routes.py
👍