Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.69 KB | None | 0 0
  1. Ii am trying to get a rest POST for python up and running and continue to run into difficulties. I am getting the following error:
  2.  
  3. in the front:
  4.  
  5. POST http://localhost:5000/profit 500 (INTERNAL SERVER ERROR)
  6. Test.vue?0465:55 this is the error from the python server  Error: Request failed with status code 500
  7.     at createError (eval at <anonymous> (app.js:801), <anonymous>:16:15)
  8.     at settle (eval at <anonymous> (app.js:882), <anonymous>:18:12)
  9.     at XMLHttpRequest.handleLoad (eval at <anonymous> (app.js:780), <anonymous>:77:7)
  10.  
  11. in the back:
  12.  
  13. /home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  14.   'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
  15.  * Serving Flask app "main"
  16.  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  17. 127.0.0.1 - - [12/Jun/2017 17:28:26] "OPTIONS /profit HTTP/1.1" 200 -
  18. [2017-06-12 17:28:26,981] ERROR in app: Exception on /profit [POST]
  19. Traceback (most recent call last):
  20.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
  21.     response = self.full_dispatch_request()
  22.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
  23.     rv = self.handle_user_exception(e)
  24.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
  25.     return cors_after_request(app.make_response(f(*args, **kwargs)))
  26.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
  27.     reraise(exc_type, exc_value, tb)
  28.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
  29.     rv = self.dispatch_request()
  30.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
  31.     return self.view_functions[rule.endpoint](**req.view_args)
  32.   File "/home/patientplatypus/vuespring/backend_PYTHON3/main.py", line 48, in profits
  33.     conn = psycopg2.connect(database = "money", user = "patientplatypus", password = "Fvnjty0b", host = "https://127.0.0.1", port = "5000")
  34.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
  35.     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  36. OperationalError: could not translate host name "https://127.0.0.1" to address: Name or service not known
  37.  
  38. 127.0.0.1 - - [12/Jun/2017 17:28:26] "POST /profit HTTP/1.1" 500 -
  39.  
  40.  
  41.  
  42.  
  43. So somehow it does not like my psycopg2 connect pointing to 127.0.0.1, but I don't know what else to do. The psycopg2 database is hosted on the backend, and that is the address of the backend.....so.......
  44.  
  45.  
  46. Here is the code I am working with at the moment. Thank you for the help.
  47.  
  48.  
  49.  
  50.  
  51.  
  52. from flask import Flask, request, jsonify
  53. from flask_sqlalchemy import SQLAlchemy
  54. from flask_cors import CORS, cross_origin
  55. import sqlite3
  56. import psycopg2
  57.  
  58. import os
  59. from os.path import join, dirname
  60. from dotenv import load_dotenv
  61.  
  62. from models.shared import db
  63. from models.profits import Profit
  64. #from models.race_unit import RaceUnit
  65.  
  66. dotenv_path = join(dirname(__file__), '.env')
  67. load_dotenv(dotenv_path)
  68.  
  69. app = Flask(__name__)
  70. CORS(app)
  71. app.config['SQLALCHEMY_DATABASE_URI'] = '%s://%s:%s@%s/%s' % (os.environ.get('DB_DRIVER'), os.environ.get('DB_USER'), os.environ.get('DB_PASSWORD'), os.environ.get('DB_HOST'), os.environ.get('DB_NAME'))
  72.  
  73. db.app = app
  74. db.init_app(app)
  75. # Create tables if they don't already exist
  76. db.create_all()
  77.  
  78. @app.route('/')
  79. def index():
  80.     return 'At Index of Python Backend'
  81.  
  82. @app.route('/profit', methods=['POST','GET'])
  83. def profits():
  84.     if request.method=='POST':
  85.         conn = psycopg2.connect(database = "money", user = "patientplatypus", password = "Fvnjty0b", host = "https://127.0.0.1", port = "5000")
  86.         cur = conn.cursor()
  87.         cur.execute("INSERT INTO PROFITS (ID,NAME,DESCRIPTION) VALUES (request.json['id'], request.json['name'], request.json['description']");
  88.  
  89.         conn.commit()
  90.         print "Records created successfully";
  91.         conn.close()
  92.     if request.method=='GET':
  93.         profits = [i.serialize for i in Profit.query.all()]
  94.         return jsonify(profits)
  95. if __name__ == '__main__':
  96.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement