flask websockets app without serving webpage - python

I have an existing Python flask app that uses blueprints for routing. The app doesn't serve webpages, it just provides REST API for the client. Now I'm trying to add WebSockets support to this flask app to push some notifications to the client (they should originate on the server, not respond to client requests). I found Flask-SocketIO library, but it says that:
"The application must serve a page to the client that loads the
Socket.IO library and establishes a connection."
Is there a way to use WebSockets in a flask app without serving webpages?

You can use the Flask-Sockets library that can be installed with pip. Just follow the documentation and examples shown in this page https://github.com/heroku-python/flask-sockets. It is fairly easy to use and does not need to have a page served by flask. Though you still need a page for your client's browser to originate from somewhere.
If you only want to use WebSockets you can also use the WebSockets library: https://pypi.org/project/websockets/. Also easy to use and not linked to flask.

Related

About a local connection between Flutter and Python

I would like to connect Flutter with a Python backend. Several tutorials such as ML models using flask or online tutorials suggest to use the REST-API of FLASK to connect Flutter and Python.
Usually, a localhost is created with FLASK and the flutter app gets the information via the http package.
I need to create a flutter-python connection, that is very secure and local, that means, that when the App is released, the Python function may not work on a server, but on the phone.
Is is possible to realize the procedure of running the Flutter frontend initializing the Flask backend on a phone?
I did some research and found an answer to hopefully point you on the right track. The solution looks similar. Let me know!
https://stackoverflow.com/a/62308032/14514188
https://github.com/rikulo/socket.io-client-dart#usage-flutter
In Flutter env. it only works with
dart:io websocket,
not with
dart:html websocket,
so in this case you have to add 'transports': ['websocket'] when creates the socket instance.
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
'transports': ['websocket'],
'extraHeaders': {'foo': 'bar'} // optional
});
The default Flask development server doesn't support websockets so you'll need to use another server. Thankfully it's simple to get eventlet working with Flask. All you should have to do is install the eventlet package using pip.
pip install eventlet
Once eventlet is installed socketio will detect and use it when running the server.
You can use chrome to double check what transport method is being used. Open your chrome dev tools Ctrl+Shift+I in Windows and go to the Network tab. On each network request you should see either transport=polling or transport=websocket
https://stackoverflow.com/a/62308032/14514188

Is it possible to use python tornado framework with phonegap?

I'm developing an multi-page web application using python tornado framework (includes API), and because the application is said to be used in mobile I decided to use phonegap to test on mobile.
Now the question, Is it possible to make tornado application listen to the phonegap (from mobile) ?
basically you want to install Tornado app in some/local server server, and create API calls from Phonegap -> Tornado server ?
It's possible, if you're developing locally - you have a local address that you define ( 127.0.0.1:8080 or whatever )
Then from phonegap you can generate Ajax calls towards your server.
The best approach in such case, is to not use Tornado with HTML files, with API work always with JSON outputs ....

How to use pyramid cookie to authenticate user in tornado web framework?

In a server, I have a pyramid app running and I plan to have another tornado app running in parallel in the same machine. Let's say the pyramid app is available in www.abcd.com, then the tornado app will be available in www.abcd.com:9000.
I want only the authenticated users in the pyramid webapp to be able to access the tornado webapp.
My guess is somehow using cookie set by the pyramid app in tornado.
Is it possible? What is the best way to do that?
The two locations are separate origins in HTTP language. By default, they should not share cookies.
Before trying to figure out how to pass cookies around I'd try to set up a front end web server like Nginx that would proxy requests between two different backend servers. Both applications could get their own path, served from www.abcd.com.

Do rest servers need to be hosted on a website or CMS?

I need to create a REST server of a python module/API of a BCI, so that the application can be accessed on my Drupal website. Will I need to create and host the REST server on a python-based website or CMS, so that it can be accessed by my Drupal website, or is the api and rest server uploaded and hosted directly on my web hosting server? If so, what is the simplest python CMS that for creating a REST server for a python module/API already available?
The beauty of REST is precisely that it doesn't matter where your API is, as long as its accessible from your Drupal server, or from the client if you have a javascript API client.
If it's a simple application and you have admin access to your Drupal server, there's nothing preventing you from hosting the Python webservice side-by-side. They may even share the same HTTP Server, like Apache or Nginx, although depending on your demands on each one it might be better to keep them separate.
If you're new to Python, the Flask framework is a decent option to write a simple REST-like API interfacing with a Python module.

Should I use orbited or gevent for integrating comet functionality into a django app

I have been working with Django for some time now and have written several apps on a setup that uses Apache 2 mod_wsgi and a PostgreSQL database on ubuntu.
I have aa app that uses xsendfile to serve files from Apache via a Django view, and also allow users to upload files via a form as well. All this working great, but I now want to ramp up the features (and the complexity I am sure) by allowing users to chat and to see when new files have been uploaded without refreshing their browser.
As I want this to be scale-able, I don't want to poll continually with AJAX as this is going to get very heavy with large numbers of users.
I have read more posts, sites and blogs then I can count on integrating comet functionality into a Django app but there are so many different opinions out there on how to do this that I am now completely confused.
Should I be using orbited, gevent, iosocket?
Where does Tornado fit into this debate?
I want the messages also be stored on the database, so do I need any special configuration
to prevent my application blocking when writing to the database?
Will running a chat server with Django have any impact on my ability to serve files from Apache?
I'd recommend using WebSockets for bidirectional realtime communication. Keep running Django as is and run a WebSocket server on another port. As far as your database blocking, yes, you'll need to keep that in mind as you write your WebSocket server and either use a non-blocking database driver, or address that in some way.
Client-side you'll want to use Socket.IO or web-socket-js to support flash fallback for older browsers which don't support flash.
For the server, I would lean towards gevent or tornado, personally. For gevent there is gevent-websocket and gevent-socketio, for tornado you get built-in WebSocket support and can use tornadio if you want to use Socket.IO. Eventlet and twisted both support WebSockets as well. There is also a pretty cool new project called autobahn which is built on twisted, and meinheld has WebSocket middleware you can use.
WebSockets are pretty exciting, and as such there are tons of great posts out there on the subject. I found these posts useful:
http://gehrcke.de/2011/06/the-best-and-simplest-tools-to-create-a-basic-websocket-application-with-flash-fallback-and-python-on-the-server-side/
http://codysoyland.com/2011/feb/6/evented-django-part-one-socketio-and-gevent/
http://toastdriven.com/blog/2011/jul/31/gevent-long-polling-you/
http://blog.jupo.org/post/8858247674/real-time-web-apps-with-django-and-websockets/
Instead of Apache + X-Sendfile you could use Nginx + X-Accel-Redirect. That way you can run a gevent/wsgi/django server behind Nginx with views that provide long-polling. No need for a separate websockets server.
I've used both Apache + X-Sendfile and Nginx + X-Accel-Redirect to serve (access-protected) content on Webfaction without any problems.

Categories