Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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:
- in the front:
- POST http://localhost:5000/profit 500 (INTERNAL SERVER ERROR)
- Test.vue?0465:55 this is the error from the python server Error: Request failed with status code 500
- at createError (eval at <anonymous> (app.js:801), <anonymous>:16:15)
- at settle (eval at <anonymous> (app.js:882), <anonymous>:18:12)
- at XMLHttpRequest.handleLoad (eval at <anonymous> (app.js:780), <anonymous>:77:7)
- in the back:
- /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.
- 'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
- * Serving Flask app "main"
- * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
- 127.0.0.1 - - [12/Jun/2017 17:28:26] "OPTIONS /profit HTTP/1.1" 200 -
- [2017-06-12 17:28:26,981] ERROR in app: Exception on /profit [POST]
- Traceback (most recent call last):
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
- response = self.full_dispatch_request()
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
- rv = self.handle_user_exception(e)
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
- return cors_after_request(app.make_response(f(*args, **kwargs)))
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
- reraise(exc_type, exc_value, tb)
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
- rv = self.dispatch_request()
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
- return self.view_functions[rule.endpoint](**req.view_args)
- File "/home/patientplatypus/vuespring/backend_PYTHON3/main.py", line 48, in profits
- conn = psycopg2.connect(database = "money", user = "patientplatypus", password = "Fvnjty0b", host = "https://127.0.0.1", port = "5000")
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
- conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
- OperationalError: could not translate host name "https://127.0.0.1" to address: Name or service not known
- 127.0.0.1 - - [12/Jun/2017 17:28:26] "POST /profit HTTP/1.1" 500 -
- 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.......
- Here is the code I am working with at the moment. Thank you for the help.
- from flask import Flask, request, jsonify
- from flask_sqlalchemy import SQLAlchemy
- from flask_cors import CORS, cross_origin
- import sqlite3
- import psycopg2
- import os
- from os.path import join, dirname
- from dotenv import load_dotenv
- from models.shared import db
- from models.profits import Profit
- #from models.race_unit import RaceUnit
- dotenv_path = join(dirname(__file__), '.env')
- load_dotenv(dotenv_path)
- app = Flask(__name__)
- CORS(app)
- 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'))
- db.app = app
- db.init_app(app)
- # Create tables if they don't already exist
- db.create_all()
- @app.route('/')
- def index():
- return 'At Index of Python Backend'
- @app.route('/profit', methods=['POST','GET'])
- def profits():
- if request.method=='POST':
- conn = psycopg2.connect(database = "money", user = "patientplatypus", password = "Fvnjty0b", host = "https://127.0.0.1", port = "5000")
- cur = conn.cursor()
- cur.execute("INSERT INTO PROFITS (ID,NAME,DESCRIPTION) VALUES (request.json['id'], request.json['name'], request.json['description']");
- conn.commit()
- print "Records created successfully";
- conn.close()
- if request.method=='GET':
- profits = [i.serialize for i in Profit.query.all()]
- return jsonify(profits)
- if __name__ == '__main__':
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement