Advertisement
Guest User

Untitled

a guest
Nov 21st, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. ================= APP.PY ===============================
  2. #!/usr/bin/env python
  3. import imp
  4. import os
  5. import sys
  6.  
  7. try:
  8.    zvirtenv = os.path.join(os.environ['OPENSHIFT_PYTHON_DIR'],
  9.                            'virtenv', 'bin', 'activate_this.py')
  10.    execfile(zvirtenv, dict(__file__ = zvirtenv) )
  11. except IOError:
  12.    pass
  13.  
  14. def run_gevent_server(app, ip, port=8080):
  15.    from gevent.pywsgi import WSGIServer
  16.    WSGIServer((ip, port), app).serve_forever()
  17.  
  18.  
  19. def run_simple_httpd_server(app, ip, port=8080):
  20.    from wsgiref.simple_server import make_server
  21.    make_server(ip, port, app).serve_forever()
  22.  
  23.  
  24. #
  25. # IMPORTANT: Put any additional includes below this line.  If placed above this
  26. # line, it's possible required libraries won't be in your searchable path
  27. #
  28.  
  29.  
  30. #
  31. #  main():
  32. #
  33. if __name__ == '__main__':
  34.    #Setting path for external libs
  35.    sys.path.append('libs')
  36.    sys.path.append('wsgi')
  37.  
  38.    #
  39.    ip   = os.environ['OPENSHIFT_PYTHON_IP']
  40.    port = int(os.environ['OPENSHIFT_PYTHON_PORT'])
  41.    zapp = imp.load_source('application', 'wsgi/application')
  42.  
  43.    #  Use gevent if we have it, otherwise run a simple httpd server.
  44.    print 'Starting WSGIServer on %s:%d ... ' % (ip, port)
  45.    try:
  46.       run_gevent_server(zapp.application, ip, port)
  47.    except:
  48.       print 'gevent probably not installed - using default simple server ...'
  49.       run_simple_httpd_server(zapp.application, ip, port)
  50.  
  51.  
  52. ================= APPLICATION.PY ==========================
  53.  
  54. #!/usr/bin/python
  55. import os
  56. import sys
  57.  
  58. sys.path.insert(0, os.path.dirname(__file__) or '.')
  59. if os.path.exists(os.path.join(os.environ['OPENSHIFT_HOMEDIR'], "python-2.6")):
  60.     PY_DIR = os.path.join(os.environ['OPENSHIFT_HOMEDIR'], "python-2.6")
  61. else:
  62.     PY_DIR = os.path.join(os.environ['OPENSHIFT_HOMEDIR'], "python")
  63.  
  64.  
  65. virtenv = PY_DIR + '/virtenv/'
  66.  
  67. PY_CACHE = os.path.join(virtenv, 'lib', '2.6', 'site-packages')
  68.  
  69. os.environ['PYTHON_EGG_CACHE'] = os.path.join(PY_CACHE)
  70. virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
  71.  
  72. try:
  73.     execfile(virtualenv, dict(__file__=virtualenv))
  74. except IOError:
  75.     pass
  76.  
  77. from rssfinderflaskapp import app as application
  78.  
  79.  
  80. =============rssfinderflaskapp.py =========================
  81. from flask import Flask
  82. from flask import request
  83. from flask import Response
  84. import json
  85.  
  86. from myanalytics import myanalytics
  87.  
  88. app = Flask(__name__)
  89. #add this so that flask doesn't swallow error messages
  90. app.config['PROPAGATE_EXCEPTIONS'] = True
  91. manal = myanalytics()
  92.  
  93. def getArgs(rq):
  94.     lang  = rq.args.get('l', None)
  95.     query = rq.args.get('q', None)
  96.     force = rq.args.get('f', None)
  97.  
  98.     if not lang or not query:
  99.         raise Exception
  100.  
  101.     return lang, query, force
  102.  
  103. @app.errorhandler(404)
  104. def page_not_found(error):
  105.     ip = request.remote_addr
  106.     manal.track("ERROR", {"ip": ip, "type" : 404, "msg": request.path})
  107.     return 'Hey man, this page does not exist', 404
  108.  
  109. @app.route("/healthy")
  110. def health():
  111.     return "OK", 200
  112.  
  113. if __name__ == "__main__":
  114.     debug_mode = True
  115.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement