How do I get data from a url callback - python

I am using a web service that will send a url callback when a particular external event happens (that service is monitoring it 24-7)
I need to have a python script running (locally on my own computer) and waiting (possibly just looping) to receive that callback and then do something with the data.
How do I do this? Do I need to have a webserver running? Where do I look for the data? I am pretty experienced with python, but not much in the use of HTTP and other web related things.
I have looked at other stackoverflow posts but they all seem to presume some prior knowledge.... I need the basics!

Here is a simple server using Bottle Microframework .
from bottle import Bottle, run, route, request
app = Bottle()
#app.route('/listener')
def my_listener():
data = request.query.your_data
#do_something_with_data(data)
return data
run(app, host="0.0.0.0", port=8080)
You can send data to server requesting http://sever_ip:8080/listener?your_data=some_data

Look into setting up a Flask server that will run in the background and listen for the callback request

Related

How to store a message from the client in the server and send it further via some route in Flask

From a web application, this is making a request to the backend application (Python with Flask and flask-socketio). From this route on the backend, an emit should be done to a socketio client standalone application. This works fine, but when the client app sends back a message directly after, I want to retrieve this message and send it back in my route to the web application. The message I get back from the client via a callback will be asynchronous, so how in the simplest manner could this be achieved? Each time I fetch the message from the client, the route has already sent back a reply to the web app without the message.
I fully understand that this flow is usually not normal, but can this be achieved without saving this message into a database, but store it somewhere on the backend and send it back to the web app?
You can use an Event object from the Python standard library.
from threading import Event
my_event = Event()
In your Flask route:
my_event.wait() # block until the event is signaled
return socketio_response
In your Socket.IO callback function:
socketio_response = data
my_event.set() # alert the route that a result is now available

Is http call significantly slower than local call generally and particularly in Flask?

I have a python library and need to provide services for others to use. Since the local installation is not a simple 'pip install ...' to complete, I want to set up a http service in Flask so users don't need to locally install it.
If the http server is very close to users' machines, I have a couple of questions to ask, since I am new to http service:
Is http call significantly slower than local call?
In local calls, my api can process texts (large amount of plaint text) in batches to speed up, instead of one by one processing. But the typical http service receives one text, process it and return it. Is that would be slower? Or the http client needs to write concurrent code to make http calls and the http service doesn't need to worry about any concurrency issue.
Is there any general method to speed up http service call?
My http service code would look like:
from flask import Flask
from flask import request
from flask import jsonify
api = Flask(__name__)
#api.route('/process', methods=['GET'])
def process():
text, stopwords = _parse_parameters()
pos = str(wlu.process(text, stopwords=stopwords))
return pos
def _parse_parameters():
text = request.args.get('text')
stopwords = request.args.get('stopwords')
return (text, stopwords)
if __name__ == '__main__':
#api.run(host='0.0.0.0', port='5000')
from waitress import serve
serve(api, host="0.0.0.0", port=5000)
EDIT:
'Local call' means users install the python library locally and do the typical importing of dependencies:
import my_library

Publish a script online

I have a Python script written up and the output of this script is a list.
Right now I need to get it online and make it accessible to others. I looked at Django , but then I realized that it may be kind of hard to create the UI. Is there any simple way to create a UI in Django and map it to an existing Python script.
Right now I using nltk, numpy, sqlite3 and things like that. Or is there a simpler way by which I can proceed?
In your case, Django is redundant.
You can use something smaller, Flask or maybe Aiohttp.
For example, all you need in aiohttp:
basic hmtl template
handler for one url (here you will call your script)
aiohttp webserver
The main idea:
Your server catch some url (for example /),
start your script, receives result,
respond with your html template (also render script result in it).
You can try creating a flask App.
Just do a pip install Flask and try the code below
from flask import Flask
import flask
import json
from flask import Response
app = Flask(__name__)
#app.route('/test',methods=['GET'])
def test():
'''
GET: Receives the request in /test route and returns a response containing {"response": [1,2,3]}
'''
my_list = [1,2,3]
resp = Response(response=json.dumps({"response": my_list}), status=200, mimetype='application/json')
return resp
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8082)
Then to test your app from your browser accessing
localhost:8082/test
or also through some app like postman
I would suggest looking into something like React for creating the UI. This way your UI would only make calls to your Flask server.

Making asynchronous HTTP requests from a flask service

I have a couple different needs for asynchrony in my Python 3.6 Flask RESTful web service running under Gunicorn.
1) I'd like for one of my service's routes to be able to send an HTTP request to another HTTP service and, without waiting for the response, send a response back to the client that called my service.
Some example code:
#route
def fire_and_forget():
# Send request to other server without waiting
# for it to send a response.
# Return my own response.
2) I'd like for another one of my service's routes to be able to send 2 or more asynchronous HTTP requests to other HTTP services and wait for them all to reply before my service sends a response.
Some example code:
#route
def combine_results():
# Send request to service A
# Send request to service B
# Wait for both to return.
# Do something with both responses
# Return my own response.
Thanks in advance.
EDIT: I am trying to avoid the additional complexity of using a queue (e.g. celery).
You can use eventlets for the the second use case. It's pretty easy to do:
import eventlet
providers = [EventfulPump(), MeetupPump()]
try:
pool = eventlet.GreenPool()
pile = eventlet.GreenPile(pool)
for each in providers:
pile.spawn(each.get, [], 5, loc) # call the interface method
except (PumpFailure, PumpOverride):
return abort(503)
results = []
for res in pile:
results += res
You can wrap each of your api endpoints in a class that implements a "common interface" (in the above it is the get method) and you can make the calls in parallel. I just place them all in a list.
Your other use case is harder to accomplish in straight python. At least a few years ago you would be forced to introduce some sort of worker process like celery to get something like that done. This question seems to cover all the issues:
Making an asynchronous task in Flask
Perhaps things have changed in flask land?

How to pass URLs as parameters in a GET request within python flask (restplus)?

I am creating a REST API using python flask. The API is ready and works on port number 8000 of my localhost. Now I intend to give this REST API a user friendly interface for which I decided to go with python - restplus. I thought of calling this service (running on 8000) internally from swagger application running on 5000
I was able to create the basic structure of the API (Swagger). The code for which looks like this:
import flask
from flask import Flask, request
from flask_restplus import Resource, Api
app = Flask(__name__)
api = Api(app)
#api.route('/HybridComparator/<string:url2>/<string:url1>')
class HybridComparator(Resource):
def get(self, url1, url2):
print(url1)
print(url2)
return url1 + ' ' + url2
if __name__ == '__main__':
app.run(debug=True)
The application as a whole runs seamlessly (with random strings as parameters) on port 5000. But when the URLs I pass are actual links, the application returns a response of 404 - Not found. Further to my investigation I realized the culprit being '/' embedded within the links I try to provide. Is there a way to handle URLs in particular?
Should I encode them before sending a request. (This will make my parameters look ugly). Is there something I am missing?
This is an entirely old question and I am sure you solved your problem by now.
But for new searchers, this may come in handy;
replace <string:url2>/<string:url1> with <path:url2>/<path:url1>
it seems that :
#api.route('/HybridComparator/<path:url2>/<path:url1>')
should fix it ,it fixes the 404 but i am getting only "http:/" part of the param

Categories