Skip to content

Instantly share code, notes, and snippets.

@msaisushma
Last active August 29, 2015 13:58
Show Gist options
  • Save msaisushma/10374644 to your computer and use it in GitHub Desktop.
Save msaisushma/10374644 to your computer and use it in GitHub Desktop.
bin
include
lib

Requirements

  1. Python 2.7
  2. Virtualenv + Pip
  3. Pyramid

Steps to run the Server

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/
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)
@nandak522
Copy link

break the file into multiple files for better readability and maintainability.

  1. All configurations can go in config.py
  2. All templates can go in a templates directory
  3. The file which runs the server, can be named server.py
  4. All url/route definitions can be part of routes.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment