from API to ip - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: from API to ip (/thread-14546.html) Pages:
1
2
|
from API to ip - whatl0ol - Dec-05-2018 Hello, I have python webserver runnng flask. I need python script IP requests for json, go to API request for json receive it and send to IP I dont know how to write thansk RE: from API to ip - whatl0ol - Dec-05-2018 (Dec-05-2018, 09:27 PM)Larz60+ Wrote: Repost in jobs, showing what you offer for compensation.thanks for answer i wrote simple script which requests data to api its simply printing, now i want to filter when user asks for data request for api and send it to user response = requests.get("API",auth=('user', 'pass')) data = response.json() print(data) if else statement but how get when user asks for json RE: from API to ip - nilamo - Dec-05-2018 If you're using flask, you can just return the data. Printing only prints it to the console on the server side, it never makes it to the client. If you didn't already have json, and instead had something like a dict, Flask has a jsonify function to help. The docs for that function will probably help, as they show example usage: http://flask.pocoo.org/docs/1.0/api/#flask.json.jsonify RE: from API to ip - whatl0ol - Dec-05-2018 (Dec-05-2018, 10:09 PM)nilamo Wrote: If you're using flask, you can just return the data. Printing only prints it to the console on the server side, it never makes it to the client. If you didn't already have json, and instead had something like a dict, Flask has a jsonify function to help. The docs for that function will probably help, as they show example usage: http://flask.pocoo.org/docs/1.0/api/#flask.json.jsonify yes i know returning but i want to send it to user, how it can be done ? RE: from API to ip - nilamo - Dec-05-2018 If you return it from a flask-routed function, it's sent to the user. If that's not happening for you, please share some code. RE: from API to ip - whatl0ol - Dec-05-2018 from flask import Flask, jsonify app = Flask(__name__) @app.route("/") def main(): response = requests.get("API",auth=('user', 'pass')) data = response.json() return jsonify() if __name__ == ("__main__"): app.run(debug = True, host = "0.0.0.0", port =80) how send user post request? (Dec-05-2018, 10:13 PM)nilamo Wrote: If you return it from a flask-routed function, it's sent to the user what does it mean? def my_form(): return render_template('my-form.html') @app.route('/', methods=['POST']) def my_form_post(): text = request.form['text'] processed_text = text.upper() return processed_text this code receive data from html form how receive data if user requests for example with curl or something? html to python - whatl0ol - Dec-06-2018 this is ip http://178.128.203.72/ i have index.html and server.pyimport requests from flask import Flask, jsonify,render_template,request app = Flask(__name__) @app.route("/", methods=['GET', 'POST']) def main(): return render_template('index.html') if __name__ == ("__main__"): app.run(debug = True, host = "0.0.0.0", port =80)I want to input which entered in index.html take with server.py and send to api then return result thanks how can ii do this? RE: from API to ip - nilamo - Dec-06-2018 (Dec-05-2018, 10:19 PM)whatl0ol Wrote: @app.route("/")You're returning an empty json object. That last line should be return data or return jsonify(data) if you want to send it to the client.(Dec-05-2018, 10:19 PM)whatl0ol Wrote: this code receive data from html form how receive data if user requests for example with curl or something?Exactly the same way. Making a POST request is the same, whether it's from curl or javascript or html-forms. If you use curl to post to / , you should get the text.
RE: from API to ip - whatl0ol - Dec-06-2018 (Dec-06-2018, 04:45 AM)nilamo Wrote:(Dec-05-2018, 10:19 PM)whatl0ol Wrote: @app.route("/")You're returning an empty json object. That last line should be undestand, now my problem is how get input from index.html in server.py RE: from API to ip - nilamo - Dec-06-2018 By submitting it in a post request. |