Advertisement
Guest User

Untitled

a guest
May 7th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. import os
  2. from flask import Flask, request
  3. import hashlib
  4. import psycopg2
  5. from psycopg2.extras import RealDictCursor
  6. import traceback
  7. import urllib.parse
  8. import pprint
  9.  
  10. app = Flask(__name__) #create an instance of the Flask library
  11. urllib.parse.uses_netloc.append("postgres")
  12. url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
  13. dbConn = psycopg2.connect( database=url.path[1:], user=url.username, password=url.password, host=url.hostname, port=url.port)
  14. dbCur = dbConn.cursor(cursor_factory=RealDictCursor)
  15.  
  16.  
  17. @app.route('/list') #whenever this webserver is called with <hostname:port>/hello then this section is called
  18. def list(): #The subroutine name that handles the call
  19. output = 'Check status:'
  20. rows = []
  21. try:
  22. dbCur.execute("select * from webcheckerdb" ) #Get all records from database
  23. rows = dbCur.fetchall()
  24. for webrecord in rows: #Loop through each record in database
  25. output = output + '<BR> ' + pprint.pformat(webrecord)
  26. except:
  27. output = "error during select: " + str(traceback.format_exc())
  28.  
  29. return output #Whatever is returned from this subroutine is what is returned to the requester and is shown on the browser page
  30.  
  31.  
  32. @app.route('/hello') #whenever this webserver is called with <hostname:port>/hello then this section is called
  33. def hello(): #The subroutine name that handles the call
  34. output = 'Hello World'
  35. return output #Whatever is returned from this subroutine is what is returned to the requester and is shown on the browser page
  36.  
  37. if __name__ == '__main__':
  38. port = int(os.environ.get('PORT', 5000)) #The port to be listening to — hence, the URL must be <hostname>:<port>/ inorder to send the request to this program
  39. app.run(host='0.0.0.0', port=port) #Start listening
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement