Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. Software Laboratory 5
  5. SOA service demo
  6.  
  7. This sample service was created for the purposes of demonstrating some
  8. of the functionality you can achieve by combining the power of three
  9. Python libraries: cx_Oracle, Flask, and Requests.
  10.  
  11. It does not intend to be perfect Python code -- in some places, perfection
  12. was traded for simplicity, some of these are marked in comments.
  13.  
  14. This comment is a so-called docstring, all Python modules and
  15. functions/methods should have one. Three " or ' characters make it
  16. possible for multiline strings, and interactive Python environments
  17. display these "docstrings" (basically header comments) for users of
  18. your code. Further info: http://www.python.org/dev/peps/pep-0257/
  19. """
  20.  
  21. from flask import Flask, jsonify, abort, request
  22. from datetime import datetime
  23. import json
  24. import cx_Oracle
  25. import requests
  26.  
  27. app = Flask(__name__)
  28.  
  29. @app.route('/cegek.json')
  30. def list_people():
  31. """Lists the first 50 persons in the database"""
  32. conn = get_db()
  33. try:
  34. cur = conn.cursor()
  35. try:
  36. cur.execute('SELECT id, nev, kapcsolattarto FROM fof1so.cegek')
  37. results = []
  38. for id, nev, kapcsolattarto in cur:
  39. results.append({'id': id, 'nev': nev, 'kapcsolattarto': kapcsolattarto})
  40. return jsonify(cegek=results)
  41. finally:
  42. cur.close()
  43. finally:
  44. conn.close()
  45.  
  46. @app.route('/cegek/<id>.json')
  47. def show_person(id):
  48. """Shows the details of a single person by szemelyi_szam"""
  49. conn = get_db()
  50. try:
  51. cur = conn.cursor()
  52. try:
  53. cur.execute('SELECT id,nev,bankszamla,kapcsolattarto FROM fof1so.cegek WHERE id = :cegid1',
  54. cegid1=id)
  55. result = cur.fetchone()
  56. if result is None:
  57. abort(404)
  58. else:
  59. id,nev,bankszamla,kapcsolattarto = result
  60. res = requests.get("https://seeks.hsbp.org",params={
  61. 'format': 'json',
  62. 'q': result[0]
  63. })
  64.  
  65. print(res.url)
  66. print(res.text)
  67. print(res.json)
  68. results.append({'id': id, 'nev': nev, 'bankszamla':bankszamla, 'kapcsolattarto': kapcsolattarto})
  69. """return jsonify(cegek=result)"""
  70. return jsonify(res[0])
  71. except: pass
  72. finally:
  73. cur.close()
  74. finally:
  75. conn.close()
  76.  
  77. @app.route('/datetest.json')
  78. def date_test():
  79. conn = get_db()
  80. try:
  81. cur = conn.cursor()
  82. try:
  83. # http://www.oracle.com/technetwork/articles/dsl/prez-python-timesanddates-093014.html
  84. # https://docs.python.org/2/library/datetime.html
  85. # its casted automatically to datetime
  86. cur.execute('SELECT datum, usd FROM oktatas.mnb_deviza where id < 10')
  87. results = []
  88. for datum, usd in cur:
  89. results.append({'datum': datum, 'datum_iso' : datum.isoformat(), 'usd': usd})
  90. return jsonify(arfolyamok=results)
  91. finally:
  92. cur.close()
  93. finally:
  94. conn.close()
  95.  
  96.  
  97. @app.route('/verbtest.json', methods=['PUT', 'POST'])
  98. def verb_test():
  99. """Lets you test HTTP verbs different from GET, expects and returns data in JSON format"""
  100. # it also shows you how to access the method used and the decoded JSON data
  101. return jsonify(method=request.method, data=request.json, url=request.url)
  102.  
  103.  
  104. def get_db():
  105. """Connects to the RDBMS and returns a connection object"""
  106. # when used with a `file` object, `with` ensures it gets closed
  107. with file('config.json') as config_file:
  108. config = json.load(config_file)
  109. return cx_Oracle.connect(config['user'], config['pass'], config['host'])
  110.  
  111.  
  112. if __name__ == "__main__":
  113. import os
  114. os.environ['NLS_LANG'] = '.UTF8'
  115. app.run(debug=True, port=os.getuid() + 10000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement