Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
from API to ip
#1
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
Reply
#2
(Dec-05-2018, 09:27 PM)Larz60+ Wrote: Repost in jobs, showing what you offer for compensation.
We will help you write code, but will not write it for you.
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
Reply
#3
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
Reply
#4
(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 ?
Reply
#5
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.
Reply
#6
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?
Reply
#7
this is ip http://178.128.203.72/

i have index.html

Output:
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>whatl0ol</title> <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}"> </head> <body> <div class="login"> <div class="heading"> <h2></h2> <form action=""{{ url_for('main') }}" method="post""> <div class="input-group input-group-lg"> <span class="input-group-addon"><i class="fa fa-user"></i></span> <input type="text" class="form-control" placeholder=""> </div> <button type="submit" id="send" name="send" class="float">check</button> </form> </div> </div> </body> </html>
and server.py

import 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?
Reply
#8
(Dec-05-2018, 10:19 PM)whatl0ol Wrote: @app.route("/")
def main():
response = requests.get("API",auth=('user', 'pass'))
data = response.json()
return jsonify()
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.
Reply
#9
(Dec-06-2018, 04:45 AM)nilamo Wrote:
(Dec-05-2018, 10:19 PM)whatl0ol Wrote: @app.route("/")
def main():
response = requests.get("API",auth=('user', 'pass'))
data = response.json()
return jsonify()
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.

undestand, now my problem is how get input from index.html in server.py
Reply
#10
By submitting it in a post request.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020